diff --git a/cmd/krel/cmd/changelog.go b/cmd/krel/cmd/changelog.go index d0cdbada18f..aec3de52ef7 100644 --- a/cmd/krel/cmd/changelog.go +++ b/cmd/krel/cmd/changelog.go @@ -246,7 +246,11 @@ func generateReleaseNotes(opts *changelogOptions, branch, startRev, endRev strin return "", err } - gatherer := notes.NewGatherer(context.Background(), notesOptions) + gatherer, err := notes.NewGatherer(context.Background(), notesOptions) + if err != nil { + return "", errors.Wrapf(err, "retrieving notes gatherer") + } + releaseNotes, history, err := gatherer.ListReleaseNotes() if err != nil { return "", errors.Wrapf(err, "listing release notes") diff --git a/cmd/krel/cmd/release_notes.go b/cmd/krel/cmd/release_notes.go index 2d8dd08e372..8220d3dda72 100644 --- a/cmd/krel/cmd/release_notes.go +++ b/cmd/krel/cmd/release_notes.go @@ -346,7 +346,10 @@ func releaseNotesFrom(startTag string) (*releaseNotesResult, error) { } // Fetch the notes - gatherer := notes.NewGatherer(context.Background(), notesOptions) + gatherer, err := notes.NewGatherer(context.Background(), notesOptions) + if err != nil { + return nil, errors.Wrapf(err, "retrieving notes gatherer") + } releaseNotes, history, err := gatherer.ListReleaseNotes() if err != nil { return nil, errors.Wrapf(err, "listing release notes") diff --git a/cmd/release-notes/main.go b/cmd/release-notes/main.go index f3999a8180d..91e6130032e 100644 --- a/cmd/release-notes/main.go +++ b/cmd/release-notes/main.go @@ -215,7 +215,10 @@ func init() { func GetReleaseNotes() (notes.ReleaseNotes, notes.ReleaseNotesHistory, error) { logrus.Info("fetching all commits. This might take a while...") - gatherer := notes.NewGatherer(context.Background(), opts) + gatherer, err := notes.NewGatherer(context.Background(), opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "retrieving notes gatherer") + } releaseNotes, history, err := gatherer.ListReleaseNotes() if err != nil { return nil, nil, errors.Wrapf(err, "listing release notes") diff --git a/pkg/github/BUILD.bazel b/pkg/github/BUILD.bazel index f83160a1b62..5dadc7b367d 100644 --- a/pkg/github/BUILD.bazel +++ b/pkg/github/BUILD.bazel @@ -2,11 +2,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", - srcs = ["github.go"], + srcs = [ + "github.go", + "record.go", + "replay.go", + ], importpath = "k8s.io/release/pkg/github", visibility = ["//visibility:public"], deps = [ "//pkg/git:go_default_library", + "//pkg/github/internal:go_default_library", "//pkg/util:go_default_library", "@com_github_google_go_github_v29//github:go_default_library", "@com_github_pkg_errors//:go_default_library", @@ -39,6 +44,7 @@ filegroup( srcs = [ ":package-srcs", "//pkg/github/githubfakes:all-srcs", + "//pkg/github/internal:all-srcs", ], tags = ["automanaged"], visibility = ["//visibility:public"], diff --git a/pkg/github/github.go b/pkg/github/github.go index 5251292268d..b9e136e10d9 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "net/http" + "os" "strings" "github.com/google/go-github/v29/github" @@ -28,6 +29,7 @@ import ( "golang.org/x/oauth2" "k8s.io/release/pkg/git" + "k8s.io/release/pkg/github/internal" "k8s.io/release/pkg/util" ) @@ -48,13 +50,33 @@ type githubClient struct { //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate //counterfeiter:generate . Client type Client interface { - ListTags( - context.Context, string, string, *github.ListOptions, - ) ([]*github.RepositoryTag, *github.Response, error) + GetCommit( + context.Context, string, string, string, + ) (*github.Commit, *github.Response, error) + + GetPullRequest( + context.Context, string, string, int, + ) (*github.PullRequest, *github.Response, error) + + GetRepoCommit( + context.Context, string, string, string, + ) (*github.RepositoryCommit, *github.Response, error) + + ListCommits( + context.Context, string, string, *github.CommitsListOptions, + ) ([]*github.RepositoryCommit, *github.Response, error) + + ListPullRequestsWithCommit( + context.Context, string, string, string, *github.PullRequestListOptions, + ) ([]*github.PullRequest, *github.Response, error) ListReleases( context.Context, string, string, *github.ListOptions, ) ([]*github.RepositoryRelease, *github.Response, error) + + ListTags( + context.Context, string, string, *github.ListOptions, + ) ([]*github.RepositoryTag, *github.Response, error) } // New creates a new default GitHub client. Tokens set via the $GITHUB_TOKEN @@ -76,16 +98,95 @@ func New() *GitHub { return &GitHub{&githubClient{github.NewClient(client)}} } -func (g *githubClient) ListTags( - ctx context.Context, owner, repo string, opt *github.ListOptions, -) ([]*github.RepositoryTag, *github.Response, error) { - return g.Client.Repositories.ListTags(ctx, owner, repo, opt) +// NewWithToken can be used to specify a GITHUB_TOKEN before retrieving the +// client to enforce authenticated GitHub requests +func NewWithToken(token string) (*GitHub, error) { + if err := os.Setenv(TokenEnvKey, token); err != nil { + return nil, errors.Wrapf(err, "unable to export %s", TokenEnvKey) + } + return New(), nil +} + +func (g *githubClient) GetCommit( + ctx context.Context, owner, repo, sha string, +) (*github.Commit, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + commit, resp, err := g.Git.GetCommit(ctx, owner, repo, sha) + if !shouldRetry(err) { + return commit, resp, err + } + } +} + +func (g *githubClient) GetPullRequest( + ctx context.Context, owner, repo string, number int, +) (*github.PullRequest, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + pr, resp, err := g.PullRequests.Get(ctx, owner, repo, number) + if !shouldRetry(err) { + return pr, resp, err + } + } +} + +func (g *githubClient) GetRepoCommit( + ctx context.Context, owner, repo, sha string, +) (*github.RepositoryCommit, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + commit, resp, err := g.Repositories.GetCommit(ctx, owner, repo, sha) + if !shouldRetry(err) { + return commit, resp, err + } + } +} + +func (g *githubClient) ListCommits( + ctx context.Context, owner, repo string, opt *github.CommitsListOptions, +) ([]*github.RepositoryCommit, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + commits, resp, err := g.Repositories.ListCommits(ctx, owner, repo, opt) + if !shouldRetry(err) { + return commits, resp, err + } + } +} + +func (g *githubClient) ListPullRequestsWithCommit( + ctx context.Context, owner, repo, sha string, + opt *github.PullRequestListOptions, +) ([]*github.PullRequest, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + prs, resp, err := g.PullRequests.ListPullRequestsWithCommit( + ctx, owner, repo, sha, opt, + ) + if !shouldRetry(err) { + return prs, resp, err + } + } } func (g *githubClient) ListReleases( ctx context.Context, owner, repo string, opt *github.ListOptions, ) ([]*github.RepositoryRelease, *github.Response, error) { - return g.Client.Repositories.ListReleases(ctx, owner, repo, opt) + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + releases, resp, err := g.Repositories.ListReleases( + ctx, owner, repo, opt, + ) + if !shouldRetry(err) { + return releases, resp, err + } + } +} + +func (g *githubClient) ListTags( + ctx context.Context, owner, repo string, opt *github.ListOptions, +) ([]*github.RepositoryTag, *github.Response, error) { + for shouldRetry := internal.DefaultGithubErrChecker(); ; { + tags, resp, err := g.Repositories.ListTags(ctx, owner, repo, opt) + if !shouldRetry(err) { + return tags, resp, err + } + } } // SetClient can be used to manually set the internal GitHub client @@ -93,6 +194,11 @@ func (g *GitHub) SetClient(client Client) { g.client = client } +// Client can be used to retrieve the Client type +func (g *GitHub) Client() Client { + return g.client +} + // TagsPerBranch is an abstraction over a simple branch to latest tag association type TagsPerBranch map[string]string diff --git a/pkg/github/githubfakes/fake_client.go b/pkg/github/githubfakes/fake_client.go index 0271787ac9d..a675539ac2e 100644 --- a/pkg/github/githubfakes/fake_client.go +++ b/pkg/github/githubfakes/fake_client.go @@ -26,6 +26,97 @@ import ( ) type FakeClient struct { + GetCommitStub func(context.Context, string, string, string) (*githuba.Commit, *githuba.Response, error) + getCommitMutex sync.RWMutex + getCommitArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + } + getCommitReturns struct { + result1 *githuba.Commit + result2 *githuba.Response + result3 error + } + getCommitReturnsOnCall map[int]struct { + result1 *githuba.Commit + result2 *githuba.Response + result3 error + } + GetPullRequestStub func(context.Context, string, string, int) (*githuba.PullRequest, *githuba.Response, error) + getPullRequestMutex sync.RWMutex + getPullRequestArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 int + } + getPullRequestReturns struct { + result1 *githuba.PullRequest + result2 *githuba.Response + result3 error + } + getPullRequestReturnsOnCall map[int]struct { + result1 *githuba.PullRequest + result2 *githuba.Response + result3 error + } + GetRepoCommitStub func(context.Context, string, string, string) (*githuba.RepositoryCommit, *githuba.Response, error) + getRepoCommitMutex sync.RWMutex + getRepoCommitArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + } + getRepoCommitReturns struct { + result1 *githuba.RepositoryCommit + result2 *githuba.Response + result3 error + } + getRepoCommitReturnsOnCall map[int]struct { + result1 *githuba.RepositoryCommit + result2 *githuba.Response + result3 error + } + ListCommitsStub func(context.Context, string, string, *githuba.CommitsListOptions) ([]*githuba.RepositoryCommit, *githuba.Response, error) + listCommitsMutex sync.RWMutex + listCommitsArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 *githuba.CommitsListOptions + } + listCommitsReturns struct { + result1 []*githuba.RepositoryCommit + result2 *githuba.Response + result3 error + } + listCommitsReturnsOnCall map[int]struct { + result1 []*githuba.RepositoryCommit + result2 *githuba.Response + result3 error + } + ListPullRequestsWithCommitStub func(context.Context, string, string, string, *githuba.PullRequestListOptions) ([]*githuba.PullRequest, *githuba.Response, error) + listPullRequestsWithCommitMutex sync.RWMutex + listPullRequestsWithCommitArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + arg5 *githuba.PullRequestListOptions + } + listPullRequestsWithCommitReturns struct { + result1 []*githuba.PullRequest + result2 *githuba.Response + result3 error + } + listPullRequestsWithCommitReturnsOnCall map[int]struct { + result1 []*githuba.PullRequest + result2 *githuba.Response + result3 error + } ListReleasesStub func(context.Context, string, string, *githuba.ListOptions) ([]*githuba.RepositoryRelease, *githuba.Response, error) listReleasesMutex sync.RWMutex listReleasesArgsForCall []struct { @@ -66,6 +157,352 @@ type FakeClient struct { invocationsMutex sync.RWMutex } +func (fake *FakeClient) GetCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string) (*githuba.Commit, *githuba.Response, error) { + fake.getCommitMutex.Lock() + ret, specificReturn := fake.getCommitReturnsOnCall[len(fake.getCommitArgsForCall)] + fake.getCommitArgsForCall = append(fake.getCommitArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("GetCommit", []interface{}{arg1, arg2, arg3, arg4}) + fake.getCommitMutex.Unlock() + if fake.GetCommitStub != nil { + return fake.GetCommitStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2, ret.result3 + } + fakeReturns := fake.getCommitReturns + return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 +} + +func (fake *FakeClient) GetCommitCallCount() int { + fake.getCommitMutex.RLock() + defer fake.getCommitMutex.RUnlock() + return len(fake.getCommitArgsForCall) +} + +func (fake *FakeClient) GetCommitCalls(stub func(context.Context, string, string, string) (*githuba.Commit, *githuba.Response, error)) { + fake.getCommitMutex.Lock() + defer fake.getCommitMutex.Unlock() + fake.GetCommitStub = stub +} + +func (fake *FakeClient) GetCommitArgsForCall(i int) (context.Context, string, string, string) { + fake.getCommitMutex.RLock() + defer fake.getCommitMutex.RUnlock() + argsForCall := fake.getCommitArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *FakeClient) GetCommitReturns(result1 *githuba.Commit, result2 *githuba.Response, result3 error) { + fake.getCommitMutex.Lock() + defer fake.getCommitMutex.Unlock() + fake.GetCommitStub = nil + fake.getCommitReturns = struct { + result1 *githuba.Commit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) GetCommitReturnsOnCall(i int, result1 *githuba.Commit, result2 *githuba.Response, result3 error) { + fake.getCommitMutex.Lock() + defer fake.getCommitMutex.Unlock() + fake.GetCommitStub = nil + if fake.getCommitReturnsOnCall == nil { + fake.getCommitReturnsOnCall = make(map[int]struct { + result1 *githuba.Commit + result2 *githuba.Response + result3 error + }) + } + fake.getCommitReturnsOnCall[i] = struct { + result1 *githuba.Commit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) GetPullRequest(arg1 context.Context, arg2 string, arg3 string, arg4 int) (*githuba.PullRequest, *githuba.Response, error) { + fake.getPullRequestMutex.Lock() + ret, specificReturn := fake.getPullRequestReturnsOnCall[len(fake.getPullRequestArgsForCall)] + fake.getPullRequestArgsForCall = append(fake.getPullRequestArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 int + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("GetPullRequest", []interface{}{arg1, arg2, arg3, arg4}) + fake.getPullRequestMutex.Unlock() + if fake.GetPullRequestStub != nil { + return fake.GetPullRequestStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2, ret.result3 + } + fakeReturns := fake.getPullRequestReturns + return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 +} + +func (fake *FakeClient) GetPullRequestCallCount() int { + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + return len(fake.getPullRequestArgsForCall) +} + +func (fake *FakeClient) GetPullRequestCalls(stub func(context.Context, string, string, int) (*githuba.PullRequest, *githuba.Response, error)) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = stub +} + +func (fake *FakeClient) GetPullRequestArgsForCall(i int) (context.Context, string, string, int) { + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + argsForCall := fake.getPullRequestArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *FakeClient) GetPullRequestReturns(result1 *githuba.PullRequest, result2 *githuba.Response, result3 error) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = nil + fake.getPullRequestReturns = struct { + result1 *githuba.PullRequest + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) GetPullRequestReturnsOnCall(i int, result1 *githuba.PullRequest, result2 *githuba.Response, result3 error) { + fake.getPullRequestMutex.Lock() + defer fake.getPullRequestMutex.Unlock() + fake.GetPullRequestStub = nil + if fake.getPullRequestReturnsOnCall == nil { + fake.getPullRequestReturnsOnCall = make(map[int]struct { + result1 *githuba.PullRequest + result2 *githuba.Response + result3 error + }) + } + fake.getPullRequestReturnsOnCall[i] = struct { + result1 *githuba.PullRequest + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) GetRepoCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string) (*githuba.RepositoryCommit, *githuba.Response, error) { + fake.getRepoCommitMutex.Lock() + ret, specificReturn := fake.getRepoCommitReturnsOnCall[len(fake.getRepoCommitArgsForCall)] + fake.getRepoCommitArgsForCall = append(fake.getRepoCommitArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("GetRepoCommit", []interface{}{arg1, arg2, arg3, arg4}) + fake.getRepoCommitMutex.Unlock() + if fake.GetRepoCommitStub != nil { + return fake.GetRepoCommitStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2, ret.result3 + } + fakeReturns := fake.getRepoCommitReturns + return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 +} + +func (fake *FakeClient) GetRepoCommitCallCount() int { + fake.getRepoCommitMutex.RLock() + defer fake.getRepoCommitMutex.RUnlock() + return len(fake.getRepoCommitArgsForCall) +} + +func (fake *FakeClient) GetRepoCommitCalls(stub func(context.Context, string, string, string) (*githuba.RepositoryCommit, *githuba.Response, error)) { + fake.getRepoCommitMutex.Lock() + defer fake.getRepoCommitMutex.Unlock() + fake.GetRepoCommitStub = stub +} + +func (fake *FakeClient) GetRepoCommitArgsForCall(i int) (context.Context, string, string, string) { + fake.getRepoCommitMutex.RLock() + defer fake.getRepoCommitMutex.RUnlock() + argsForCall := fake.getRepoCommitArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *FakeClient) GetRepoCommitReturns(result1 *githuba.RepositoryCommit, result2 *githuba.Response, result3 error) { + fake.getRepoCommitMutex.Lock() + defer fake.getRepoCommitMutex.Unlock() + fake.GetRepoCommitStub = nil + fake.getRepoCommitReturns = struct { + result1 *githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) GetRepoCommitReturnsOnCall(i int, result1 *githuba.RepositoryCommit, result2 *githuba.Response, result3 error) { + fake.getRepoCommitMutex.Lock() + defer fake.getRepoCommitMutex.Unlock() + fake.GetRepoCommitStub = nil + if fake.getRepoCommitReturnsOnCall == nil { + fake.getRepoCommitReturnsOnCall = make(map[int]struct { + result1 *githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }) + } + fake.getRepoCommitReturnsOnCall[i] = struct { + result1 *githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) ListCommits(arg1 context.Context, arg2 string, arg3 string, arg4 *githuba.CommitsListOptions) ([]*githuba.RepositoryCommit, *githuba.Response, error) { + fake.listCommitsMutex.Lock() + ret, specificReturn := fake.listCommitsReturnsOnCall[len(fake.listCommitsArgsForCall)] + fake.listCommitsArgsForCall = append(fake.listCommitsArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 *githuba.CommitsListOptions + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("ListCommits", []interface{}{arg1, arg2, arg3, arg4}) + fake.listCommitsMutex.Unlock() + if fake.ListCommitsStub != nil { + return fake.ListCommitsStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2, ret.result3 + } + fakeReturns := fake.listCommitsReturns + return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 +} + +func (fake *FakeClient) ListCommitsCallCount() int { + fake.listCommitsMutex.RLock() + defer fake.listCommitsMutex.RUnlock() + return len(fake.listCommitsArgsForCall) +} + +func (fake *FakeClient) ListCommitsCalls(stub func(context.Context, string, string, *githuba.CommitsListOptions) ([]*githuba.RepositoryCommit, *githuba.Response, error)) { + fake.listCommitsMutex.Lock() + defer fake.listCommitsMutex.Unlock() + fake.ListCommitsStub = stub +} + +func (fake *FakeClient) ListCommitsArgsForCall(i int) (context.Context, string, string, *githuba.CommitsListOptions) { + fake.listCommitsMutex.RLock() + defer fake.listCommitsMutex.RUnlock() + argsForCall := fake.listCommitsArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *FakeClient) ListCommitsReturns(result1 []*githuba.RepositoryCommit, result2 *githuba.Response, result3 error) { + fake.listCommitsMutex.Lock() + defer fake.listCommitsMutex.Unlock() + fake.ListCommitsStub = nil + fake.listCommitsReturns = struct { + result1 []*githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) ListCommitsReturnsOnCall(i int, result1 []*githuba.RepositoryCommit, result2 *githuba.Response, result3 error) { + fake.listCommitsMutex.Lock() + defer fake.listCommitsMutex.Unlock() + fake.ListCommitsStub = nil + if fake.listCommitsReturnsOnCall == nil { + fake.listCommitsReturnsOnCall = make(map[int]struct { + result1 []*githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }) + } + fake.listCommitsReturnsOnCall[i] = struct { + result1 []*githuba.RepositoryCommit + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) ListPullRequestsWithCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string, arg5 *githuba.PullRequestListOptions) ([]*githuba.PullRequest, *githuba.Response, error) { + fake.listPullRequestsWithCommitMutex.Lock() + ret, specificReturn := fake.listPullRequestsWithCommitReturnsOnCall[len(fake.listPullRequestsWithCommitArgsForCall)] + fake.listPullRequestsWithCommitArgsForCall = append(fake.listPullRequestsWithCommitArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 string + arg5 *githuba.PullRequestListOptions + }{arg1, arg2, arg3, arg4, arg5}) + fake.recordInvocation("ListPullRequestsWithCommit", []interface{}{arg1, arg2, arg3, arg4, arg5}) + fake.listPullRequestsWithCommitMutex.Unlock() + if fake.ListPullRequestsWithCommitStub != nil { + return fake.ListPullRequestsWithCommitStub(arg1, arg2, arg3, arg4, arg5) + } + if specificReturn { + return ret.result1, ret.result2, ret.result3 + } + fakeReturns := fake.listPullRequestsWithCommitReturns + return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 +} + +func (fake *FakeClient) ListPullRequestsWithCommitCallCount() int { + fake.listPullRequestsWithCommitMutex.RLock() + defer fake.listPullRequestsWithCommitMutex.RUnlock() + return len(fake.listPullRequestsWithCommitArgsForCall) +} + +func (fake *FakeClient) ListPullRequestsWithCommitCalls(stub func(context.Context, string, string, string, *githuba.PullRequestListOptions) ([]*githuba.PullRequest, *githuba.Response, error)) { + fake.listPullRequestsWithCommitMutex.Lock() + defer fake.listPullRequestsWithCommitMutex.Unlock() + fake.ListPullRequestsWithCommitStub = stub +} + +func (fake *FakeClient) ListPullRequestsWithCommitArgsForCall(i int) (context.Context, string, string, string, *githuba.PullRequestListOptions) { + fake.listPullRequestsWithCommitMutex.RLock() + defer fake.listPullRequestsWithCommitMutex.RUnlock() + argsForCall := fake.listPullRequestsWithCommitArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 +} + +func (fake *FakeClient) ListPullRequestsWithCommitReturns(result1 []*githuba.PullRequest, result2 *githuba.Response, result3 error) { + fake.listPullRequestsWithCommitMutex.Lock() + defer fake.listPullRequestsWithCommitMutex.Unlock() + fake.ListPullRequestsWithCommitStub = nil + fake.listPullRequestsWithCommitReturns = struct { + result1 []*githuba.PullRequest + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + +func (fake *FakeClient) ListPullRequestsWithCommitReturnsOnCall(i int, result1 []*githuba.PullRequest, result2 *githuba.Response, result3 error) { + fake.listPullRequestsWithCommitMutex.Lock() + defer fake.listPullRequestsWithCommitMutex.Unlock() + fake.ListPullRequestsWithCommitStub = nil + if fake.listPullRequestsWithCommitReturnsOnCall == nil { + fake.listPullRequestsWithCommitReturnsOnCall = make(map[int]struct { + result1 []*githuba.PullRequest + result2 *githuba.Response + result3 error + }) + } + fake.listPullRequestsWithCommitReturnsOnCall[i] = struct { + result1 []*githuba.PullRequest + result2 *githuba.Response + result3 error + }{result1, result2, result3} +} + func (fake *FakeClient) ListReleases(arg1 context.Context, arg2 string, arg3 string, arg4 *githuba.ListOptions) ([]*githuba.RepositoryRelease, *githuba.Response, error) { fake.listReleasesMutex.Lock() ret, specificReturn := fake.listReleasesReturnsOnCall[len(fake.listReleasesArgsForCall)] @@ -207,6 +644,16 @@ func (fake *FakeClient) ListTagsReturnsOnCall(i int, result1 []*githuba.Reposito func (fake *FakeClient) Invocations() map[string][][]interface{} { fake.invocationsMutex.RLock() defer fake.invocationsMutex.RUnlock() + fake.getCommitMutex.RLock() + defer fake.getCommitMutex.RUnlock() + fake.getPullRequestMutex.RLock() + defer fake.getPullRequestMutex.RUnlock() + fake.getRepoCommitMutex.RLock() + defer fake.getRepoCommitMutex.RUnlock() + fake.listCommitsMutex.RLock() + defer fake.listCommitsMutex.RUnlock() + fake.listPullRequestsWithCommitMutex.RLock() + defer fake.listPullRequestsWithCommitMutex.RUnlock() fake.listReleasesMutex.RLock() defer fake.listReleasesMutex.RUnlock() fake.listTagsMutex.RLock() diff --git a/pkg/github/internal/BUILD.bazel b/pkg/github/internal/BUILD.bazel new file mode 100644 index 00000000000..4786c796bd8 --- /dev/null +++ b/pkg/github/internal/BUILD.bazel @@ -0,0 +1,36 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["retry.go"], + importpath = "k8s.io/release/pkg/github/internal", + visibility = ["//pkg/github:__subpackages__"], + deps = [ + "@com_github_google_go_github_v29//github:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = ["retry_test.go"], + embed = [":go_default_library"], + deps = [ + "@com_github_google_go_github_v29//github:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/notes/internal/retry.go b/pkg/github/internal/retry.go similarity index 100% rename from pkg/notes/internal/retry.go rename to pkg/github/internal/retry.go diff --git a/pkg/notes/internal/retry_test.go b/pkg/github/internal/retry_test.go similarity index 98% rename from pkg/notes/internal/retry_test.go rename to pkg/github/internal/retry_test.go index ca01e7daa24..2b32de916e3 100644 --- a/pkg/notes/internal/retry_test.go +++ b/pkg/github/internal/retry_test.go @@ -25,7 +25,7 @@ import ( "github.com/google/go-github/v29/github" "github.com/sirupsen/logrus" - "k8s.io/release/pkg/notes/internal" + "k8s.io/release/pkg/github/internal" ) func TestMain(m *testing.M) { diff --git a/pkg/notes/client/record.go b/pkg/github/record.go similarity index 82% rename from pkg/notes/client/record.go rename to pkg/github/record.go index 081bc53d907..75df28b12ae 100644 --- a/pkg/notes/client/record.go +++ b/pkg/github/record.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package client +package github import ( "context" @@ -34,10 +34,12 @@ type gitHubAPI string const ( gitHubAPIGetCommit gitHubAPI = "GetCommit" - gitHubAPIListCommits gitHubAPI = "ListCommits" - gitHubAPIListPullRequestsWithCommit gitHubAPI = "ListPullRequestsWithCommit" gitHubAPIGetPullRequest gitHubAPI = "GetPullRequest" gitHubAPIGetRepoCommit gitHubAPI = "GetRepoCommit" + gitHubAPIListCommits gitHubAPI = "ListCommits" + gitHubAPIListPullRequestsWithCommit gitHubAPI = "ListPullRequestsWithCommit" + gitHubAPIListReleases gitHubAPI = "ListReleases" + gitHubAPIListTags gitHubAPI = "ListTags" ) type apiRecord struct { @@ -119,6 +121,32 @@ func (c *githubNotesRecordClient) GetRepoCommit(ctx context.Context, owner, repo return commit, resp, nil } +func (c *githubNotesRecordClient) ListReleases( + ctx context.Context, owner, repo string, opt *github.ListOptions, +) ([]*github.RepositoryRelease, *github.Response, error) { + releases, resp, err := c.client.ListReleases(ctx, owner, repo, opt) + if err != nil { + return nil, nil, err + } + if err := c.recordAPICall(gitHubAPIListReleases, releases, resp); err != nil { + return nil, nil, err + } + return releases, resp, nil +} + +func (c *githubNotesRecordClient) ListTags( + ctx context.Context, owner, repo string, opt *github.ListOptions, +) ([]*github.RepositoryTag, *github.Response, error) { + tags, resp, err := c.client.ListTags(ctx, owner, repo, opt) + if err != nil { + return nil, nil, err + } + if err := c.recordAPICall(gitHubAPIListTags, tags, resp); err != nil { + return nil, nil, err + } + return tags, resp, nil +} + // recordAPICall records a single GitHub API call into a JSON file by ensuring // naming conventions func (c *githubNotesRecordClient) recordAPICall( diff --git a/pkg/notes/client/replay.go b/pkg/github/replay.go similarity index 79% rename from pkg/notes/client/replay.go rename to pkg/github/replay.go index 232b547d897..b11949cf32b 100644 --- a/pkg/notes/client/replay.go +++ b/pkg/github/replay.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package client +package github import ( "context" @@ -105,6 +105,36 @@ func (c *githubNotesReplayClient) GetRepoCommit(ctx context.Context, owner, repo return result, record.response(), nil } +func (c *githubNotesReplayClient) ListReleases( + ctx context.Context, owner, repo string, opt *github.ListOptions, +) ([]*github.RepositoryRelease, *github.Response, error) { + data, err := c.readRecordedData(gitHubAPIListReleases) + if err != nil { + return nil, nil, err + } + result := []*github.RepositoryRelease{} + record := apiRecord{Result: result} + if err := json.Unmarshal(data, &record); err != nil { + return nil, nil, err + } + return result, record.response(), nil +} + +func (c *githubNotesReplayClient) ListTags( + ctx context.Context, owner, repo string, opt *github.ListOptions, +) ([]*github.RepositoryTag, *github.Response, error) { + data, err := c.readRecordedData(gitHubAPIListTags) + if err != nil { + return nil, nil, err + } + result := []*github.RepositoryTag{} + record := apiRecord{Result: result} + if err := json.Unmarshal(data, &record); err != nil { + return nil, nil, err + } + return result, record.response(), nil +} + func (c *githubNotesReplayClient) readRecordedData(api gitHubAPI) ([]byte, error) { c.replayMutex.Lock() defer c.replayMutex.Unlock() diff --git a/pkg/notes/BUILD.bazel b/pkg/notes/BUILD.bazel index 81f3acab5fd..92a6878c590 100644 --- a/pkg/notes/BUILD.bazel +++ b/pkg/notes/BUILD.bazel @@ -9,7 +9,7 @@ go_library( importpath = "k8s.io/release/pkg/notes", visibility = ["//visibility:public"], deps = [ - "//pkg/notes/client:go_default_library", + "//pkg/github:go_default_library", "//pkg/notes/options:go_default_library", "@com_github_google_go_github_v29//github:go_default_library", "@com_github_nozzle_throttler//:go_default_library", @@ -29,12 +29,10 @@ go_test( deps = [ "//pkg/git:go_default_library", "//pkg/github:go_default_library", - "//pkg/notes/client:go_default_library", - "//pkg/notes/client/clientfakes:go_default_library", + "//pkg/github/githubfakes:go_default_library", "@com_github_google_go_github_v29//github:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_stretchr_testify//require:go_default_library", - "@org_golang_x_oauth2//:go_default_library", ], ) @@ -49,7 +47,6 @@ filegroup( name = "all-srcs", srcs = [ ":package-srcs", - "//pkg/notes/client:all-srcs", "//pkg/notes/document:all-srcs", "//pkg/notes/internal:all-srcs", "//pkg/notes/options:all-srcs", diff --git a/pkg/notes/client/BUILD.bazel b/pkg/notes/client/BUILD.bazel deleted file mode 100644 index b885a0f8bab..00000000000 --- a/pkg/notes/client/BUILD.bazel +++ /dev/null @@ -1,35 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -go_library( - name = "go_default_library", - srcs = [ - "client.go", - "record.go", - "replay.go", - ], - importpath = "k8s.io/release/pkg/notes/client", - visibility = ["//visibility:public"], - deps = [ - "//pkg/notes/internal:go_default_library", - "@com_github_google_go_github_v29//github:go_default_library", - "@com_github_pkg_errors//:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", - ], -) - -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [ - ":package-srcs", - "//pkg/notes/client/clientfakes:all-srcs", - ], - tags = ["automanaged"], - visibility = ["//visibility:public"], -) diff --git a/pkg/notes/client/client.go b/pkg/notes/client/client.go deleted file mode 100644 index da8240447f2..00000000000 --- a/pkg/notes/client/client.go +++ /dev/null @@ -1,94 +0,0 @@ -/* -Copyright 2019 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package client - -import ( - "context" - - "github.com/google/go-github/v29/github" - "k8s.io/release/pkg/notes/internal" -) - -//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate - -// Client is used to talk to GitHub and query the repo/commit information -// needed to assemble the release notes -//counterfeiter:generate . Client -type Client interface { - GetCommit(ctx context.Context, owner string, repo string, sha string) (*github.Commit, *github.Response, error) - ListCommits(ctx context.Context, owner, repo string, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) - ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) - GetPullRequest(ctx context.Context, owner string, repo string, number int) (*github.PullRequest, *github.Response, error) - - // TODO: get rid of that method, currently only used in some test case - GetRepoCommit(ctx context.Context, owner, repo, sha string) (*github.RepositoryCommit, *github.Response, error) -} - -func New(ghc *github.Client) Client { - return &githubNotesClient{ghc} -} - -type githubNotesClient struct { - *github.Client -} - -var _ Client = &githubNotesClient{} - -func (c *githubNotesClient) GetCommit(ctx context.Context, owner, repo, sha string) (*github.Commit, *github.Response, error) { - for shouldRetry := internal.DefaultGithubErrChecker(); ; { - commit, resp, err := c.Git.GetCommit(ctx, owner, repo, sha) - if !shouldRetry(err) { - return commit, resp, err - } - } -} - -func (c *githubNotesClient) ListCommits(ctx context.Context, owner, repo string, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) { - for shouldRetry := internal.DefaultGithubErrChecker(); ; { - commits, resp, err := c.Repositories.ListCommits(ctx, owner, repo, opt) - if !shouldRetry(err) { - return commits, resp, err - } - } -} - -func (c *githubNotesClient) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) { - for shouldRetry := internal.DefaultGithubErrChecker(); ; { - prs, resp, err := c.PullRequests.ListPullRequestsWithCommit(ctx, owner, repo, sha, opt) - if !shouldRetry(err) { - return prs, resp, err - } - } -} - -func (c *githubNotesClient) GetPullRequest(ctx context.Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error) { - for shouldRetry := internal.DefaultGithubErrChecker(); ; { - pr, resp, err := c.PullRequests.Get(ctx, owner, repo, number) - if !shouldRetry(err) { - return pr, resp, err - } - } -} - -func (c *githubNotesClient) GetRepoCommit(ctx context.Context, owner, repo, sha string) (*github.RepositoryCommit, *github.Response, error) { - for shouldRetry := internal.DefaultGithubErrChecker(); ; { - commit, resp, err := c.Repositories.GetCommit(ctx, owner, repo, sha) - if !shouldRetry(err) { - return commit, resp, err - } - } -} diff --git a/pkg/notes/client/clientfakes/BUILD.bazel b/pkg/notes/client/clientfakes/BUILD.bazel deleted file mode 100644 index 2e6a2e9a335..00000000000 --- a/pkg/notes/client/clientfakes/BUILD.bazel +++ /dev/null @@ -1,26 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -go_library( - name = "go_default_library", - srcs = ["fake_client.go"], - importpath = "k8s.io/release/pkg/notes/client/clientfakes", - visibility = ["//visibility:public"], - deps = [ - "//pkg/notes/client:go_default_library", - "@com_github_google_go_github_v29//github:go_default_library", - ], -) - -filegroup( - name = "package-srcs", - srcs = glob(["**"]), - tags = ["automanaged"], - visibility = ["//visibility:private"], -) - -filegroup( - name = "all-srcs", - srcs = [":package-srcs"], - tags = ["automanaged"], - visibility = ["//visibility:public"], -) diff --git a/pkg/notes/client/clientfakes/fake_client.go b/pkg/notes/client/clientfakes/fake_client.go deleted file mode 100644 index 7627b6c2398..00000000000 --- a/pkg/notes/client/clientfakes/fake_client.go +++ /dev/null @@ -1,502 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Code generated by counterfeiter. DO NOT EDIT. -package clientfakes - -import ( - "context" - "sync" - - "github.com/google/go-github/v29/github" - "k8s.io/release/pkg/notes/client" -) - -type FakeClient struct { - GetCommitStub func(context.Context, string, string, string) (*github.Commit, *github.Response, error) - getCommitMutex sync.RWMutex - getCommitArgsForCall []struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - } - getCommitReturns struct { - result1 *github.Commit - result2 *github.Response - result3 error - } - getCommitReturnsOnCall map[int]struct { - result1 *github.Commit - result2 *github.Response - result3 error - } - GetPullRequestStub func(context.Context, string, string, int) (*github.PullRequest, *github.Response, error) - getPullRequestMutex sync.RWMutex - getPullRequestArgsForCall []struct { - arg1 context.Context - arg2 string - arg3 string - arg4 int - } - getPullRequestReturns struct { - result1 *github.PullRequest - result2 *github.Response - result3 error - } - getPullRequestReturnsOnCall map[int]struct { - result1 *github.PullRequest - result2 *github.Response - result3 error - } - GetRepoCommitStub func(context.Context, string, string, string) (*github.RepositoryCommit, *github.Response, error) - getRepoCommitMutex sync.RWMutex - getRepoCommitArgsForCall []struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - } - getRepoCommitReturns struct { - result1 *github.RepositoryCommit - result2 *github.Response - result3 error - } - getRepoCommitReturnsOnCall map[int]struct { - result1 *github.RepositoryCommit - result2 *github.Response - result3 error - } - ListCommitsStub func(context.Context, string, string, *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) - listCommitsMutex sync.RWMutex - listCommitsArgsForCall []struct { - arg1 context.Context - arg2 string - arg3 string - arg4 *github.CommitsListOptions - } - listCommitsReturns struct { - result1 []*github.RepositoryCommit - result2 *github.Response - result3 error - } - listCommitsReturnsOnCall map[int]struct { - result1 []*github.RepositoryCommit - result2 *github.Response - result3 error - } - ListPullRequestsWithCommitStub func(context.Context, string, string, string, *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) - listPullRequestsWithCommitMutex sync.RWMutex - listPullRequestsWithCommitArgsForCall []struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - arg5 *github.PullRequestListOptions - } - listPullRequestsWithCommitReturns struct { - result1 []*github.PullRequest - result2 *github.Response - result3 error - } - listPullRequestsWithCommitReturnsOnCall map[int]struct { - result1 []*github.PullRequest - result2 *github.Response - result3 error - } - invocations map[string][][]interface{} - invocationsMutex sync.RWMutex -} - -func (fake *FakeClient) GetCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string) (*github.Commit, *github.Response, error) { - fake.getCommitMutex.Lock() - ret, specificReturn := fake.getCommitReturnsOnCall[len(fake.getCommitArgsForCall)] - fake.getCommitArgsForCall = append(fake.getCommitArgsForCall, struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - }{arg1, arg2, arg3, arg4}) - fake.recordInvocation("GetCommit", []interface{}{arg1, arg2, arg3, arg4}) - fake.getCommitMutex.Unlock() - if fake.GetCommitStub != nil { - return fake.GetCommitStub(arg1, arg2, arg3, arg4) - } - if specificReturn { - return ret.result1, ret.result2, ret.result3 - } - fakeReturns := fake.getCommitReturns - return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 -} - -func (fake *FakeClient) GetCommitCallCount() int { - fake.getCommitMutex.RLock() - defer fake.getCommitMutex.RUnlock() - return len(fake.getCommitArgsForCall) -} - -func (fake *FakeClient) GetCommitCalls(stub func(context.Context, string, string, string) (*github.Commit, *github.Response, error)) { - fake.getCommitMutex.Lock() - defer fake.getCommitMutex.Unlock() - fake.GetCommitStub = stub -} - -func (fake *FakeClient) GetCommitArgsForCall(i int) (context.Context, string, string, string) { - fake.getCommitMutex.RLock() - defer fake.getCommitMutex.RUnlock() - argsForCall := fake.getCommitArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 -} - -func (fake *FakeClient) GetCommitReturns(result1 *github.Commit, result2 *github.Response, result3 error) { - fake.getCommitMutex.Lock() - defer fake.getCommitMutex.Unlock() - fake.GetCommitStub = nil - fake.getCommitReturns = struct { - result1 *github.Commit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) GetCommitReturnsOnCall(i int, result1 *github.Commit, result2 *github.Response, result3 error) { - fake.getCommitMutex.Lock() - defer fake.getCommitMutex.Unlock() - fake.GetCommitStub = nil - if fake.getCommitReturnsOnCall == nil { - fake.getCommitReturnsOnCall = make(map[int]struct { - result1 *github.Commit - result2 *github.Response - result3 error - }) - } - fake.getCommitReturnsOnCall[i] = struct { - result1 *github.Commit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) GetPullRequest(arg1 context.Context, arg2 string, arg3 string, arg4 int) (*github.PullRequest, *github.Response, error) { - fake.getPullRequestMutex.Lock() - ret, specificReturn := fake.getPullRequestReturnsOnCall[len(fake.getPullRequestArgsForCall)] - fake.getPullRequestArgsForCall = append(fake.getPullRequestArgsForCall, struct { - arg1 context.Context - arg2 string - arg3 string - arg4 int - }{arg1, arg2, arg3, arg4}) - fake.recordInvocation("GetPullRequest", []interface{}{arg1, arg2, arg3, arg4}) - fake.getPullRequestMutex.Unlock() - if fake.GetPullRequestStub != nil { - return fake.GetPullRequestStub(arg1, arg2, arg3, arg4) - } - if specificReturn { - return ret.result1, ret.result2, ret.result3 - } - fakeReturns := fake.getPullRequestReturns - return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 -} - -func (fake *FakeClient) GetPullRequestCallCount() int { - fake.getPullRequestMutex.RLock() - defer fake.getPullRequestMutex.RUnlock() - return len(fake.getPullRequestArgsForCall) -} - -func (fake *FakeClient) GetPullRequestCalls(stub func(context.Context, string, string, int) (*github.PullRequest, *github.Response, error)) { - fake.getPullRequestMutex.Lock() - defer fake.getPullRequestMutex.Unlock() - fake.GetPullRequestStub = stub -} - -func (fake *FakeClient) GetPullRequestArgsForCall(i int) (context.Context, string, string, int) { - fake.getPullRequestMutex.RLock() - defer fake.getPullRequestMutex.RUnlock() - argsForCall := fake.getPullRequestArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 -} - -func (fake *FakeClient) GetPullRequestReturns(result1 *github.PullRequest, result2 *github.Response, result3 error) { - fake.getPullRequestMutex.Lock() - defer fake.getPullRequestMutex.Unlock() - fake.GetPullRequestStub = nil - fake.getPullRequestReturns = struct { - result1 *github.PullRequest - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) GetPullRequestReturnsOnCall(i int, result1 *github.PullRequest, result2 *github.Response, result3 error) { - fake.getPullRequestMutex.Lock() - defer fake.getPullRequestMutex.Unlock() - fake.GetPullRequestStub = nil - if fake.getPullRequestReturnsOnCall == nil { - fake.getPullRequestReturnsOnCall = make(map[int]struct { - result1 *github.PullRequest - result2 *github.Response - result3 error - }) - } - fake.getPullRequestReturnsOnCall[i] = struct { - result1 *github.PullRequest - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) GetRepoCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string) (*github.RepositoryCommit, *github.Response, error) { - fake.getRepoCommitMutex.Lock() - ret, specificReturn := fake.getRepoCommitReturnsOnCall[len(fake.getRepoCommitArgsForCall)] - fake.getRepoCommitArgsForCall = append(fake.getRepoCommitArgsForCall, struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - }{arg1, arg2, arg3, arg4}) - fake.recordInvocation("GetRepoCommit", []interface{}{arg1, arg2, arg3, arg4}) - fake.getRepoCommitMutex.Unlock() - if fake.GetRepoCommitStub != nil { - return fake.GetRepoCommitStub(arg1, arg2, arg3, arg4) - } - if specificReturn { - return ret.result1, ret.result2, ret.result3 - } - fakeReturns := fake.getRepoCommitReturns - return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 -} - -func (fake *FakeClient) GetRepoCommitCallCount() int { - fake.getRepoCommitMutex.RLock() - defer fake.getRepoCommitMutex.RUnlock() - return len(fake.getRepoCommitArgsForCall) -} - -func (fake *FakeClient) GetRepoCommitCalls(stub func(context.Context, string, string, string) (*github.RepositoryCommit, *github.Response, error)) { - fake.getRepoCommitMutex.Lock() - defer fake.getRepoCommitMutex.Unlock() - fake.GetRepoCommitStub = stub -} - -func (fake *FakeClient) GetRepoCommitArgsForCall(i int) (context.Context, string, string, string) { - fake.getRepoCommitMutex.RLock() - defer fake.getRepoCommitMutex.RUnlock() - argsForCall := fake.getRepoCommitArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 -} - -func (fake *FakeClient) GetRepoCommitReturns(result1 *github.RepositoryCommit, result2 *github.Response, result3 error) { - fake.getRepoCommitMutex.Lock() - defer fake.getRepoCommitMutex.Unlock() - fake.GetRepoCommitStub = nil - fake.getRepoCommitReturns = struct { - result1 *github.RepositoryCommit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) GetRepoCommitReturnsOnCall(i int, result1 *github.RepositoryCommit, result2 *github.Response, result3 error) { - fake.getRepoCommitMutex.Lock() - defer fake.getRepoCommitMutex.Unlock() - fake.GetRepoCommitStub = nil - if fake.getRepoCommitReturnsOnCall == nil { - fake.getRepoCommitReturnsOnCall = make(map[int]struct { - result1 *github.RepositoryCommit - result2 *github.Response - result3 error - }) - } - fake.getRepoCommitReturnsOnCall[i] = struct { - result1 *github.RepositoryCommit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) ListCommits(arg1 context.Context, arg2 string, arg3 string, arg4 *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) { - fake.listCommitsMutex.Lock() - ret, specificReturn := fake.listCommitsReturnsOnCall[len(fake.listCommitsArgsForCall)] - fake.listCommitsArgsForCall = append(fake.listCommitsArgsForCall, struct { - arg1 context.Context - arg2 string - arg3 string - arg4 *github.CommitsListOptions - }{arg1, arg2, arg3, arg4}) - fake.recordInvocation("ListCommits", []interface{}{arg1, arg2, arg3, arg4}) - fake.listCommitsMutex.Unlock() - if fake.ListCommitsStub != nil { - return fake.ListCommitsStub(arg1, arg2, arg3, arg4) - } - if specificReturn { - return ret.result1, ret.result2, ret.result3 - } - fakeReturns := fake.listCommitsReturns - return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 -} - -func (fake *FakeClient) ListCommitsCallCount() int { - fake.listCommitsMutex.RLock() - defer fake.listCommitsMutex.RUnlock() - return len(fake.listCommitsArgsForCall) -} - -func (fake *FakeClient) ListCommitsCalls(stub func(context.Context, string, string, *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error)) { - fake.listCommitsMutex.Lock() - defer fake.listCommitsMutex.Unlock() - fake.ListCommitsStub = stub -} - -func (fake *FakeClient) ListCommitsArgsForCall(i int) (context.Context, string, string, *github.CommitsListOptions) { - fake.listCommitsMutex.RLock() - defer fake.listCommitsMutex.RUnlock() - argsForCall := fake.listCommitsArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 -} - -func (fake *FakeClient) ListCommitsReturns(result1 []*github.RepositoryCommit, result2 *github.Response, result3 error) { - fake.listCommitsMutex.Lock() - defer fake.listCommitsMutex.Unlock() - fake.ListCommitsStub = nil - fake.listCommitsReturns = struct { - result1 []*github.RepositoryCommit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) ListCommitsReturnsOnCall(i int, result1 []*github.RepositoryCommit, result2 *github.Response, result3 error) { - fake.listCommitsMutex.Lock() - defer fake.listCommitsMutex.Unlock() - fake.ListCommitsStub = nil - if fake.listCommitsReturnsOnCall == nil { - fake.listCommitsReturnsOnCall = make(map[int]struct { - result1 []*github.RepositoryCommit - result2 *github.Response - result3 error - }) - } - fake.listCommitsReturnsOnCall[i] = struct { - result1 []*github.RepositoryCommit - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) ListPullRequestsWithCommit(arg1 context.Context, arg2 string, arg3 string, arg4 string, arg5 *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) { - fake.listPullRequestsWithCommitMutex.Lock() - ret, specificReturn := fake.listPullRequestsWithCommitReturnsOnCall[len(fake.listPullRequestsWithCommitArgsForCall)] - fake.listPullRequestsWithCommitArgsForCall = append(fake.listPullRequestsWithCommitArgsForCall, struct { - arg1 context.Context - arg2 string - arg3 string - arg4 string - arg5 *github.PullRequestListOptions - }{arg1, arg2, arg3, arg4, arg5}) - fake.recordInvocation("ListPullRequestsWithCommit", []interface{}{arg1, arg2, arg3, arg4, arg5}) - fake.listPullRequestsWithCommitMutex.Unlock() - if fake.ListPullRequestsWithCommitStub != nil { - return fake.ListPullRequestsWithCommitStub(arg1, arg2, arg3, arg4, arg5) - } - if specificReturn { - return ret.result1, ret.result2, ret.result3 - } - fakeReturns := fake.listPullRequestsWithCommitReturns - return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3 -} - -func (fake *FakeClient) ListPullRequestsWithCommitCallCount() int { - fake.listPullRequestsWithCommitMutex.RLock() - defer fake.listPullRequestsWithCommitMutex.RUnlock() - return len(fake.listPullRequestsWithCommitArgsForCall) -} - -func (fake *FakeClient) ListPullRequestsWithCommitCalls(stub func(context.Context, string, string, string, *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error)) { - fake.listPullRequestsWithCommitMutex.Lock() - defer fake.listPullRequestsWithCommitMutex.Unlock() - fake.ListPullRequestsWithCommitStub = stub -} - -func (fake *FakeClient) ListPullRequestsWithCommitArgsForCall(i int) (context.Context, string, string, string, *github.PullRequestListOptions) { - fake.listPullRequestsWithCommitMutex.RLock() - defer fake.listPullRequestsWithCommitMutex.RUnlock() - argsForCall := fake.listPullRequestsWithCommitArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 -} - -func (fake *FakeClient) ListPullRequestsWithCommitReturns(result1 []*github.PullRequest, result2 *github.Response, result3 error) { - fake.listPullRequestsWithCommitMutex.Lock() - defer fake.listPullRequestsWithCommitMutex.Unlock() - fake.ListPullRequestsWithCommitStub = nil - fake.listPullRequestsWithCommitReturns = struct { - result1 []*github.PullRequest - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) ListPullRequestsWithCommitReturnsOnCall(i int, result1 []*github.PullRequest, result2 *github.Response, result3 error) { - fake.listPullRequestsWithCommitMutex.Lock() - defer fake.listPullRequestsWithCommitMutex.Unlock() - fake.ListPullRequestsWithCommitStub = nil - if fake.listPullRequestsWithCommitReturnsOnCall == nil { - fake.listPullRequestsWithCommitReturnsOnCall = make(map[int]struct { - result1 []*github.PullRequest - result2 *github.Response - result3 error - }) - } - fake.listPullRequestsWithCommitReturnsOnCall[i] = struct { - result1 []*github.PullRequest - result2 *github.Response - result3 error - }{result1, result2, result3} -} - -func (fake *FakeClient) Invocations() map[string][][]interface{} { - fake.invocationsMutex.RLock() - defer fake.invocationsMutex.RUnlock() - fake.getCommitMutex.RLock() - defer fake.getCommitMutex.RUnlock() - fake.getPullRequestMutex.RLock() - defer fake.getPullRequestMutex.RUnlock() - fake.getRepoCommitMutex.RLock() - defer fake.getRepoCommitMutex.RUnlock() - fake.listCommitsMutex.RLock() - defer fake.listCommitsMutex.RUnlock() - fake.listPullRequestsWithCommitMutex.RLock() - defer fake.listPullRequestsWithCommitMutex.RUnlock() - copiedInvocations := map[string][][]interface{}{} - for key, value := range fake.invocations { - copiedInvocations[key] = value - } - return copiedInvocations -} - -func (fake *FakeClient) recordInvocation(key string, args []interface{}) { - fake.invocationsMutex.Lock() - defer fake.invocationsMutex.Unlock() - if fake.invocations == nil { - fake.invocations = map[string][][]interface{}{} - } - if fake.invocations[key] == nil { - fake.invocations[key] = [][]interface{}{} - } - fake.invocations[key] = append(fake.invocations[key], args) -} - -var _ client.Client = new(FakeClient) diff --git a/pkg/notes/document/BUILD.bazel b/pkg/notes/document/BUILD.bazel index 09e21389037..e4a8038f80e 100644 --- a/pkg/notes/document/BUILD.bazel +++ b/pkg/notes/document/BUILD.bazel @@ -15,7 +15,7 @@ go_library( go_test( name = "go_default_test", srcs = ["document_test.go"], - data = ["testdata/document.md.golden"], + data = glob(["testdata/**"]), embed = [":go_default_library"], deps = [ "//pkg/notes/internal:go_default_library", diff --git a/pkg/notes/internal/BUILD.bazel b/pkg/notes/internal/BUILD.bazel index e319c1b15a7..df4c68c5ec1 100644 --- a/pkg/notes/internal/BUILD.bazel +++ b/pkg/notes/internal/BUILD.bazel @@ -1,27 +1,10 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = [ - "retry.go", - "template.go", - ], + srcs = ["template.go"], importpath = "k8s.io/release/pkg/notes/internal", visibility = ["//pkg/notes:__subpackages__"], - deps = [ - "@com_github_google_go_github_v29//github:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", - ], -) - -go_test( - name = "go_default_test", - srcs = ["retry_test.go"], - embed = [":go_default_library"], - deps = [ - "@com_github_google_go_github_v29//github:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", - ], ) filegroup( diff --git a/pkg/notes/notes.go b/pkg/notes/notes.go index 37503189fb2..7b70d411d32 100644 --- a/pkg/notes/notes.go +++ b/pkg/notes/notes.go @@ -27,12 +27,12 @@ import ( "strings" "sync" - "github.com/google/go-github/v29/github" + gogithub "github.com/google/go-github/v29/github" "github.com/nozzle/throttler" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "k8s.io/release/pkg/notes/client" + "k8s.io/release/pkg/github" "k8s.io/release/pkg/notes/options" ) @@ -129,27 +129,31 @@ type ReleaseNotes map[int]*ReleaseNote type ReleaseNotesHistory []int type Result struct { - commit *github.RepositoryCommit - pullRequest *github.PullRequest + commit *gogithub.RepositoryCommit + pullRequest *gogithub.PullRequest } type Gatherer struct { - client client.Client + client github.Client context context.Context options *options.Options } // NewGatherer creates a new notes gatherer -func NewGatherer(ctx context.Context, opts *options.Options) *Gatherer { +func NewGatherer(ctx context.Context, opts *options.Options) (*Gatherer, error) { + client, err := opts.Client() + if err != nil { + return nil, errors.Wrap(err, "unable to create notes client") + } return &Gatherer{ - client: opts.Client(), + client: client, context: ctx, options: opts, - } + }, nil } // NewGathererWithClient creates a new notes gatherer with a specific client -func NewGathererWithClient(ctx context.Context, c client.Client) *Gatherer { +func NewGathererWithClient(ctx context.Context, c github.Client) *Gatherer { return &Gatherer{ client: c, context: ctx, @@ -382,7 +386,7 @@ func (g *Gatherer) ReleaseNoteFromCommit(result *Result, relVer string) (*Releas // ListCommits lists all commits starting from a given commit SHA and ending at // a given commit SHA. -func (g *Gatherer) ListCommits(branch, start, end string) ([]*github.RepositoryCommit, error) { +func (g *Gatherer) ListCommits(branch, start, end string) ([]*gogithub.RepositoryCommit, error) { startCommit, _, err := g.client.GetCommit(g.context, g.options.GithubOrg, g.options.GithubRepo, start) if err != nil { return nil, err @@ -395,7 +399,7 @@ func (g *Gatherer) ListCommits(branch, start, end string) ([]*github.RepositoryC allCommits := &commitList{} - worker := func(clo *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) { + worker := func(clo *gogithub.CommitsListOptions) ([]*gogithub.RepositoryCommit, *gogithub.Response, error) { commits, resp, err := g.client.ListCommits(g.context, g.options.GithubOrg, g.options.GithubRepo, clo) if err != nil { return nil, nil, err @@ -403,11 +407,11 @@ func (g *Gatherer) ListCommits(branch, start, end string) ([]*github.RepositoryC return commits, resp, err } - clo := github.CommitsListOptions{ + clo := gogithub.CommitsListOptions{ SHA: branch, Since: startCommit.GetCommitter().GetDate(), Until: endCommit.GetCommitter().GetDate(), - ListOptions: github.ListOptions{ + ListOptions: gogithub.ListOptions{ Page: 1, PerPage: 100, }, @@ -452,16 +456,16 @@ func (g *Gatherer) ListCommits(branch, start, end string) ([]*github.RepositoryC type commitList struct { sync.RWMutex - list []*github.RepositoryCommit + list []*gogithub.RepositoryCommit } -func (l *commitList) Add(c []*github.RepositoryCommit) { +func (l *commitList) Add(c []*gogithub.RepositoryCommit) { l.Lock() defer l.Unlock() l.list = append(l.list, c...) } -func (l *commitList) List() []*github.RepositoryCommit { +func (l *commitList) List() []*gogithub.RepositoryCommit { l.RLock() defer l.RUnlock() return l.list @@ -510,7 +514,7 @@ func matchesIncludeFilter(msg string) *regexp.Regexp { // gatherNotes list commits that have release notes starting from a given // commit SHA and ending at a given commit SHA. This function is similar to // ListCommits except that only commits with tagged release notes are returned. -func (g *Gatherer) gatherNotes(commits []*github.RepositoryCommit) (filtered []*Result, err error) { +func (g *Gatherer) gatherNotes(commits []*gogithub.RepositoryCommit) (filtered []*Result, err error) { allResults := &resultList{} nrOfCommits := len(commits) @@ -534,7 +538,7 @@ func (g *Gatherer) gatherNotes(commits []*github.RepositoryCommit) (filtered []* // and use that throttler for all API calls. t := throttler.New(maxParallelRequests, nrOfCommits) - notesForCommit := func(commit *github.RepositoryCommit) { + notesForCommit := func(commit *gogithub.RepositoryCommit) { res, err := g.notesForCommit(commit) if err == nil && res != nil { allResults.Add(res) @@ -568,7 +572,7 @@ func (g *Gatherer) gatherNotes(commits []*github.RepositoryCommit) (filtered []* return allResults.List(), nil } -func (g *Gatherer) notesForCommit(commit *github.RepositoryCommit) (*Result, error) { +func (g *Gatherer) notesForCommit(commit *gogithub.RepositoryCommit) (*Result, error) { prs, err := g.PRsFromCommit(commit) if err != nil { if err == errNoPRIDFoundInCommitMessage || err == errNoPRFoundForCommitSHA { @@ -634,8 +638,8 @@ func (l *resultList) List() []*Result { // PRsFromCommit return an API Pull Request struct given a commit struct. This is // useful for going from a commit log to the PR (which contains useful info such // as labels). -func (g *Gatherer) PRsFromCommit(commit *github.RepositoryCommit) ( - []*github.PullRequest, error, +func (g *Gatherer) PRsFromCommit(commit *gogithub.RepositoryCommit) ( + []*gogithub.PullRequest, error, ) { githubPRs, err := g.prsForCommitFromMessage(*commit.Commit.Message) if err != nil { @@ -652,7 +656,7 @@ func (g *Gatherer) PRsFromCommit(commit *github.RepositoryCommit) ( // a given string. This pattern is used often in the k/k repo and we can take // advantage of this to contextualize release note generation with the kind, sig, // area, etc labels. -func LabelsWithPrefix(pr *github.PullRequest, prefix string) []string { +func LabelsWithPrefix(pr *gogithub.PullRequest, prefix string) []string { labels := []string{} for _, label := range pr.Labels { if strings.HasPrefix(*label.Name, prefix) { @@ -664,7 +668,7 @@ func LabelsWithPrefix(pr *github.PullRequest, prefix string) []string { // IsActionRequired indicates whether or not the release-note-action-required // label was set on the PR. -func IsActionRequired(pr *github.PullRequest) bool { +func IsActionRequired(pr *gogithub.PullRequest) bool { for _, label := range pr.Labels { if *label.Name == "release-note-action-required" { return true @@ -711,10 +715,10 @@ func HasString(a []string, x string) bool { } // prsForCommitFromSHA retrieves the PR numbers for a commit given its sha -func (g *Gatherer) prsForCommitFromSHA(sha string) (prs []*github.PullRequest, err error) { - plo := &github.PullRequestListOptions{ +func (g *Gatherer) prsForCommitFromSHA(sha string) (prs []*gogithub.PullRequest, err error) { + plo := &gogithub.PullRequestListOptions{ State: "closed", - ListOptions: github.ListOptions{ + ListOptions: gogithub.ListOptions{ Page: 1, PerPage: 100, }, @@ -742,7 +746,7 @@ func (g *Gatherer) prsForCommitFromSHA(sha string) (prs []*github.PullRequest, e return prs, nil } -func (g *Gatherer) prsForCommitFromMessage(commitMessage string) (prs []*github.PullRequest, err error) { +func (g *Gatherer) prsForCommitFromMessage(commitMessage string) (prs []*gogithub.PullRequest, err error) { prsNum, err := prsNumForCommitFromMessage(commitMessage) if err != nil { return nil, err diff --git a/pkg/notes/notes_gatherer_test.go b/pkg/notes/notes_gatherer_test.go index 48166d5642d..7da4ffbffb5 100644 --- a/pkg/notes/notes_gatherer_test.go +++ b/pkg/notes/notes_gatherer_test.go @@ -31,7 +31,7 @@ import ( "github.com/google/go-github/v29/github" "github.com/sirupsen/logrus" "k8s.io/release/pkg/git" - "k8s.io/release/pkg/notes/client/clientfakes" + "k8s.io/release/pkg/github/githubfakes" ) func TestMain(m *testing.M) { @@ -185,7 +185,7 @@ func TestListCommits(t *testing.T) { tc := tc t.Parallel() - client := &clientfakes.FakeClient{} + client := &githubfakes.FakeClient{} for i, returns := range tc.getCommitReturns { if i == always { @@ -383,7 +383,7 @@ func TestGatherNotes(t *testing.T) { tc := tc t.Parallel() - client := &clientfakes.FakeClient{} + client := &githubfakes.FakeClient{} gatherer := NewGathererWithClient(context.Background(), client) if stubber := tc.listPullRequestsWithCommitStubber; stubber != nil { diff --git a/pkg/notes/notes_test.go b/pkg/notes/notes_test.go index 8707b602a29..6c177ac1c2d 100644 --- a/pkg/notes/notes_test.go +++ b/pkg/notes/notes_test.go @@ -23,26 +23,19 @@ import ( "os" "testing" - "github.com/google/go-github/v29/github" "github.com/stretchr/testify/require" - "golang.org/x/oauth2" kgithub "k8s.io/release/pkg/github" - "k8s.io/release/pkg/notes/client" ) -func githubClient(t *testing.T) (client.Client, context.Context) { - token, tokenSet := os.LookupEnv(kgithub.TokenEnvKey) +func githubClient(t *testing.T) (kgithub.Client, context.Context) { + _, tokenSet := os.LookupEnv(kgithub.TokenEnvKey) if !tokenSet { t.Skipf("%s is not set", kgithub.TokenEnvKey) } - ctx := context.Background() - httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - )) - c := client.New(github.NewClient(httpClient)) - return c, ctx + c := kgithub.New() + return c.Client(), context.Background() } func TestStripActionRequired(t *testing.T) { diff --git a/pkg/notes/options/BUILD.bazel b/pkg/notes/options/BUILD.bazel index 752fbdc1fff..7e46d5e9204 100644 --- a/pkg/notes/options/BUILD.bazel +++ b/pkg/notes/options/BUILD.bazel @@ -8,12 +8,9 @@ go_library( deps = [ "//pkg/git:go_default_library", "//pkg/github:go_default_library", - "//pkg/notes/client:go_default_library", "//pkg/notes/internal:go_default_library", - "@com_github_google_go_github_v29//github:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", - "@org_golang_x_oauth2//:go_default_library", ], ) diff --git a/pkg/notes/options/options.go b/pkg/notes/options/options.go index 0ee1e131824..29b9bee5e4a 100644 --- a/pkg/notes/options/options.go +++ b/pkg/notes/options/options.go @@ -17,21 +17,16 @@ limitations under the License. package options import ( - "context" "os" "strings" "io/ioutil" - gogithub "github.com/google/go-github/v29/github" - "github.com/pkg/errors" "github.com/sirupsen/logrus" - "golang.org/x/oauth2" "k8s.io/release/pkg/git" "k8s.io/release/pkg/github" - "k8s.io/release/pkg/notes/client" "k8s.io/release/pkg/notes/internal" ) @@ -254,22 +249,20 @@ func (o *Options) repo() (repo *git.Repo, err error) { // a Client which in addition records the responses from Github and stores them // on disk, or a Client that replays those pre-recorded responses and does not // talk to the GitHub API at all. -func (o *Options) Client() client.Client { +func (o *Options) Client() (github.Client, error) { if o.ReplayDir != "" { - return client.NewReplayer(o.ReplayDir) + return github.NewReplayer(o.ReplayDir), nil } // Create a real GitHub API client - // TODO: refactor on top of our internal github package - ctx := context.Background() - httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: o.githubToken}, - )) - c := client.New(gogithub.NewClient(httpClient)) + gh, err := github.NewWithToken(o.githubToken) + if err != nil { + return nil, errors.Wrap(err, "unable to create GitHub client") + } if o.RecordDir != "" { - return client.NewRecorder(c, o.RecordDir) + return github.NewRecorder(gh.Client(), o.RecordDir), nil } - return c + return gh.Client(), nil }