@@ -11,14 +11,15 @@ import (
11
11
)
12
12
13
13
type GithubProvider struct {
14
- client * github.Client
15
- organization string
16
- allBranches bool
14
+ client * github.Client
15
+ organization string
16
+ allBranches bool
17
+ allPullRequests bool
17
18
}
18
19
19
20
var _ SCMProviderService = & GithubProvider {}
20
21
21
- func NewGithubProvider (ctx context.Context , organization string , token string , url string , allBranches bool ) (* GithubProvider , error ) {
22
+ func NewGithubProvider (ctx context.Context , organization string , token string , url string , allBranches bool , allPullRequests bool ) (* GithubProvider , error ) {
22
23
var ts oauth2.TokenSource
23
24
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
24
25
if token == "" {
@@ -40,7 +41,7 @@ func NewGithubProvider(ctx context.Context, organization string, token string, u
40
41
return nil , err
41
42
}
42
43
}
43
- return & GithubProvider {client : client , organization : organization , allBranches : allBranches }, nil
44
+ return & GithubProvider {client : client , organization : organization , allBranches : allBranches , allPullRequests : allPullRequests }, nil
44
45
}
45
46
46
47
func (g * GithubProvider ) GetBranches (ctx context.Context , repo * Repository ) ([]* Repository , error ) {
@@ -64,6 +65,32 @@ func (g *GithubProvider) GetBranches(ctx context.Context, repo *Repository) ([]*
64
65
return repos , nil
65
66
}
66
67
68
+ func (g * GithubProvider ) GetPullRequests (ctx context.Context , repo * Repository ) ([]* Repository , error ) {
69
+ repos := []* Repository {}
70
+ pullRequests , err := g .listPullRequests (ctx , repo )
71
+ if err != nil {
72
+ return nil , fmt .Errorf ("error listing pull requests for %s/%s: %v" , repo .Organization , repo .Repository , err )
73
+ }
74
+
75
+ // go-github's PullRequest type does not have a GetLabel() function.
76
+ var labels []string
77
+ for _ , pullRequest := range pullRequests {
78
+ for _ , label := range pullRequest .Labels {
79
+ labels = append (labels , label .GetName ())
80
+ }
81
+ repos = append (repos , & Repository {
82
+ Organization : repo .Organization ,
83
+ Repository : repo .Repository ,
84
+ URL : repo .URL ,
85
+ Branch : pullRequest .GetTitle (),
86
+ SHA : pullRequest .GetHead ().GetSHA (),
87
+ Labels : labels ,
88
+ RepositoryId : repo .RepositoryId ,
89
+ })
90
+ }
91
+ return repos , nil
92
+ }
93
+
67
94
func (g * GithubProvider ) ListRepos (ctx context.Context , cloneProtocol string ) ([]* Repository , error ) {
68
95
opt := & github.RepositoryListByOrgOptions {
69
96
ListOptions : github.ListOptions {PerPage : 100 },
@@ -104,7 +131,7 @@ func (g *GithubProvider) ListRepos(ctx context.Context, cloneProtocol string) ([
104
131
105
132
func (g * GithubProvider ) RepoHasPath (ctx context.Context , repo * Repository , path string ) (bool , error ) {
106
133
_ , _ , resp , err := g .client .Repositories .GetContents (ctx , repo .Organization , repo .Repository , path , & github.RepositoryContentGetOptions {
107
- Ref : repo .Branch ,
134
+ Ref : repo .SHA ,
108
135
})
109
136
// 404s are not an error here, just a normal false.
110
137
if resp != nil && resp .StatusCode == 404 {
@@ -153,3 +180,33 @@ func (g *GithubProvider) listBranches(ctx context.Context, repo *Repository) ([]
153
180
}
154
181
return branches , nil
155
182
}
183
+
184
+ func (g * GithubProvider ) listPullRequests (ctx context.Context , repo * Repository ) ([]github.PullRequest , error ) {
185
+
186
+ if ! g .allPullRequests {
187
+ return nil , nil
188
+ }
189
+
190
+ opt := & github.PullRequestListOptions {
191
+ ListOptions : github.ListOptions {PerPage : 100 },
192
+ }
193
+
194
+ githubPullRequests := []github.PullRequest {}
195
+
196
+ for {
197
+ allPullRequests , resp , err := g .client .PullRequests .List (ctx , repo .Organization , repo .Repository , opt )
198
+ if err != nil {
199
+ return nil , err
200
+ }
201
+
202
+ for _ , pr := range allPullRequests {
203
+ githubPullRequests = append (githubPullRequests , * pr )
204
+ }
205
+
206
+ if resp .NextPage == 0 {
207
+ break
208
+ }
209
+ opt .Page = resp .NextPage
210
+ }
211
+ return githubPullRequests , nil
212
+ }
0 commit comments