Skip to content

Commit

Permalink
feat: support GitHub enterprise for BOSH Releases
Browse files Browse the repository at this point in the history
story: TPCF-26493

Co-authored-by: Joe Eltgroth <jeltgroth@vmware.com>
  • Loading branch information
crhntr and joeeltgroth committed Aug 15, 2024
1 parent 7cd368b commit 173c1aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
5 changes: 4 additions & 1 deletion internal/acceptance/workflows/scenario/step_funcs_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", 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
Expand Down
6 changes: 5 additions & 1 deletion internal/commands/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions internal/component/github_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions internal/gh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
25 changes: 19 additions & 6 deletions internal/gh/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

0 comments on commit 173c1aa

Please sign in to comment.