@@ -2,7 +2,6 @@ package config
22
33import (
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
2120type 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
104145func (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
0 commit comments