Repository navigation
Expand file tree
/
Copy pathworkflow.go
More file actions
84 lines (69 loc) · 1.93 KB
/
Copy pathworkflow.go
File metadata and controls
84 lines (69 loc) · 1.93 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
package main
import (
"fmt"
"strings"
"github.com/google/go-github/v92/github"
"github.com/prometheus/client_golang/prometheus"
)
type (
GithubWorkflowList struct {
org string
repo *github.Repository
list map[int64]*GitHubWorkflow
}
GitHubWorkflow struct {
*github.Workflow
ref *GithubWorkflowList
}
)
func NewGithubWorkflowList(org string, repo *github.Repository) *GithubWorkflowList {
return &GithubWorkflowList{
org: org,
repo: repo,
list: map[int64]*GitHubWorkflow{},
}
}
func (list *GithubWorkflowList) Add(workflow ...*github.Workflow) {
for _, item := range workflow {
list.list[*item.ID] = new(GitHubWorkflow{Workflow: item, ref: list})
}
}
func (list *GithubWorkflowList) GetById(id int64) *GitHubWorkflow {
if workflow, ok := list.list[id]; ok {
return workflow
}
return nil
}
func (list *GithubWorkflowList) Count() int {
return len(list.list)
}
func (list *GithubWorkflowList) List() map[int64]*GitHubWorkflow {
return list.list
}
func (workflow *GitHubWorkflow) IsActive() bool {
if workflow != nil {
return strings.EqualFold(workflow.GetState(), "active")
}
return false
}
func (workflow *GitHubWorkflow) AddDefaultLabels(labels prometheus.Labels) {
// reset
labels["workflow"] = ""
labels["workflowUrl"] = ""
labels["workflowDefinitionUrl"] = ""
if workflow != nil {
// for whatever reason we only get the definition url, not the real "workflow" overview page
orgName := workflow.ref.repo.Organization.GetName()
repoName := workflow.ref.repo.GetName()
defaultBranch := workflow.ref.repo.GetDefaultBranch()
workflowOverviewUrl := strings.Replace(
workflow.GetHTMLURL(),
fmt.Sprintf("%s/%s/blob/%s/.github/workflows/", orgName, repoName, defaultBranch),
fmt.Sprintf("%s/%s/actions/workflows/", orgName, repoName),
1,
)
labels["workflow"] = workflow.GetName()
labels["workflowUrl"] = workflowOverviewUrl
labels["workflowDefinitionUrl"] = workflow.GetHTMLURL()
}
}