Skip to content

Commit

Permalink
Addressed issues per request
Browse files Browse the repository at this point in the history
  • Loading branch information
Njegos Railic committed Nov 11, 2019
1 parent e9afc7c commit fa80c10
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
// Config holds the application configuration
type Config struct {
RepositoryPath string
CheckPaths bool `envconfig:"default=true"`
CheckOwners bool `envconfig:"default=true"`
Checks []string `envconfig:"optional"`
Github struct {
AccessToken string `envconfig:"optional"`
BaseURL string `envconfig:"optional"`
Expand All @@ -35,8 +34,6 @@ type Config struct {

func main() {
var cfg Config
var checks []check.Checker
var ghClient *github.Client
err := envconfig.Init(&cfg)
fatalOnError(err)

Expand All @@ -46,55 +43,30 @@ func main() {
defer cancelFunc()
cancelOnInterrupt(ctx, cancelFunc)

// check are the both checks disabled
if !cfg.CheckPaths && !cfg.CheckOwners {
log.Error("No checks requested. Please set CHECK_PATHS or CHECK_OWNERS. Aborting...")
fatalOnError(err)
}
fatalOnError(err)

// init codeowners entries
codeownersEntries, err := codeowners.NewFromPath(cfg.RepositoryPath)
fatalOnError(err)

<<<<<<< HEAD
<<<<<<< HEAD
if cfg.CheckPaths {
=======
if cfg.CheckPaths == true {
>>>>>>> Adding more checks for team permissions. Adding more ENV variables
=======
if cfg.CheckPaths {
>>>>>>> Fixed comparison to bool constant
checks = []check.Checker{
check.NewFileExist(),
}
// init checks
var checks []check.Checker

if isEnabled(cfg.Checks, "files") {
checks = append(checks, check.NewFileExist())
}

<<<<<<< HEAD
<<<<<<< HEAD
if cfg.CheckOwners {
=======
if cfg.CheckOwners == true {
>>>>>>> Adding more checks for team permissions. Adding more ENV variables
=======
if cfg.CheckOwners {
>>>>>>> Fixed comparison to bool constant
if isEnabled(cfg.Checks, "owners") {
// init GitHub client
httpClient := http.DefaultClient
if cfg.Github.AccessToken != "" {
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.Github.AccessToken},
))
}

ghClient, err = newGithubClient(cfg, httpClient)
ghClient, err := newGithubClient(cfg, httpClient)
fatalOnError(err)

checks = []check.Checker{
check.NewFileExist(),
check.NewValidOwner(cfg.OwnerChecker, ghClient),
}

checks = append(checks, check.NewValidOwner(cfg.OwnerChecker, ghClient))
}

// run check runner
Expand All @@ -113,6 +85,27 @@ func main() {
}
}

func isEnabled(checks []string, name string) bool {
// if a user does not specify concrete checks then all checks are enabled
if len(checks) == 0 {
return true
}

if contains(checks, name) {
return true
}
return false
}

func contains(checks []string, name string) bool {
for _, c := range checks {
if c == name {
return true
}
}
return false
}

func newGithubClient(cfg Config, httpClient *http.Client) (ghClient *github.Client, err error) {
baseURL, uploadURL := cfg.Github.BaseURL, cfg.Github.UploadURL

Expand Down

0 comments on commit fa80c10

Please sign in to comment.