Skip to content

Commit ac5546f

Browse files
feat: add github enterprise support
1 parent 13d6154 commit ac5546f

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

cmd/semantic-release/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func main() {
5151
vFile := flag.Bool("vf", false, "create a .version file")
5252
showVersion := flag.Bool("version", false, "outputs the semantic-release version")
5353
updateFile := flag.String("update", "", "updates the version of a certain file")
54+
gheHost := flag.String("ghe-host", os.Getenv("GITHUB_ENTERPRISE_HOST"), "github enterprise host")
5455
flag.Parse()
5556

5657
if *showVersion {
@@ -72,7 +73,7 @@ func main() {
7273
exitIfError(errors.New("slug missing"))
7374
}
7475

75-
repo, err := semrel.NewRepository(context.TODO(), *slug, *token)
76+
repo, err := semrel.NewRepository(context.TODO(), *gheHost, *slug, *token)
7677
exitIfError(err)
7778

7879
logger.Println("getting default branch...")

semrel.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Repository struct {
5656
Client *github.Client
5757
}
5858

59-
func NewRepository(ctx context.Context, slug, token string) (*Repository, error) {
59+
func NewRepository(ctx context.Context, gheHost, slug, token string) (*Repository, error) {
6060
if !strings.Contains(slug, "/") {
6161
return nil, errors.New("invalid slug")
6262
}
@@ -65,9 +65,17 @@ func NewRepository(ctx context.Context, slug, token string) (*Repository, error)
6565
repo.Owner = splited[0]
6666
repo.Repo = splited[1]
6767
repo.Ctx = ctx
68-
repo.Client = github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
69-
&oauth2.Token{AccessToken: token},
70-
)))
68+
oauthClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
69+
if gheHost != "" {
70+
gheUrl := fmt.Sprintf("https://%s/", gheHost)
71+
rClient, err := github.NewEnterpriseClient(gheUrl, gheUrl, oauthClient)
72+
if err != nil {
73+
return nil, err
74+
}
75+
repo.Client = rClient
76+
} else {
77+
repo.Client = github.NewClient(oauthClient)
78+
}
7179
return repo, nil
7280
}
7381

semrel_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import (
1414
)
1515

1616
func TestNewRepository(t *testing.T) {
17-
repo, err := NewRepository(context.TODO(), "", "")
17+
repo, err := NewRepository(context.TODO(), "", "", "")
1818
if repo != nil || err == nil {
1919
t.Fatal("invalid initialization")
2020
}
21-
repo, err = NewRepository(context.TODO(), "owner/test-repo", "token")
21+
repo, err = NewRepository(context.TODO(), "", "owner/test-repo", "token")
2222
if repo == nil || err != nil {
2323
t.Fatal("invalid initialization")
2424
}
25+
repo, err = NewRepository(context.TODO(), "github.enterprise", "owner/test-repo", "token")
26+
if repo.Client.BaseURL.Host != "github.enterprise" || err != nil {
27+
t.Fatal("invalid enterprise initialization")
28+
}
2529
}
2630

2731
func createCommit(sha, message string) *github.RepositoryCommit {
@@ -95,7 +99,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
9599
}
96100

97101
func getNewTestRepo(t *testing.T) (*Repository, *httptest.Server) {
98-
repo, err := NewRepository(context.TODO(), "owner/test-repo", "token")
102+
repo, err := NewRepository(context.TODO(), "", "owner/test-repo", "token")
99103
if err != nil {
100104
t.Fatal(err)
101105
return nil, nil

0 commit comments

Comments
 (0)