Skip to content

Commit 0552b53

Browse files
authored
Merge pull request #46 from iamrare/master
`github_repo_open_issues` and `github_repo_pull_request_count` separately
2 parents d94272f + a7c07d8 commit 0552b53

File tree

9 files changed

+1046
-8
lines changed

9 files changed

+1046
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- "1.12"
4+
- "1.14"
55

66
script:
77
- diff -u <(echo -n) <(gofmt -s -d ./)

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.12-stretch as build
1+
FROM golang:1.14.0-stretch as build
22
LABEL maintainer="Infinity Works"
33

44
ENV GO111MODULE=on
@@ -10,7 +10,7 @@ RUN go mod download \
1010
&& go test ./... \
1111
&& CGO_ENABLED=0 GOOS=linux go build -o /bin/main
1212

13-
FROM alpine:3.10
13+
FROM alpine:3.11.3
1414

1515
RUN apk --no-cache add ca-certificates \
1616
&& addgroup exporter \

exporter/gather.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func (e *Exporter) gatherData() ([]*Datum, *RateLimits, error) {
3535
if strings.Contains(response.url, "/repos/") {
3636
getReleases(e, response.url, &d.Releases)
3737
}
38+
// Get PRs
39+
if strings.Contains(response.url, "/repos/") {
40+
getPRs(e, response.url, &d.Pulls)
41+
}
3842
json.Unmarshal(response.body, &d)
3943
data = append(data, d)
4044
}
@@ -111,6 +115,20 @@ func getReleases(e *Exporter, url string, data *[]Release) {
111115
json.Unmarshal(releasesResponse[0].body, &data)
112116
}
113117

118+
func getPRs(e *Exporter, url string, data *[]Pull) {
119+
i := strings.Index(url, "?")
120+
baseURL := url[:i]
121+
pullsURL := baseURL + "/pulls"
122+
pullsResponse, err := asyncHTTPGets([]string{pullsURL}, e.APIToken)
123+
124+
if err != nil {
125+
log.Errorf("Unable to obtain pull requests from API, Error: %s", err)
126+
}
127+
fmt.Println(&data)
128+
129+
json.Unmarshal(pullsResponse[0].body, &data)
130+
}
131+
114132
// isArray simply looks for key details that determine if the JSON response is an array or not.
115133
func isArray(body []byte) bool {
116134

exporter/metrics.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ func AddMetrics() map[string]*prometheus.Desc {
1818
"Total number of open issues for given repository",
1919
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
2020
)
21+
APIMetrics["PullRequestCount"] = prometheus.NewDesc(
22+
prometheus.BuildFQName("github", "repo", "pull_request_count"),
23+
"Total number of pull requests for given repository",
24+
[]string{"repo"}, nil,
25+
)
2126
APIMetrics["Watchers"] = prometheus.NewDesc(
2227
prometheus.BuildFQName("github", "repo", "watchers"),
2328
"Total number of watchers/subscribers for given repository",
@@ -64,7 +69,6 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr
6469
for _, x := range data {
6570
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Stars"], prometheus.GaugeValue, x.Stars, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
6671
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Forks"], prometheus.GaugeValue, x.Forks, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
67-
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, x.OpenIssues, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
6872
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Watchers"], prometheus.GaugeValue, x.Watchers, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
6973
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Size"], prometheus.GaugeValue, x.Size, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
7074

@@ -73,6 +77,15 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr
7377
ch <- prometheus.MustNewConstMetric(e.APIMetrics["ReleaseDownloads"], prometheus.GaugeValue, float64(asset.Downloads), x.Name, x.Owner.Login, release.Name, asset.Name, asset.CreatedAt)
7478
}
7579
}
80+
prCount := 0
81+
for range x.Pulls {
82+
prCount += 1
83+
}
84+
// issueCount = x.OpenIssue - prCount
85+
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, (x.OpenIssues - float64(prCount)), x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
86+
87+
// prCount
88+
ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequestCount"], prometheus.GaugeValue, float64(prCount), x.Name)
7689
}
7790

7891
// Set Rate limit stats

exporter/structs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ type Datum struct {
3838
Watchers float64 `json:"subscribers_count"`
3939
Size float64 `json:"size"`
4040
Releases []Release
41+
Pulls []Pull
4142
}
4243

4344
type Release struct {
4445
Name string `json:"name"`
4546
Assets []Asset `json:"assets"`
4647
}
4748

49+
type Pull struct {
50+
Url string `json:"url"`
51+
User struct {
52+
Login string `json:"login"`
53+
} `json:"user"`
54+
}
55+
4856
type Asset struct {
4957
Name string `json:"name"`
5058
Size int64 `json:"size"`

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/infinityworks/github-exporter
22

3-
go 1.12
3+
go 1.14
44

55
require (
66
github.com/fatih/structs v1.1.0

test/github_exporter_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ func TestGithubExporter(t *testing.T) {
3333
githubRepos(),
3434
githubRateLimit(),
3535
githubReleases(),
36+
githubPulls(),
3637
).
3738
Get("/metrics").
3839
Expect(t).
3940
Assert(bodyContains(`github_rate_limit 60`)).
4041
Assert(bodyContains(`github_rate_remaining 60`)).
4142
Assert(bodyContains(`github_rate_reset 1.566853865e+09`)).
4243
Assert(bodyContains(`github_repo_forks{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 10`)).
44+
Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo"} 3`)).
4345
Assert(bodyContains(`github_repo_open_issues{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 2`)).
4446
Assert(bodyContains(`github_repo_size_kb{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 946`)).
4547
Assert(bodyContains(`github_repo_stars{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 120`)).
@@ -105,6 +107,17 @@ func githubReleases() *apitest.Mock {
105107
End()
106108
}
107109

110+
func githubPulls() *apitest.Mock {
111+
return apitest.NewMock().
112+
Get("https://api.github.com/repos/myOrg/myRepo/pulls").
113+
Header("Authorization", "token 12345").
114+
RespondWith().
115+
Times(2).
116+
Body(readFile("testdata/pulls_response.json")).
117+
Status(http.StatusOK).
118+
End()
119+
}
120+
108121
func readFile(path string) string {
109122
bytes, err := ioutil.ReadFile(path)
110123
if err != nil {

test/testdata/my_repo_response.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"mirror_url": null,
8686
"archived": false,
8787
"disabled": false,
88-
"open_issues_count": 2,
88+
"open_issues_count": 5,
8989
"license": {
9090
"key": "mit",
9191
"name": "MIT License",
@@ -94,9 +94,9 @@
9494
"node_id": "MDc6TGljZW5zZTEz"
9595
},
9696
"forks": 10,
97-
"open_issues": 2,
97+
"open_issues": 5,
9898
"watchers": 120,
9999
"default_branch": "master",
100100
"network_count": 10,
101101
"subscribers_count": 5
102-
}
102+
}

0 commit comments

Comments
 (0)