-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathrepository_utils.go
More file actions
135 lines (112 loc) · 3.3 KB
/
repository_utils.go
File metadata and controls
135 lines (112 loc) · 3.3 KB
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
135
package github
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/v67/github"
)
// checkRepositoryBranchExists tests if a branch exists in a repository.
func checkRepositoryBranchExists(client *github.Client, owner, repo, branch string) error {
ctx := context.WithValue(context.Background(), ctxId, buildTwoPartID(repo, branch))
_, _, err := client.Repositories.GetBranch(ctx, owner, repo, branch, 2)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
return fmt.Errorf("branch %s not found in repository %s/%s or repository is not readable", branch, owner, repo)
}
}
return err
}
return nil
}
func getFileCommit(client *github.Client, owner, repo, file, branch string) (*github.RepositoryCommit, error) {
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/%s", repo, file))
opts := &github.CommitsListOptions{
SHA: branch,
Path: file,
}
allCommits := []*github.RepositoryCommit{}
for {
commits, resp, err := client.Repositories.ListCommits(ctx, owner, repo, opts)
if err != nil {
return nil, err
}
allCommits = append(allCommits, commits...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, c := range allCommits {
sha := c.GetSHA()
// Skip merge commits
if strings.Contains(c.Commit.GetMessage(), "Merge branch") {
continue
}
opts := &github.ListOptions{}
allFiles := []*github.CommitFile{}
var rc *github.RepositoryCommit
var resp *github.Response
var err error
for {
rc, resp, err = client.Repositories.GetCommit(ctx, owner, repo, sha, opts)
if err != nil {
return nil, err
}
allFiles = append(allFiles, rc.Files...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, f := range allFiles {
if f.GetFilename() == file && f.GetStatus() != "removed" {
return rc, nil
}
}
}
return nil, fmt.Errorf("cannot find file %s in repo %s/%s", file, owner, repo)
}
// getAutolinkByKeyPrefix returns a single autolink reference by key prefix that was configured for the given repository.
func getAutolinkByKeyPrefix(client *github.Client, owner, repo, keyPrefix string) (*github.Autolink, error) {
autolinks, err := listAutolinks(client, owner, repo)
if err != nil {
return nil, err
}
for _, autolink := range autolinks {
if *autolink.KeyPrefix == keyPrefix {
return autolink, nil
}
}
return nil, fmt.Errorf("cannot find autolink reference %s in repo %s/%s", keyPrefix, owner, repo)
}
// listAutolinks returns all autolink references for the given repository.
func listAutolinks(client *github.Client, owner, repo string) ([]*github.Autolink, error) {
ctx := context.WithValue(context.Background(), ctxId, fmt.Sprintf("%s/%s", owner, repo))
opts := &github.ListOptions{
PerPage: maxPerPage,
}
var allAutolinks []*github.Autolink
for {
autolinks, resp, err := client.Repositories.ListAutolinks(ctx, owner, repo, opts)
if err != nil {
return nil, err
}
allAutolinks = append(allAutolinks, autolinks...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return allAutolinks, nil
}
// get the list of retriable errors
func getDefaultRetriableErrors() map[int]bool {
return map[int]bool{
500: true,
502: true,
503: true,
504: true,
}
}