Skip to content

Commit 336be0f

Browse files
authored
Merge pull request #1178 from saschagrunert/github-refactor
Merge notes client and github client
2 parents 2dcbd70 + ce92daa commit 336be0f

File tree

23 files changed

+731
-758
lines changed

23 files changed

+731
-758
lines changed

cmd/krel/cmd/changelog.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ func generateReleaseNotes(opts *changelogOptions, branch, startRev, endRev strin
246246
return "", err
247247
}
248248

249-
gatherer := notes.NewGatherer(context.Background(), notesOptions)
249+
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
250+
if err != nil {
251+
return "", errors.Wrapf(err, "retrieving notes gatherer")
252+
}
253+
250254
releaseNotes, history, err := gatherer.ListReleaseNotes()
251255
if err != nil {
252256
return "", errors.Wrapf(err, "listing release notes")

cmd/krel/cmd/release_notes.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ func releaseNotesFrom(startTag string) (*releaseNotesResult, error) {
346346
}
347347

348348
// Fetch the notes
349-
gatherer := notes.NewGatherer(context.Background(), notesOptions)
349+
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
350+
if err != nil {
351+
return nil, errors.Wrapf(err, "retrieving notes gatherer")
352+
}
350353
releaseNotes, history, err := gatherer.ListReleaseNotes()
351354
if err != nil {
352355
return nil, errors.Wrapf(err, "listing release notes")

cmd/release-notes/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ func init() {
215215
func GetReleaseNotes() (notes.ReleaseNotes, notes.ReleaseNotesHistory, error) {
216216
logrus.Info("fetching all commits. This might take a while...")
217217

218-
gatherer := notes.NewGatherer(context.Background(), opts)
218+
gatherer, err := notes.NewGatherer(context.Background(), opts)
219+
if err != nil {
220+
return nil, nil, errors.Wrapf(err, "retrieving notes gatherer")
221+
}
219222
releaseNotes, history, err := gatherer.ListReleaseNotes()
220223
if err != nil {
221224
return nil, nil, errors.Wrapf(err, "listing release notes")

pkg/github/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["github.go"],
5+
srcs = [
6+
"github.go",
7+
"record.go",
8+
"replay.go",
9+
],
610
importpath = "k8s.io/release/pkg/github",
711
visibility = ["//visibility:public"],
812
deps = [
913
"//pkg/git:go_default_library",
14+
"//pkg/github/internal:go_default_library",
1015
"//pkg/util:go_default_library",
1116
"@com_github_google_go_github_v29//github:go_default_library",
1217
"@com_github_pkg_errors//:go_default_library",
@@ -39,6 +44,7 @@ filegroup(
3944
srcs = [
4045
":package-srcs",
4146
"//pkg/github/githubfakes:all-srcs",
47+
"//pkg/github/internal:all-srcs",
4248
],
4349
tags = ["automanaged"],
4450
visibility = ["//visibility:public"],

pkg/github/github.go

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23+
"os"
2324
"strings"
2425

2526
"github.com/google/go-github/v29/github"
@@ -28,6 +29,7 @@ import (
2829
"golang.org/x/oauth2"
2930

3031
"k8s.io/release/pkg/git"
32+
"k8s.io/release/pkg/github/internal"
3133
"k8s.io/release/pkg/util"
3234
)
3335

@@ -48,13 +50,33 @@ type githubClient struct {
4850
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
4951
//counterfeiter:generate . Client
5052
type Client interface {
51-
ListTags(
52-
context.Context, string, string, *github.ListOptions,
53-
) ([]*github.RepositoryTag, *github.Response, error)
53+
GetCommit(
54+
context.Context, string, string, string,
55+
) (*github.Commit, *github.Response, error)
56+
57+
GetPullRequest(
58+
context.Context, string, string, int,
59+
) (*github.PullRequest, *github.Response, error)
60+
61+
GetRepoCommit(
62+
context.Context, string, string, string,
63+
) (*github.RepositoryCommit, *github.Response, error)
64+
65+
ListCommits(
66+
context.Context, string, string, *github.CommitsListOptions,
67+
) ([]*github.RepositoryCommit, *github.Response, error)
68+
69+
ListPullRequestsWithCommit(
70+
context.Context, string, string, string, *github.PullRequestListOptions,
71+
) ([]*github.PullRequest, *github.Response, error)
5472

5573
ListReleases(
5674
context.Context, string, string, *github.ListOptions,
5775
) ([]*github.RepositoryRelease, *github.Response, error)
76+
77+
ListTags(
78+
context.Context, string, string, *github.ListOptions,
79+
) ([]*github.RepositoryTag, *github.Response, error)
5880
}
5981

6082
// New creates a new default GitHub client. Tokens set via the $GITHUB_TOKEN
@@ -76,23 +98,107 @@ func New() *GitHub {
7698
return &GitHub{&githubClient{github.NewClient(client)}}
7799
}
78100

79-
func (g *githubClient) ListTags(
80-
ctx context.Context, owner, repo string, opt *github.ListOptions,
81-
) ([]*github.RepositoryTag, *github.Response, error) {
82-
return g.Client.Repositories.ListTags(ctx, owner, repo, opt)
101+
// NewWithToken can be used to specify a GITHUB_TOKEN before retrieving the
102+
// client to enforce authenticated GitHub requests
103+
func NewWithToken(token string) (*GitHub, error) {
104+
if err := os.Setenv(TokenEnvKey, token); err != nil {
105+
return nil, errors.Wrapf(err, "unable to export %s", TokenEnvKey)
106+
}
107+
return New(), nil
108+
}
109+
110+
func (g *githubClient) GetCommit(
111+
ctx context.Context, owner, repo, sha string,
112+
) (*github.Commit, *github.Response, error) {
113+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
114+
commit, resp, err := g.Git.GetCommit(ctx, owner, repo, sha)
115+
if !shouldRetry(err) {
116+
return commit, resp, err
117+
}
118+
}
119+
}
120+
121+
func (g *githubClient) GetPullRequest(
122+
ctx context.Context, owner, repo string, number int,
123+
) (*github.PullRequest, *github.Response, error) {
124+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
125+
pr, resp, err := g.PullRequests.Get(ctx, owner, repo, number)
126+
if !shouldRetry(err) {
127+
return pr, resp, err
128+
}
129+
}
130+
}
131+
132+
func (g *githubClient) GetRepoCommit(
133+
ctx context.Context, owner, repo, sha string,
134+
) (*github.RepositoryCommit, *github.Response, error) {
135+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
136+
commit, resp, err := g.Repositories.GetCommit(ctx, owner, repo, sha)
137+
if !shouldRetry(err) {
138+
return commit, resp, err
139+
}
140+
}
141+
}
142+
143+
func (g *githubClient) ListCommits(
144+
ctx context.Context, owner, repo string, opt *github.CommitsListOptions,
145+
) ([]*github.RepositoryCommit, *github.Response, error) {
146+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
147+
commits, resp, err := g.Repositories.ListCommits(ctx, owner, repo, opt)
148+
if !shouldRetry(err) {
149+
return commits, resp, err
150+
}
151+
}
152+
}
153+
154+
func (g *githubClient) ListPullRequestsWithCommit(
155+
ctx context.Context, owner, repo, sha string,
156+
opt *github.PullRequestListOptions,
157+
) ([]*github.PullRequest, *github.Response, error) {
158+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
159+
prs, resp, err := g.PullRequests.ListPullRequestsWithCommit(
160+
ctx, owner, repo, sha, opt,
161+
)
162+
if !shouldRetry(err) {
163+
return prs, resp, err
164+
}
165+
}
83166
}
84167

85168
func (g *githubClient) ListReleases(
86169
ctx context.Context, owner, repo string, opt *github.ListOptions,
87170
) ([]*github.RepositoryRelease, *github.Response, error) {
88-
return g.Client.Repositories.ListReleases(ctx, owner, repo, opt)
171+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
172+
releases, resp, err := g.Repositories.ListReleases(
173+
ctx, owner, repo, opt,
174+
)
175+
if !shouldRetry(err) {
176+
return releases, resp, err
177+
}
178+
}
179+
}
180+
181+
func (g *githubClient) ListTags(
182+
ctx context.Context, owner, repo string, opt *github.ListOptions,
183+
) ([]*github.RepositoryTag, *github.Response, error) {
184+
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
185+
tags, resp, err := g.Repositories.ListTags(ctx, owner, repo, opt)
186+
if !shouldRetry(err) {
187+
return tags, resp, err
188+
}
189+
}
89190
}
90191

91192
// SetClient can be used to manually set the internal GitHub client
92193
func (g *GitHub) SetClient(client Client) {
93194
g.client = client
94195
}
95196

197+
// Client can be used to retrieve the Client type
198+
func (g *GitHub) Client() Client {
199+
return g.client
200+
}
201+
96202
// TagsPerBranch is an abstraction over a simple branch to latest tag association
97203
type TagsPerBranch map[string]string
98204

0 commit comments

Comments
 (0)