Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GitHub Enterprise for GitHub Release Source #505

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 35 additions & 14 deletions internal/commands/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
"text/template"
"time"

"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/google/go-github/v40/github"
"github.com/google/go-github/v50/github"
"github.com/pivotal-cf/jhanda"

"github.com/pivotal-cf/kiln/internal/baking"
"github.com/pivotal-cf/kiln/internal/gh"
"github.com/pivotal-cf/kiln/pkg/notes"
)
Expand All @@ -27,12 +29,15 @@ const releaseDateFormat = "2006-01-02"

type ReleaseNotes struct {
Options 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"`
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"`
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"`
VariableFiles []string `long:"variables-file" short:"vf" description:"path to a file containing variables to interpolate"`
Variables []string `long:"variable" short:"vr" description:"key value pairs of variables to interpolate"`
notes.IssuesQuery
notes.TrainstatQuery
}
Expand All @@ -42,19 +47,21 @@ type ReleaseNotes struct {
stat func(name string) (fs.FileInfo, error)
io.Writer

fetchNotesData FetchNotesData
fetchNotesData FetchNotesData
variablesService baking.TemplateVariablesService

repoOwner, repoName string
}

type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery notes.IssuesQuery, trainstatClient notes.TrainstatNotesFetcher) (notes.Data, error)
type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery notes.IssuesQuery, trainstatClient notes.TrainstatNotesFetcher, variables map[string]any) (notes.Data, error)

func NewReleaseNotesCommand() (ReleaseNotes, error) {
return ReleaseNotes{
fetchNotesData: notes.FetchData,
readFile: os.ReadFile,
Writer: os.Stdout,
stat: os.Stat,
variablesService: baking.NewTemplateVariablesService(osfs.New(".")),
fetchNotesData: notes.FetchData,
readFile: os.ReadFile,
Writer: os.Stdout,
stat: os.Stat,
}, nil
}

Expand All @@ -72,6 +79,16 @@ func (r ReleaseNotes) Execute(args []string) error {
return err
}

templateVariables, err := r.variablesService.FromPathsAndPairs(r.Options.VariableFiles, r.Options.Variables)
if err != nil {
return fmt.Errorf("failed to parse template variables: %s", err)
}
if varValue, ok := templateVariables["github_token"]; !ok && r.Options.GithubToken != "" {
templateVariables["github_token"] = r.Options.GithubToken
} else if ok && r.Options.GithubToken == "" {
r.Options.GithubToken = varValue.(string)
}

ctx := context.Background()

if err := r.initRepo(); err != nil {
Expand All @@ -85,7 +102,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 All @@ -97,6 +117,7 @@ func (r ReleaseNotes) Execute(args []string) error {
nonFlagArgs[0], nonFlagArgs[1],
r.Options.IssuesQuery,
&trainstatClient,
templateVariables,
)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/release_notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v40/github"
"github.com/google/go-github/v50/github"
"github.com/pivotal-cf/jhanda"

"github.com/pivotal-cf/kiln/pkg/cargo"
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
repoOwner: "bunch",
repoName: "banana",
readFile: readFileFunc,
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, tro, trn, kfp, ir, fr string, iq notes.IssuesQuery, _ notes.TrainstatNotesFetcher) (notes.Data, error) {
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, tro, trn, kfp, ir, fr string, iq notes.IssuesQuery, _ notes.TrainstatNotesFetcher, __ map[string]any) (notes.Data, error) {
ctx, repository, client = c, repo, ghc
tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision = tro, trn, kfp, ir, fr
issuesQuery = iq
Expand All @@ -94,7 +94,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
{BOSHReleaseTarballLock: cargo.BOSHReleaseTarballLock{Name: "lemon", Version: "1.1.0"}},
},
Bumps: cargo.BumpList{
{Name: "banana", FromVersion: "1.1.0", ToVersion: "1.2.0"},
{Name: "banana", From: cargo.BOSHReleaseTarballLock{Version: "1.1.0"}, To: cargo.BOSHReleaseTarballLock{Version: "1.2.0"}},
},
TrainstatNotes: []string{
"* **[Feature]** this is a feature.",
Expand Down
2 changes: 1 addition & 1 deletion internal/component/fakes/release_by_tag_getter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/component/fakes/releases_lister.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions internal/component/github_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v40/github"
"golang.org/x/oauth2"
"github.com/google/go-github/v50/github"

"github.com/pivotal-cf/kiln/internal/gh"
"github.com/pivotal-cf/kiln/pkg/cargo"
Expand All @@ -38,18 +37,16 @@ func NewGithubReleaseSource(c cargo.ReleaseSourceConfig) *GithubReleaseSource {
if c.Type != "" && c.Type != ReleaseSourceTypeGithub {
panic(panicMessageWrongReleaseSourceType)
}
if c.GithubToken == "" {
if c.GithubToken == "" { // TODO remove this
crhntr marked this conversation as resolved.
Show resolved Hide resolved
panic("no token passed for github release source")
}
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)

githubClient, err := c.GitHubClient(context.TODO())
if err != nil {
panic(err)
}
return &GithubReleaseSource{
ReleaseSourceConfig: c,
Token: c.GithubToken,
Expand Down
2 changes: 1 addition & 1 deletion internal/component/github_release_source_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"testing"

"github.com/google/go-github/v40/github"
"github.com/google/go-github/v50/github"

. "github.com/onsi/gomega"

Expand Down
2 changes: 1 addition & 1 deletion internal/component/github_release_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"testing"

"github.com/google/go-github/v40/github"
"github.com/google/go-github/v50/github"
. "github.com/onsi/gomega"

"github.com/pivotal-cf/kiln/internal/component"
Expand Down
10 changes: 7 additions & 3 deletions internal/gh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package gh
import (
"context"

"github.com/google/go-github/v40/github"
"github.com/google/go-github/v50/github"
"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")
})
}
Loading
Loading