@@ -4,20 +4,22 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "strconv"
7
8
8
9
"github.com/google/go-github/v35/github"
9
10
"golang.org/x/oauth2"
10
11
)
11
12
12
13
type GithubProvider struct {
13
- client * github.Client
14
- organization string
15
- allBranches bool
14
+ client * github.Client
15
+ organization string
16
+ allBranches bool
17
+ allPullRequests bool
16
18
}
17
19
18
20
var _ SCMProviderService = & GithubProvider {}
19
21
20
- 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 ) {
21
23
var ts oauth2.TokenSource
22
24
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
23
25
if token == "" {
@@ -39,7 +41,8 @@ func NewGithubProvider(ctx context.Context, organization string, token string, u
39
41
return nil , err
40
42
}
41
43
}
42
- return & GithubProvider {client : client , organization : organization , allBranches : allBranches }, nil
44
+
45
+ return & GithubProvider {client : client , organization : organization , allBranches : allBranches , allPullRequests : allPullRequests }, nil
43
46
}
44
47
45
48
func (g * GithubProvider ) ListRepos (ctx context.Context , cloneProtocol string ) ([]* Repository , error ) {
@@ -79,6 +82,24 @@ func (g *GithubProvider) ListRepos(ctx context.Context, cloneProtocol string) ([
79
82
Labels : githubRepo .Topics ,
80
83
})
81
84
}
85
+
86
+ if g .allPullRequests {
87
+ pullRequests , err := g .listPullRequests (ctx , githubRepo )
88
+ if err != nil {
89
+ return nil , fmt .Errorf ("error listing pull requests for %s/%s: %v" , githubRepo .Owner .GetLogin (), githubRepo .GetName (), err )
90
+ }
91
+
92
+ for _ , pr := range pullRequests {
93
+ repos = append (repos , & Repository {
94
+ Organization : githubRepo .Owner .GetLogin (),
95
+ Repository : githubRepo .GetName (),
96
+ URL : url ,
97
+ Branch : strconv .FormatInt (int64 (pr .GetNumber ()), 10 ), // PR number is an int
98
+ SHA : pr .GetHead ().GetSHA (),
99
+ Labels : githubRepo .Topics ,
100
+ })
101
+ }
102
+ }
82
103
}
83
104
if resp .NextPage == 0 {
84
105
break
@@ -90,7 +111,7 @@ func (g *GithubProvider) ListRepos(ctx context.Context, cloneProtocol string) ([
90
111
91
112
func (g * GithubProvider ) RepoHasPath (ctx context.Context , repo * Repository , path string ) (bool , error ) {
92
113
_ , _ , resp , err := g .client .Repositories .GetContents (ctx , repo .Organization , repo .Repository , path , & github.RepositoryContentGetOptions {
93
- Ref : repo .Branch ,
114
+ Ref : repo .SHA ,
94
115
})
95
116
// 404s are not an error here, just a normal false.
96
117
if resp != nil && resp .StatusCode == 404 {
@@ -132,3 +153,29 @@ func (g *GithubProvider) listBranches(ctx context.Context, repo *github.Reposito
132
153
}
133
154
return branches , nil
134
155
}
156
+
157
+ func (g * GithubProvider ) listPullRequests (ctx context.Context , repo * github.Repository ) ([]github.PullRequest , error ) {
158
+ opt := & github.PullRequestListOptions {
159
+ ListOptions : github.ListOptions {PerPage : 100 },
160
+ }
161
+
162
+ pullRequests := []github.PullRequest {}
163
+
164
+ for {
165
+ allPullRequests , resp , err := g .client .PullRequests .List (ctx , repo .Owner .GetLogin (), repo .GetName (), opt )
166
+ if err != nil {
167
+ return nil , err
168
+ }
169
+
170
+ for _ , pr := range allPullRequests {
171
+ pullRequests = append (pullRequests , * pr )
172
+ }
173
+
174
+ if resp .NextPage == 0 {
175
+ break
176
+ }
177
+ opt .Page = resp .NextPage
178
+ }
179
+
180
+ return pullRequests , nil
181
+ }
0 commit comments