Skip to content

Commit f0537e1

Browse files
committed
trivial: removed duplicated code and fixed CR comments
1 parent f3680ee commit f0537e1

File tree

2 files changed

+87
-49
lines changed

2 files changed

+87
-49
lines changed

config/config.go

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config
22

33
import (
44
"context"
5-
"io/ioutil"
65
"net/http"
76
"net/url"
87
"path"
@@ -20,13 +19,17 @@ import (
2019
// Config struct holds all of the runtime confgiguration for the application
2120
type Config struct {
2221
*cfg.BaseConfig
23-
apiUrl *url.URL
24-
repositories []string
25-
organisations []string
26-
users []string
27-
apiToken string
28-
targetURLs []string
29-
gitHubApp bool
22+
apiUrl *url.URL
23+
repositories []string
24+
organisations []string
25+
users []string
26+
apiToken string
27+
targetURLs []string
28+
gitHubApp bool
29+
gitHubAppKeyPath string
30+
gitHubAppId int64
31+
gitHubAppInstallationId int64
32+
gitHubRateLimit float64
3033
}
3134

3235
// Init populates the Config struct based on environmental runtime configuration
@@ -45,6 +48,10 @@ func Init() Config {
4548
"",
4649
nil,
4750
false,
51+
"",
52+
0,
53+
0,
54+
15000,
4855
}
4956

5057
err := appConfig.SetAPIURL(cfg.GetEnv("API_URL", "https://api.github.com"))
@@ -64,12 +71,21 @@ func Init() Config {
6471
appConfig.SetUsers(strings.Split(users, ", "))
6572
}
6673

67-
gitHubApp := os.Getenv("GITHUB_APP")
74+
gitHubApp := strings.ToLower(os.Getenv("GITHUB_APP"))
6875
if gitHubApp == "true" {
6976
gitHubAppKeyPath := os.Getenv("GITHUB_APP_KEY_PATH")
7077
gitHubAppId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_ID"), 10, 64)
7178
gitHubAppInstalaltionId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_INSTALLATION_ID"), 10, 64)
72-
appConfig.SetAPITokenFromGitHubApp(gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
79+
gitHubRateLimit, _ := strconv.ParseFloat(cfg.GetEnv("GITHUB_RATE_LIMIT", "15000"), 64)
80+
appConfig.SetGitHubApp(true)
81+
appConfig.SetGitHubAppKeyPath(gitHubAppKeyPath)
82+
appConfig.SetGitHubAppId(gitHubAppId)
83+
appConfig.SetGitHubAppInstallationId(gitHubAppInstalaltionId)
84+
appConfig.SetGitHubRateLimit(gitHubRateLimit)
85+
err = appConfig.SetAPITokenFromGitHubApp()
86+
if err != nil {
87+
log.Errorf("Error initializing Configuration, Error: %v", err)
88+
}
7389
}
7490

7591
tokenEnv := os.Getenv("GITHUB_TOKEN")
@@ -100,6 +116,31 @@ func (c *Config) APIToken() string {
100116
return c.apiToken
101117
}
102118

119+
// Returns the GitHub App atuhentication value
120+
func (c *Config) GitHubApp() bool {
121+
return c.gitHubApp
122+
}
123+
124+
// SetGitHubAppKeyPath accepts a string for GitHub app private key path
125+
func (c *Config) GitHubAppKeyPath() string {
126+
return c.gitHubAppKeyPath
127+
}
128+
129+
// SetGitHubAppId accepts a string for GitHub app id
130+
func (c *Config) GitHubAppId() int64 {
131+
return c.gitHubAppId
132+
}
133+
134+
// SetGitHubAppInstallationId accepts a string for GitHub app installation id
135+
func (c *Config) GitHubAppInstallationId() int64 {
136+
return c.gitHubAppInstallationId
137+
}
138+
139+
// SetGitHubAppRateLimit accepts a string for GitHub RateLimit
140+
func (c *Config) GitHubRateLimit() float64 {
141+
return c.gitHubRateLimit
142+
}
143+
103144
// Sets the base API URL returning an error if the supplied string is not a valid URL
104145
func (c *Config) SetAPIURL(u string) error {
105146
ur, err := url.Parse(u)
@@ -140,9 +181,34 @@ func (c *Config) SetAPITokenFromFile(tokenFile string) error {
140181
return nil
141182
}
142183

184+
// SetGitHubApp accepts a boolean for GitHub app authentication
185+
func (c *Config) SetGitHubApp(githubApp bool) {
186+
c.gitHubApp = githubApp
187+
}
188+
189+
// SetGitHubAppKeyPath accepts a string for GitHub app private key path
190+
func (c *Config) SetGitHubAppKeyPath(gitHubAppKeyPath string) {
191+
c.gitHubAppKeyPath = gitHubAppKeyPath
192+
}
193+
194+
// SetGitHubAppId accepts a string for GitHub app id
195+
func (c *Config) SetGitHubAppId(gitHubAppId int64) {
196+
c.gitHubAppId = gitHubAppId
197+
}
198+
199+
// SetGitHubAppInstallationId accepts a string for GitHub app installation id
200+
func (c *Config) SetGitHubAppInstallationId(gitHubAppInstallationId int64) {
201+
c.gitHubAppInstallationId = gitHubAppInstallationId
202+
}
203+
204+
// SetGitHubAppRateLimit accepts a string for GitHub RateLimit
205+
func (c *Config) SetGitHubRateLimit(gitHubRateLimit float64) {
206+
c.gitHubRateLimit = gitHubRateLimit
207+
}
208+
143209
// SetAPITokenFromGitHubApp generating api token from github app configuration.
144-
func (c *Config) SetAPITokenFromGitHubApp(gitHubAppId int64, gitHubAppInstalaltionId int64, gitHubAppKeyPath string) error {
145-
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
210+
func (c *Config) SetAPITokenFromGitHubApp() error {
211+
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, c.gitHubAppId, c.gitHubAppInstallationId, c.gitHubAppKeyPath)
146212
if err != nil {
147213
return err
148214
}
@@ -181,8 +247,7 @@ func (c *Config) setScrapeURLs() error {
181247
}
182248

183249
// Append github orginisations to the array
184-
185-
250+
186251
if len(c.organisations) > 0 {
187252
for _, x := range c.organisations {
188253
y := *c.apiUrl

exporter/prometheus.go

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package exporter
22

33
import (
4-
"context"
5-
"errors"
6-
"net/http"
7-
"os"
84
"path"
95
"strconv"
10-
"strings"
116

12-
"github.com/bradleyfalzon/ghinstallation"
137
"github.com/prometheus/client_golang/prometheus"
148
log "github.com/sirupsen/logrus"
159
)
@@ -29,15 +23,17 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
2923
data := []*Datum{}
3024
var err error
3125

32-
gitHubApp := os.Getenv("GITHUB_APP")
33-
if strings.ToLower(gitHubApp) == "true" {
26+
if e.Config.GitHubApp() {
3427
needReAuth, err := e.isTokenExpired()
3528
if err != nil {
3629
log.Errorf("Error checking token expiration status: %v", err)
3730
return
3831
}
3932
if needReAuth {
40-
e.reAuth()
33+
err = e.Config.SetAPITokenFromGitHubApp()
34+
if err != nil {
35+
log.Errorf("Error authenticating with GitHub app: %v", err)
36+
}
4137
}
4238
}
4339
// Scrape the Data from Github
@@ -79,7 +75,7 @@ func (e *Exporter) isTokenExpired() (bool, error) {
7975
defer resp.Body.Close()
8076
// Triggers if rate-limiting isn't enabled on private Github Enterprise installations
8177
if resp.StatusCode == 404 {
82-
return false, errors.New("404 Error")
78+
return false, nil
8379
}
8480

8581
limit, err := strconv.ParseFloat(resp.Header.Get("X-RateLimit-Limit"), 64)
@@ -88,33 +84,10 @@ func (e *Exporter) isTokenExpired() (bool, error) {
8884
return false, err
8985
}
9086

91-
defaultLimit := os.Getenv("GITHUB_RATE_LIMIT")
92-
if len(defaultLimit) == 0 {
93-
defaultLimit = "15000"
94-
}
95-
defaultLimitInt, err := strconv.ParseInt(defaultLimit, 10, 64)
96-
if err != nil {
97-
return false, err
98-
}
99-
if limit < float64(defaultLimitInt) {
87+
defaultRateLimit := e.Config.GitHubRateLimit()
88+
if limit < defaultRateLimit {
10089
return true, nil
10190
}
10291
return false, nil
10392

10493
}
105-
106-
func (e *Exporter) reAuth() error {
107-
gitHubAppKeyPath := os.Getenv("GITHUB_APP_KEY_PATH")
108-
gitHubAppId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_ID"), 10, 64)
109-
gitHubAppInstalaltionId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_INSTALLATION_ID"), 10, 64)
110-
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
111-
if err != nil {
112-
return err
113-
}
114-
strToken, err := itr.Token(context.Background())
115-
if err != nil {
116-
return err
117-
}
118-
e.Config.SetAPIToken(strToken)
119-
return nil
120-
}

0 commit comments

Comments
 (0)