Skip to content

Commit

Permalink
Merge pull request #162 from fluxcd/feature/github-enterprise
Browse files Browse the repository at this point in the history
Add support for GitHub enterprise commit status
  • Loading branch information
stefanprodan authored Mar 21, 2021
2 parents 763c291 + f030be3 commit e0e4204
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/notifier/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"time"

Expand All @@ -39,7 +40,12 @@ func NewGitHub(addr string, token string) (*GitHub, error) {
return nil, errors.New("github token cannot be empty")
}

_, id, err := parseGitAddress(addr)
host, id, err := parseGitAddress(addr)
if err != nil {
return nil, err
}

baseUrl, err := url.Parse(host)
if err != nil {
return nil, err
}
Expand All @@ -49,10 +55,15 @@ func NewGitHub(addr string, token string) (*GitHub, error) {
return nil, fmt.Errorf("invalid repository id %q", id)
}

ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
if baseUrl.Host != "github.com" {
client, err = github.NewEnterpriseClient(host, host, tc)
if err != nil {
return nil, fmt.Errorf("could not create enterprise GitHub client: %v", err)
}
}

return &GitHub{
Owner: comp[0],
Expand Down
9 changes: 9 additions & 0 deletions internal/notifier/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func TestNewGitHubBasic(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, g.Owner, "foo")
assert.Equal(t, g.Repo, "bar")
assert.Equal(t, g.Client.BaseURL.Host, "api.github.com")
}

func TestNewEmterpriseGitHubBasic(t *testing.T) {
g, err := NewGitHub("https://foobar.com/foo/bar", "foobar")
assert.Nil(t, err)
assert.Equal(t, g.Owner, "foo")
assert.Equal(t, g.Repo, "bar")
assert.Equal(t, g.Client.BaseURL.Host, "foobar.com")
}

func TestNewGitHubInvalidUrl(t *testing.T) {
Expand Down

0 comments on commit e0e4204

Please sign in to comment.