Skip to content

Commit 560314d

Browse files
authored
Merge pull request #1 from iamrare/add-more-functionality
Add more functionality
2 parents d94272f + 56c01b3 commit 560314d

4 files changed

Lines changed: 42 additions & 3 deletions

File tree

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{}, 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))
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"`

0 commit comments

Comments
 (0)