Skip to content

Commit

Permalink
wip: ideas for refactoring to create github client per release
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Rohn committed Oct 16, 2024
1 parent 406c7e9 commit 6f3b66e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
6 changes: 4 additions & 2 deletions pkg/cargo/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ func ReleaseNotes(ctx context.Context, kf Kilnfile, list BumpList) (BumpList, er
}

func getGithubRepositoryClientForRelease(kf Kilnfile) func(ctx context.Context, _ Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
// todo: []repositoryReleaseLister
return func(ctx context.Context, kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
spec, err := kf.BOSHReleaseTarballSpecification(lock.Name)
if err != nil {
return nil, err
}

_, owner, _, err := gh.RepositoryHostOwnerAndNameFromPath(spec.GitHubRepository)
host, owner, _, err := gh.RepositoryHostOwnerAndNameFromPath(spec.GitHubRepository)
if err != nil {
return nil, err
}
Expand All @@ -207,7 +208,8 @@ func getGithubRepositoryClientForRelease(kf Kilnfile) func(ctx context.Context,
return nil, fmt.Errorf("release source with id %s not found", lock.RemoteSource)
}
source := kf.ReleaseSources[i]
client, err := source.GitHubClient(ctx)

client, err := source.GitHubClient(ctx, host)
if err != nil {
return nil, err
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/cargo/kilnfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/pivotal-cf/kiln/internal/gh"
"strings"

"github.com/google/go-github/v50/github"
"golang.org/x/oauth2"

"gopkg.in/yaml.v3"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -173,18 +172,20 @@ type ReleaseSourceConfig struct {
Password string `yaml:"password,omitempty"`
}

func (c ReleaseSourceConfig) GitHubClient(ctx context.Context) (*github.Client, error) {
func (c ReleaseSourceConfig) GitHubClient(ctx context.Context, githubHost string) (*github.Client, error) {
if c.GithubToken == "" {
return nil, errors.New("no token passed for github release source")
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.GithubToken})
tokenClient := oauth2.NewClient(ctx, tokenSource)
var githubClient *github.Client
if c.Endpoint != "" {
return github.NewEnterpriseClient(c.Endpoint, c.Endpoint, tokenClient)
}
githubClient = github.NewClient(tokenClient)
return githubClient, nil
//tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.GithubToken})
//tokenClient := oauth2.NewClient(ctx, tokenSource)
//var githubClient *github.Client
//gh.Client(ctx) todo use gh.Client not github.NewClient or github.NewEnterpriseClient
return gh.Client(ctx, host, c.GithubToken, c.GithubEnterpriseToken)

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

undefined: host

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

too many arguments in call to gh.Client

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

c.GithubEnterpriseToken undefined (type ReleaseSourceConfig has no field or method GithubEnterpriseToken)) (typecheck)

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

undefined: host

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

too many arguments in call to gh.Client

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

c.GithubEnterpriseToken undefined (type ReleaseSourceConfig has no field or method GithubEnterpriseToken)) (typecheck)

Check failure on line 183 in pkg/cargo/kilnfile.go

View workflow job for this annotation

GitHub Actions / test

undefined: host
//if strings.HasSuffix(githubHost, "broadcom.net") {
// return github.NewEnterpriseClient(c.Endpoint, c.Endpoint, tokenClient)
//}
//githubClient = github.NewClient(tokenClient)
//return githubClient, nil
}

// BOSHReleaseTarballLock represents an exact build of a bosh release
Expand Down
28 changes: 0 additions & 28 deletions pkg/notes/notes_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,6 @@ func FetchData(ctx context.Context, repo *git.Repository, client *github.Client,
return f.fetch(ctx)
}

// FetchDataWithoutRepo can be used to generate release notes from tile metadata
func FetchDataWithoutRepo(ctx context.Context, client *github.Client, tileRepoOwner, tileRepoName string, kilnfile cargo.Kilnfile, kilnfileLockInitial, kilnfileLockFinal cargo.KilnfileLock, issuesQuery IssuesQuery) (Data, error) {
r := fetchNotesData{
repoOwner: tileRepoOwner,
repoName: tileRepoName,
issuesQuery: issuesQuery,
issuesService: client.Issues,
}
data := Data{
Bumps: cargo.CalculateBumps(kilnfileLockFinal.Releases, kilnfileLockInitial.Releases),
Stemcell: kilnfileLockFinal.Stemcell,
}
var err error
data.Issues, data.Bumps, err = r.fetchIssuesAndReleaseNotes(ctx, kilnfile, kilnfile, data.Bumps, issuesQuery)
if err != nil {
return Data{}, err
}

for _, c := range kilnfileLockFinal.Releases {
data.Components = append(data.Components, BOSHReleaseData{
BOSHReleaseTarballLock: c,
Releases: data.Bumps.ForLock(c).Releases,
})
}

return data, nil
}

func newFetchNotesData(repo *git.Repository, tileRepoOwner string, tileRepoName string, kilnfilePath string, initialRevision string, finalRevision string, client *github.Client, issuesQuery IssuesQuery, trainstatClient TrainstatNotesFetcher, variables map[string]any) (fetchNotesData, error) {
if repo == nil {
return fetchNotesData{}, errors.New("git repository required to generate release notes")
Expand Down

0 comments on commit 6f3b66e

Please sign in to comment.