@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"net/http"
23
+ "os"
23
24
"strings"
24
25
25
26
"github.com/google/go-github/v29/github"
@@ -28,6 +29,7 @@ import (
28
29
"golang.org/x/oauth2"
29
30
30
31
"k8s.io/release/pkg/git"
32
+ "k8s.io/release/pkg/github/internal"
31
33
"k8s.io/release/pkg/util"
32
34
)
33
35
@@ -48,13 +50,33 @@ type githubClient struct {
48
50
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
49
51
//counterfeiter:generate . Client
50
52
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 )
54
72
55
73
ListReleases (
56
74
context.Context , string , string , * github.ListOptions ,
57
75
) ([]* github.RepositoryRelease , * github.Response , error )
76
+
77
+ ListTags (
78
+ context.Context , string , string , * github.ListOptions ,
79
+ ) ([]* github.RepositoryTag , * github.Response , error )
58
80
}
59
81
60
82
// New creates a new default GitHub client. Tokens set via the $GITHUB_TOKEN
@@ -76,23 +98,107 @@ func New() *GitHub {
76
98
return & GitHub {& githubClient {github .NewClient (client )}}
77
99
}
78
100
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
+ }
83
166
}
84
167
85
168
func (g * githubClient ) ListReleases (
86
169
ctx context.Context , owner , repo string , opt * github.ListOptions ,
87
170
) ([]* 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
+ }
89
190
}
90
191
91
192
// SetClient can be used to manually set the internal GitHub client
92
193
func (g * GitHub ) SetClient (client Client ) {
93
194
g .client = client
94
195
}
95
196
197
+ // Client can be used to retrieve the Client type
198
+ func (g * GitHub ) Client () Client {
199
+ return g .client
200
+ }
201
+
96
202
// TagsPerBranch is an abstraction over a simple branch to latest tag association
97
203
type TagsPerBranch map [string ]string
98
204
0 commit comments