This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
github.go
134 lines (123 loc) · 3.78 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package scm_provider
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v35/github"
"golang.org/x/oauth2"
)
type GithubProvider struct {
client *github.Client
organization string
allBranches bool
}
var _ SCMProviderService = &GithubProvider{}
func NewGithubProvider(ctx context.Context, organization string, token string, url string, allBranches bool) (*GithubProvider, error) {
var ts oauth2.TokenSource
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
if token != "" {
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
}
httpClient := oauth2.NewClient(ctx, ts)
var client *github.Client
if url == "" {
client = github.NewClient(httpClient)
} else {
var err error
client, err = github.NewEnterpriseClient(url, url, httpClient)
if err != nil {
return nil, err
}
}
return &GithubProvider{client: client, organization: organization, allBranches: allBranches}, nil
}
func (g *GithubProvider) ListRepos(ctx context.Context, cloneProtocol string) ([]*Repository, error) {
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
repos := []*Repository{}
for {
githubRepos, resp, err := g.client.Repositories.ListByOrg(ctx, g.organization, opt)
if err != nil {
return nil, fmt.Errorf("error listing repositories for %s: %v", g.organization, err)
}
for _, githubRepo := range githubRepos {
var url string
switch cloneProtocol {
// Default to SSH if unspecified (i.e. if "").
case "", "ssh":
url = githubRepo.GetSSHURL()
case "https":
url = githubRepo.GetCloneURL()
default:
return nil, fmt.Errorf("unknown clone protocol for GitHub %v", cloneProtocol)
}
branches, err := g.listBranches(ctx, githubRepo)
if err != nil {
return nil, fmt.Errorf("error listing branches for %s/%s: %v", githubRepo.Owner.GetLogin(), githubRepo.GetName(), err)
}
for _, branch := range branches {
repos = append(repos, &Repository{
Organization: githubRepo.Owner.GetLogin(),
Repository: githubRepo.GetName(),
URL: url,
Branch: branch.GetName(),
SHA: branch.GetCommit().GetSHA(),
Labels: githubRepo.Topics,
})
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return repos, nil
}
func (g *GithubProvider) RepoHasPath(ctx context.Context, repo *Repository, path string) (bool, error) {
_, _, resp, err := g.client.Repositories.GetContents(ctx, repo.Organization, repo.Repository, path, &github.RepositoryContentGetOptions{
Ref: repo.Branch,
})
// 404s are not an error here, just a normal false.
if resp != nil && resp.StatusCode == 404 {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (g *GithubProvider) listBranches(ctx context.Context, repo *github.Repository) ([]github.Branch, error) {
// If we don't specifically want to query for all branches, just use the default branch and call it a day.
if !g.allBranches {
defaultBranch, _, err := g.client.Repositories.GetBranch(ctx, repo.Owner.GetLogin(), repo.GetName(), repo.GetDefaultBranch())
if err != nil {
return nil, err
}
return []github.Branch{*defaultBranch}, nil
}
// Otherwise, scrape the ListBranches API.
opt := &github.BranchListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
branches := []github.Branch{}
for {
githubBranches, resp, err := g.client.Repositories.ListBranches(ctx, repo.Owner.GetLogin(), repo.GetName(), opt)
if err != nil {
return nil, err
}
for _, githubBranch := range githubBranches {
branches = append(branches, *githubBranch)
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return branches, nil
}