Skip to content

Commit

Permalink
trivial: removed duplicated code and fixed CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
galbirk committed Apr 8, 2024
1 parent f3680ee commit f0537e1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 49 deletions.
93 changes: 79 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"context"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand All @@ -20,13 +19,17 @@ import (
// Config struct holds all of the runtime confgiguration for the application
type Config struct {
*cfg.BaseConfig
apiUrl *url.URL
repositories []string
organisations []string
users []string
apiToken string
targetURLs []string
gitHubApp bool
apiUrl *url.URL
repositories []string
organisations []string
users []string
apiToken string
targetURLs []string
gitHubApp bool
gitHubAppKeyPath string
gitHubAppId int64
gitHubAppInstallationId int64
gitHubRateLimit float64
}

// Init populates the Config struct based on environmental runtime configuration
Expand All @@ -45,6 +48,10 @@ func Init() Config {
"",
nil,
false,
"",
0,
0,
15000,
}

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

gitHubApp := os.Getenv("GITHUB_APP")
gitHubApp := strings.ToLower(os.Getenv("GITHUB_APP"))
if gitHubApp == "true" {
gitHubAppKeyPath := os.Getenv("GITHUB_APP_KEY_PATH")
gitHubAppId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_ID"), 10, 64)
gitHubAppInstalaltionId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_INSTALLATION_ID"), 10, 64)
appConfig.SetAPITokenFromGitHubApp(gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
gitHubRateLimit, _ := strconv.ParseFloat(cfg.GetEnv("GITHUB_RATE_LIMIT", "15000"), 64)
appConfig.SetGitHubApp(true)
appConfig.SetGitHubAppKeyPath(gitHubAppKeyPath)
appConfig.SetGitHubAppId(gitHubAppId)
appConfig.SetGitHubAppInstallationId(gitHubAppInstalaltionId)
appConfig.SetGitHubRateLimit(gitHubRateLimit)
err = appConfig.SetAPITokenFromGitHubApp()
if err != nil {
log.Errorf("Error initializing Configuration, Error: %v", err)
}
}

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

// Returns the GitHub App atuhentication value
func (c *Config) GitHubApp() bool {
return c.gitHubApp
}

// SetGitHubAppKeyPath accepts a string for GitHub app private key path
func (c *Config) GitHubAppKeyPath() string {
return c.gitHubAppKeyPath
}

// SetGitHubAppId accepts a string for GitHub app id
func (c *Config) GitHubAppId() int64 {
return c.gitHubAppId
}

// SetGitHubAppInstallationId accepts a string for GitHub app installation id
func (c *Config) GitHubAppInstallationId() int64 {
return c.gitHubAppInstallationId
}

// SetGitHubAppRateLimit accepts a string for GitHub RateLimit
func (c *Config) GitHubRateLimit() float64 {
return c.gitHubRateLimit
}

// Sets the base API URL returning an error if the supplied string is not a valid URL
func (c *Config) SetAPIURL(u string) error {
ur, err := url.Parse(u)
Expand Down Expand Up @@ -140,9 +181,34 @@ func (c *Config) SetAPITokenFromFile(tokenFile string) error {
return nil
}

// SetGitHubApp accepts a boolean for GitHub app authentication
func (c *Config) SetGitHubApp(githubApp bool) {
c.gitHubApp = githubApp
}

// SetGitHubAppKeyPath accepts a string for GitHub app private key path
func (c *Config) SetGitHubAppKeyPath(gitHubAppKeyPath string) {
c.gitHubAppKeyPath = gitHubAppKeyPath
}

// SetGitHubAppId accepts a string for GitHub app id
func (c *Config) SetGitHubAppId(gitHubAppId int64) {
c.gitHubAppId = gitHubAppId
}

// SetGitHubAppInstallationId accepts a string for GitHub app installation id
func (c *Config) SetGitHubAppInstallationId(gitHubAppInstallationId int64) {
c.gitHubAppInstallationId = gitHubAppInstallationId
}

// SetGitHubAppRateLimit accepts a string for GitHub RateLimit
func (c *Config) SetGitHubRateLimit(gitHubRateLimit float64) {
c.gitHubRateLimit = gitHubRateLimit
}

// SetAPITokenFromGitHubApp generating api token from github app configuration.
func (c *Config) SetAPITokenFromGitHubApp(gitHubAppId int64, gitHubAppInstalaltionId int64, gitHubAppKeyPath string) error {
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
func (c *Config) SetAPITokenFromGitHubApp() error {
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, c.gitHubAppId, c.gitHubAppInstallationId, c.gitHubAppKeyPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,8 +247,7 @@ func (c *Config) setScrapeURLs() error {
}

// Append github orginisations to the array



if len(c.organisations) > 0 {
for _, x := range c.organisations {
y := *c.apiUrl
Expand Down
43 changes: 8 additions & 35 deletions exporter/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package exporter

import (
"context"
"errors"
"net/http"
"os"
"path"
"strconv"
"strings"

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

gitHubApp := os.Getenv("GITHUB_APP")
if strings.ToLower(gitHubApp) == "true" {
if e.Config.GitHubApp() {
needReAuth, err := e.isTokenExpired()
if err != nil {
log.Errorf("Error checking token expiration status: %v", err)
return
}
if needReAuth {
e.reAuth()
err = e.Config.SetAPITokenFromGitHubApp()
if err != nil {
log.Errorf("Error authenticating with GitHub app: %v", err)
}
}
}
// Scrape the Data from Github
Expand Down Expand Up @@ -79,7 +75,7 @@ func (e *Exporter) isTokenExpired() (bool, error) {
defer resp.Body.Close()
// Triggers if rate-limiting isn't enabled on private Github Enterprise installations
if resp.StatusCode == 404 {
return false, errors.New("404 Error")
return false, nil
}

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

defaultLimit := os.Getenv("GITHUB_RATE_LIMIT")
if len(defaultLimit) == 0 {
defaultLimit = "15000"
}
defaultLimitInt, err := strconv.ParseInt(defaultLimit, 10, 64)
if err != nil {
return false, err
}
if limit < float64(defaultLimitInt) {
defaultRateLimit := e.Config.GitHubRateLimit()
if limit < defaultRateLimit {
return true, nil
}
return false, nil

}

func (e *Exporter) reAuth() error {
gitHubAppKeyPath := os.Getenv("GITHUB_APP_KEY_PATH")
gitHubAppId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_ID"), 10, 64)
gitHubAppInstalaltionId, _ := strconv.ParseInt(os.Getenv("GITHUB_APP_INSTALLATION_ID"), 10, 64)
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, gitHubAppId, gitHubAppInstalaltionId, gitHubAppKeyPath)
if err != nil {
return err
}
strToken, err := itr.Token(context.Background())
if err != nil {
return err
}
e.Config.SetAPIToken(strToken)
return nil
}

0 comments on commit f0537e1

Please sign in to comment.