-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
228 lines (186 loc) · 4.86 KB
/
main.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/google/go-github/v53/github"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/oauth2"
)
type Issue struct {
Number int
Labels []*github.Label
User string
Repo string
}
type Repo struct {
Owner string
Name string
}
var (
//nolint:gochecknoglobals
IssueCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "github_issue",
Subsystem: "prometheus_exporter",
Name: "issue_count",
Help: "Number of issues",
},
[]string{"number", "label", "author", "repo"},
)
)
func main() {
if err := core(); err != nil {
log.Fatal(err)
}
}
func core() error {
interval, err := getInterval()
if err != nil {
return err
}
githubToken, err := readGithubConfig()
if err != nil {
return fmt.Errorf("failed to read Datadog Config: %w", err)
}
repositories, err := getRepositories()
if err != nil {
return fmt.Errorf("failed to get GitHub repository name: %w", err)
}
repositoryList, err := parseRepositories(repositories)
if err != nil {
return err
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
label := getLabelForFilter()
prometheus.MustRegister(IssueCount)
http.Handle("/metrics", promhttp.Handler())
go func() {
ticker := time.NewTicker(time.Duration(interval) * time.Second)
// register metrics as background
for range ticker.C {
err := snapshot(label, repositoryList, client)
if err != nil {
log.Fatal(err)
}
}
}()
return http.ListenAndServe(":8080", nil)
}
func snapshot(label string, repositoryList []Repo, client *github.Client) error {
IssueCount.Reset()
issues, err := getIssues(repositoryList, label, client)
if err != nil {
return fmt.Errorf("failed to get Issues: %w", err)
}
issueInfos := getIssueInfos(issues)
for _, issueInfo := range issueInfos {
labelsTag := make([]string, len(issueInfo.Labels))
for i, label := range issueInfo.Labels {
labelsTag[i] = *label.Name
}
labels := prometheus.Labels{
"number": strconv.Itoa(issueInfo.Number),
"label": strings.Join(labelsTag, ","),
"author": issueInfo.User,
"repo": issueInfo.Repo,
}
IssueCount.With(labels).Set(1)
}
return nil
}
func getInterval() (int, error) {
const defaultGithubAPIIntervalSecond = 300
githubAPIInterval := os.Getenv("GITHUB_API_INTERVAL")
if len(githubAPIInterval) == 0 {
return defaultGithubAPIIntervalSecond, nil
}
integerGithubAPIInterval, err := strconv.Atoi(githubAPIInterval)
if err != nil {
return 0, fmt.Errorf("failed to read Datadog Config: %w", err)
}
return integerGithubAPIInterval, nil
}
func readGithubConfig() (string, error) {
githubToken := os.Getenv("GITHUB_TOKEN")
if len(githubToken) == 0 {
return "", fmt.Errorf("missing environment variable: GITHUB_TOKEN")
}
return githubToken, nil
}
func getIssues(githubRepositories []Repo, label string, client *github.Client) ([]*github.Issue, error) {
ctx := context.Background()
issues := []*github.Issue{}
for _, githubRepository := range githubRepositories {
const perPage = 100
issueListByRepoOptions := github.IssueListByRepoOptions{
Labels: []string{label},
ListOptions: github.ListOptions{PerPage: perPage},
}
for {
issuesInRepo, resp, err := client.Issues.ListByRepo(ctx, githubRepository.Owner, githubRepository.Name, &issueListByRepoOptions)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub Issues: %w", err)
}
issues = append(issues, issuesInRepo...)
if resp.NextPage == 0 {
break
}
issueListByRepoOptions.Page = resp.NextPage
}
}
return issues, nil
}
func getRepositories() (string, error) {
githubRepositories := os.Getenv("GITHUB_REPOSITORIES")
if len(githubRepositories) == 0 {
return "", fmt.Errorf("missing environment variable: GITHUB_REPOSITORIES")
}
return githubRepositories, nil
}
func getLabelForFilter() string {
githubLabel := os.Getenv("GITHUB_LABEL")
if len(githubLabel) == 0 {
return ""
}
return githubLabel
}
func parseRepositories(repositories string) ([]Repo, error) {
repos := strings.Split(repositories, ",")
arr := make([]Repo, len(repos))
for i, repo := range repos {
a := strings.Split(repo, "/")
if len(a) != 2 { //nolint:gomnd
return nil, errors.New("repository is invalid: " + repo)
}
arr[i] = Repo{
Owner: a[0],
Name: a[1],
}
}
return arr, nil
}
func getIssueInfos(issues []*github.Issue) []Issue {
issueInfos := make([]Issue, len(issues))
for i, issue := range issues {
repos := strings.Split(*issue.URL, "/")
issueInfos[i] = Issue{
Number: issue.GetNumber(),
Labels: issue.Labels,
User: issue.User.GetLogin(),
Repo: repos[4] + "/" + repos[5],
}
}
return issueInfos
}