-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathget_workflow_runs_from_github.go
More file actions
97 lines (88 loc) · 2.64 KB
/
get_workflow_runs_from_github.go
File metadata and controls
97 lines (88 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package metrics
import (
"context"
"github-actions-exporter/pkg/config"
"log"
"strconv"
"strings"
"time"
"github.com/google/go-github/v33/github"
)
// getFieldValue return value from run element which corresponds to field
func getFieldValue(repo string, run github.WorkflowRun, field string) string {
switch field {
case "repo":
return repo
case "id":
return strconv.FormatInt(*run.ID, 10)
case "node_id":
return *run.NodeID
case "head_branch":
return *run.HeadBranch
case "head_sha":
return *run.HeadSHA
case "run_number":
return strconv.Itoa(*run.RunNumber)
case "workflow_id":
return strconv.FormatInt(*run.WorkflowID, 10)
case "workflow":
return *workflows[repo][*run.WorkflowID].Name
case "event":
return *run.Event
case "status":
return *run.Status
case "conclusion":
return run.GetConclusion()
case "created_at":
return run.CreatedAt.Format(time.RFC3339)
case "updated_at":
return run.UpdatedAt.Format(time.RFC3339)
}
return ""
}
//
func getRelevantFields(repo string, run *github.WorkflowRun) []string {
relevantFields := strings.Split(config.WorkflowFields, ",")
result := make([]string, len(relevantFields))
for i, field := range relevantFields {
result[i] = getFieldValue(repo, *run, field)
}
return result
}
// getWorkflowRunsFromGithub - return informations and status about a worflow
func getWorkflowRunsFromGithub() {
for {
for _, repo := range config.Github.Repositories.Value() {
r := strings.Split(repo, "/")
resp, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), r[0], r[1], nil)
if err != nil {
log.Printf("ListRepositoryWorkflowRuns error for %s: %s", repo, err.Error())
} else {
for _, run := range resp.WorkflowRuns {
var s float64 = 0
if run.GetConclusion() == "success" {
s = 1
} else if run.GetConclusion() == "skipped" {
s = 2
} else if run.GetConclusion() == "in_progress" {
s = 3
} else if run.GetConclusion() == "queued" {
s = 4
}
fields := getRelevantFields(repo, run)
workflowRunStatusGauge.WithLabelValues(fields...).Set(s)
resp, _, err := client.Actions.GetWorkflowRunUsageByID(context.Background(), r[0], r[1], *run.ID)
if err != nil { // Fallback for Github Enterprise
created := run.CreatedAt.Time.Unix()
updated := run.UpdatedAt.Time.Unix()
elapsed := updated - created
workflowRunDurationGauge.WithLabelValues(fields...).Set(float64(elapsed * 1000))
} else {
workflowRunDurationGauge.WithLabelValues(fields...).Set(float64(resp.GetRunDurationMS()))
}
}
}
}
time.Sleep(time.Duration(config.Github.Refresh) * time.Second)
}
}