From 4a956296c80adae0995c411f1cb05d513a2de5dd Mon Sep 17 00:00:00 2001 From: Christopher Hunter Date: Thu, 15 Aug 2024 12:25:15 -0700 Subject: [PATCH] feat: support GitHub enterprise for BOSH Releases story: TPCF-26493 Co-authored-by: Joe Eltgroth --- .../workflows/scenario/step_funcs_github.go | 5 +++- internal/commands/release_notes.go | 6 ++++- internal/component/github_release_source.go | 12 +++++++-- internal/gh/client.go | 8 ++++-- internal/gh/client_test.go | 25 ++++++++++++++----- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/internal/acceptance/workflows/scenario/step_funcs_github.go b/internal/acceptance/workflows/scenario/step_funcs_github.go index 8f048a3d2..744fdc2d5 100644 --- a/internal/acceptance/workflows/scenario/step_funcs_github.go +++ b/internal/acceptance/workflows/scenario/step_funcs_github.go @@ -13,7 +13,10 @@ func githubRepoHasReleaseWithTag(ctx context.Context, repoOrg, repoName, tag str if err != nil { return err } - ghAPI := gh.Client(ctx, accessToken) + ghAPI, err := gh.Client(ctx, "https://github.com", accessToken) + if err != nil { + return fmt.Errorf("failed to setup github client: %w", err) + } _, response, err := ghAPI.Repositories.GetReleaseByTag(ctx, repoOrg, repoName, tag) if err != nil { return err diff --git a/internal/commands/release_notes.go b/internal/commands/release_notes.go index dc732deb0..8a1d703e9 100644 --- a/internal/commands/release_notes.go +++ b/internal/commands/release_notes.go @@ -30,6 +30,7 @@ type ReleaseNotes struct { ReleaseDate string `long:"release-date" short:"d" description:"release date of the tile"` TemplateName string `long:"template" short:"t" description:"path to template"` GithubToken string `long:"github-token" short:"g" description:"auth token for fetching issues merged between releases" env:"GITHUB_TOKEN"` + GithubHost string `long:"github-host" description:"set this when you are using GitHub enterprise" env:"GITHUB_HOST"` Kilnfile string `long:"kilnfile" short:"k" description:"path to Kilnfile"` DocsFile string `long:"update-docs" short:"u" description:"path to docs file to update"` Window string `long:"window" short:"w" description:"GA window for release notes" default:"ga"` @@ -85,7 +86,10 @@ func (r ReleaseNotes) Execute(args []string) error { var client *github.Client if r.Options.GithubToken != "" { - client = gh.Client(ctx, r.Options.GithubToken) + client, err = gh.Client(ctx, r.Options.GithubHost, r.Options.GithubToken) + if err != nil { + return fmt.Errorf("failed to setup github client: %w", err) + } } trainstatClient := notes.NewTrainstatClient(r.Options.TrainstatQuery.TrainstatURL) diff --git a/internal/component/github_release_source.go b/internal/component/github_release_source.go index 8fba53e16..999d5a20c 100644 --- a/internal/component/github_release_source.go +++ b/internal/component/github_release_source.go @@ -44,11 +44,19 @@ func NewGithubReleaseSource(c cargo.ReleaseSourceConfig) *GithubReleaseSource { if c.Org == "" { panic("no github org passed for github release source") } - ctx := context.TODO() tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.GithubToken}) tokenClient := oauth2.NewClient(ctx, tokenSource) - githubClient := github.NewClient(tokenClient) + var githubClient *github.Client + if c.Endpoint != "" { + var err error + githubClient, err = github.NewEnterpriseClient(c.Endpoint, c.Endpoint, tokenClient) + if err != nil { + panic(err) + } + } else { + githubClient = github.NewClient(tokenClient) + } return &GithubReleaseSource{ ReleaseSourceConfig: c, diff --git a/internal/gh/client.go b/internal/gh/client.go index 861009bed..5263c525b 100644 --- a/internal/gh/client.go +++ b/internal/gh/client.go @@ -7,6 +7,10 @@ import ( "golang.org/x/oauth2" ) -func Client(ctx context.Context, accessToken string) *github.Client { - return github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}))) +func Client(ctx context.Context, host, accessToken string) (*github.Client, error) { + client := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})) + if host == "" { + return github.NewClient(client), nil + } + return github.NewEnterpriseClient(host, host, client) } diff --git a/internal/gh/client_test.go b/internal/gh/client_test.go index c86606b48..bcb97fab3 100644 --- a/internal/gh/client_test.go +++ b/internal/gh/client_test.go @@ -4,14 +4,27 @@ import ( "context" "testing" - "github.com/stretchr/testify/require" - "github.com/pivotal-cf/kiln/internal/gh" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestClient(t *testing.T) { - ctx := context.Background() - token := "xxx" - ghClient := gh.Client(ctx, token) - require.NotNil(t, ghClient.Client()) + t.Run("when the host is empty", func(t *testing.T) { + ctx := context.Background() + token := "xxx" + ghClient, err := gh.Client(ctx, "", token) + require.NoError(t, err) + require.NotNil(t, ghClient.Client()) + assert.Contains(t, ghClient.BaseURL.String(), "https://api.github.com") + }) + + t.Run("when the host is not empty", func(t *testing.T) { + ctx := context.Background() + token := "xxx" + ghClient, err := gh.Client(ctx, "https://example.com", token) + require.NoError(t, err) + require.NotNil(t, ghClient.Client()) + assert.Contains(t, ghClient.BaseURL.String(), "https://example.com") + }) }