Просмотр исходного кода

Merge pull request #303 from petegallagher/host-base-url

Adding baseurl flag to allow self-hosted GitLab/GitHub servers
Zachary Rice 6 лет назад
Родитель
Сommit
454acebe5a
4 измененных файлов с 28 добавлено и 8 удалено
  1. 13 3
      hosts/github.go
  2. 10 2
      hosts/gitlab.go
  3. 4 3
      hosts/host.go
  4. 1 0
      options/options.go

+ 13 - 3
hosts/github.go

@@ -27,16 +27,26 @@ type Github struct {
 
 // NewGithubClient accepts a manager struct and returns a Github host pointer which will be used to
 // perform a github audit on an organization, user, or PR.
-func NewGithubClient(m manager.Manager) *Github {
+func NewGithubClient(m manager.Manager) (*Github, error) {
+	var err error
 	ctx := context.Background()
 	token := oauth2.StaticTokenSource(
 		&oauth2.Token{AccessToken: options.GetAccessToken(m.Opts)},
 	)
 
+	var githubClient *github.Client
+	httpClient := oauth2.NewClient(ctx, token)
+
+	if m.Opts.BaseURL == "" {
+		githubClient = github.NewClient(httpClient)
+	} else {
+		githubClient, err = github.NewEnterpriseClient(m.Opts.BaseURL, m.Opts.BaseURL, httpClient)
+	}
+
 	return &Github{
 		manager: m,
-		client:  github.NewClient(oauth2.NewClient(ctx, token)),
-	}
+		client:  githubClient,
+	}, err
 }
 
 // Audit will audit a github user or organization's repos.

+ 10 - 2
hosts/gitlab.go

@@ -22,12 +22,20 @@ type Gitlab struct {
 
 // NewGitlabClient accepts a manager struct and returns a Gitlab host pointer which will be used to
 // perform a gitlab audit on an group or user.
-func NewGitlabClient(m manager.Manager) *Gitlab {
-	return &Gitlab{
+func NewGitlabClient(m manager.Manager) (*Gitlab, error) {
+	var err error
+
+	gitlabClient := &Gitlab{
 		manager: m,
 		ctx:     context.Background(),
 		client:  gitlab.NewClient(nil, options.GetAccessToken(m.Opts)),
 	}
+
+	if m.Opts.BaseURL != "" {
+		err = gitlabClient.client.SetBaseURL(m.Opts.BaseURL)
+	}
+
+	return gitlabClient, err
 }
 
 // Audit will audit a github user or organization's repos.

+ 4 - 3
hosts/host.go

@@ -20,11 +20,12 @@ type Host interface {
 // Run kicks off a host audit. This function accepts a manager and determines what host it should audit
 func Run(m *manager.Manager) error {
 	var host Host
+	var err error
 	switch getHost(m.Opts.Host) {
 	case _github:
-		host = NewGithubClient(*m)
+		host, err = NewGithubClient(*m)
 	case _gitlab:
-		host = NewGitlabClient(*m)
+		host, err = NewGitlabClient(*m)
 	default:
 		return nil
 	}
@@ -34,7 +35,7 @@ func Run(m *manager.Manager) error {
 	} else {
 		host.Audit()
 	}
-	return nil
+	return err
 }
 
 func getHost(host string) int {

+ 1 - 0
options/options.go

@@ -52,6 +52,7 @@ type Options struct {
 
 	// Hosts
 	Host         string `long:"host" description:"git hosting service like gitlab or github. Supported hosts include: Github, Gitlab"`
+	BaseURL      string `long:"baseurl" description:"Base URL for API requests. Defaults to the public GitLab or GitHub API, but can be set to a domain endpoint to use with a self hosted server."`
 	Organization string `long:"org" description:"organization to audit"`
 	User         string `long:"user" description:"user to audit"` //work
 	PullRequest  string `long:"pr" description:"pull/merge request url"`