Skip to content

Commit

Permalink
Support github enterprise (#43)
Browse files Browse the repository at this point in the history
* Support github enterprise with NewEnterpriseClient
  • Loading branch information
LenaHoinkis authored Jun 17, 2020
1 parent c25bff8 commit 9b19ca4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/github_receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var (
authtokenFile = flagx.FileBytes{}
githubOrg = flag.String("org", "", "The github user or organization name where all repos are found.")
githubRepo = flag.String("repo", "", "The default repository for creating issues when alerts do not include a repo label.")
githubBaseURL = flag.String("enterprise.base-url", "", "The URL of your GitHub Enterprise with API suffix (for example '/api/v3/').")
githubUploadURL = flag.String("enterprise.upload-url", "", "The upload URL needs to be set if it differs from the Github Enterprise base URL.")
enableAutoClose = flag.Bool("enable-auto-close", false, "Once an alert stops firing, automatically close open issues.")
labelOnResolved = flag.String("label-on-resolved", "", "Once an alert stops firing, apply this label.")
enableInMemory = flag.Bool("enable-inmemory", false, "Perform all operations in memory, without using github API.")
Expand Down Expand Up @@ -124,9 +126,18 @@ func main() {
var client alerts.ReceiverClient
if *enableInMemory {
client = local.NewClient()
} else {
} else if *githubBaseURL == "" {
client = issues.NewClient(*githubOrg, token, *alertLabel)
} else {
var err error
client, err = issues.NewEnterpriseClient(*githubBaseURL, *githubUploadURL, *githubOrg, token, *alertLabel)
if err != nil {
fmt.Print(err)
osExit(1)
return
}
}

promSrv := prometheusx.MustServeMetrics()
defer promSrv.Close()

Expand Down
9 changes: 9 additions & 0 deletions cmd/github_receiver/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Test_main(t *testing.T) {
authfile string
authtoken string
repo string
baseURL string
titleTmpl string
inmemory bool
expectStatus int
Expand Down Expand Up @@ -48,6 +49,13 @@ func Test_main(t *testing.T) {
titleTmpl: "{{ x }}",
expectStatus: 1,
},
{
name: "bad-baseURL",
repo: "fake-repo",
authtoken: "token",
baseURL: "invalidURLEscape%zz",
expectStatus: 1,
},
}
flag.CommandLine.SetOutput(ioutil.Discard)
for _, tt := range tests {
Expand All @@ -60,6 +68,7 @@ func Test_main(t *testing.T) {
authtokenFile = []byte(tt.authfile)
*githubOrg = "fake-org"
*githubRepo = tt.repo
*githubBaseURL = tt.baseURL
*enableInMemory = tt.inmemory
// Guarantee no port conflicts between tests of main.
*prometheusx.ListenAddress = ":0"
Expand Down
24 changes: 24 additions & 0 deletions issues/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func NewClient(org, authToken, alertLabel string) *Client {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: authToken},
)

client := &Client{
GithubClient: github.NewClient(oauth2.NewClient(ctx, tokenSource)),
org: org,
Expand All @@ -98,6 +99,29 @@ func NewClient(org, authToken, alertLabel string) *Client {
return client
}

// NewEnterpriseClient creates an Enterprise Client authenticated using the Github authToken.
// Future operations are only performed on the given github enterprise "org/repo".
// If uploadURL is empty it will be set to baseURL
func NewEnterpriseClient(baseURL, uploadURL, org, authToken, alertLabel string) (*Client, error) {
ctx := context.Background()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: authToken},
)

if uploadURL == "" {
uploadURL = baseURL
}

githubClient, err := github.NewEnterpriseClient(baseURL, uploadURL, oauth2.NewClient(ctx, tokenSource))

client := &Client{
GithubClient: githubClient,
org: org,
alertLabel: alertLabel,
}
return client, err
}

// CreateIssue creates a new Github issue. New issues are unassigned. Issues are
// labeled with with an alert named alertLabel. Labels are created automatically
// if they do not already exist in a repo.
Expand Down
15 changes: 15 additions & 0 deletions issues/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,18 @@ func TestClient_rateLimit(t *testing.T) {
return
}
}

func TestClient_newEnterpriseClient(t *testing.T) {
baseURL := "https://github.example.com/"
c, _ := issues.NewEnterpriseClient(baseURL, "", "fake-org", "FAKE-AUTH-TOKEN", "alert")

if c.GithubClient.BaseURL.String() != baseURL {
t.Errorf("client baseURL got %q but want %q", c.GithubClient.BaseURL.String(), baseURL)
return
}

if c.GithubClient.UploadURL.String() != baseURL {
t.Errorf("client uploadURL got %q but want %q", c.GithubClient.UploadURL.String(), baseURL)
return
}
}

0 comments on commit 9b19ca4

Please sign in to comment.