/
github.go
252 lines (215 loc) · 7.44 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package github
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/beatlabs/ergo"
"github.com/google/go-github/v41/github"
"golang.org/x/oauth2"
)
// RepositoryClient for Github API.
type RepositoryClient struct {
organization string
repo string
client *github.Client
}
// NewGithubClient set up a github client.
func NewGithubClient(ctx context.Context, accessToken string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}
// NewRepositoryClient instantiate a RepositoryClient.
func NewRepositoryClient(organization, repo string, client *github.Client) *RepositoryClient {
return &RepositoryClient{
organization: organization,
repo: repo,
client: client,
}
}
// CreateDraftRelease creates a draft release.
func (gc *RepositoryClient) CreateDraftRelease(ctx context.Context, name, tagName, releaseBody, targetBranch string) error {
isDraft := true
githubRelease := &github.RepositoryRelease{
Name: &name,
TagName: &tagName,
Draft: &isDraft,
Body: &releaseBody,
TargetCommitish: &targetBranch,
}
_, _, err := gc.client.Repositories.CreateRelease(
ctx,
gc.organization,
gc.repo,
githubRelease,
)
return err
}
// LastRelease fetches the latest release for a repository.
func (gc *RepositoryClient) LastRelease(ctx context.Context) (*ergo.Release, error) {
githubRelease, _, err := gc.client.Repositories.GetLatestRelease(
ctx, gc.organization, gc.repo)
if err != nil {
var errorResponse *github.ErrorResponse
if errors.As(err, &errorResponse) && errorResponse.Response.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("latest release not found: %w", errorResponse)
}
return nil, err
}
return &ergo.Release{
ID: *githubRelease.ID,
Body: *githubRelease.Body,
TagName: githubRelease.GetTagName(),
ReleaseURL: githubRelease.GetHTMLURL(),
Draft: githubRelease.GetDraft(),
}, nil
}
// EditRelease allows to edit a repository release.
func (gc *RepositoryClient) EditRelease(ctx context.Context, release *ergo.Release) (*ergo.Release, error) {
if release == nil {
return nil, errors.New("nothing to release: input release is nil")
}
releasePayload := &github.RepositoryRelease{
Body: &release.Body,
}
githubRelease, _, err := gc.client.Repositories.EditRelease(ctx, gc.organization, gc.repo, release.ID, releasePayload)
if err != nil {
return nil, err
}
release.Body = githubRelease.GetBody()
return release, nil
}
// PublishRelease changes the draft flag of a repo release to false.
func (gc *RepositoryClient) PublishRelease(ctx context.Context, releaseID int64) error {
editReleasePayload := &github.RepositoryRelease{
Draft: github.Bool(false),
}
_, _, err := gc.client.Repositories.EditRelease(ctx, gc.organization, gc.repo, releaseID, editReleasePayload)
if err != nil {
return fmt.Errorf("editing draft flag of release (ID=%d): %w", releaseID, err)
}
return nil
}
// GetRef branch reference object given the branch name.
func (gc *RepositoryClient) GetRef(ctx context.Context, branch string) (*ergo.Reference, error) {
ref, _, err := gc.client.Git.GetRef(ctx, gc.organization, gc.repo, "refs/heads/"+branch)
if err != nil {
return nil, err
}
return &ergo.Reference{SHA: *ref.Object.SHA, Ref: *ref.Ref}, nil
}
// CreateTag given the version name.
func (gc *RepositoryClient) CreateTag(ctx context.Context, versionName, sha, m string) (*ergo.Tag, error) {
s := "commit"
tag := github.Tag{
Tag: &versionName,
Message: &m,
Object: &github.GitObject{Type: &s, SHA: &sha},
}
t, _, err := gc.client.Git.CreateTag(ctx, gc.organization, gc.repo, &tag)
errResponse, ok := err.(*github.ErrorResponse)
if ok && errResponse.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("error on tag creation: %v", err)
}
url := "tags/" + versionName
ref := github.Reference{
Object: &github.GitObject{
SHA: &sha,
},
Ref: &url,
}
_, _, err = gc.client.Git.CreateRef(ctx, gc.organization, gc.repo, &ref)
errResponse, ok = err.(*github.ErrorResponse)
if ok && errResponse.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("error on tag creation: %v", err)
}
return &ergo.Tag{Name: *t.Tag}, err
}
// CompareBranch compare the base branch with the given one.
func (gc *RepositoryClient) CompareBranch(ctx context.Context, baseBranch, branch string) (*ergo.StatusReport, error) {
commitsAhead, err := gc.commitsDiff(ctx, baseBranch, branch)
if err != nil {
return nil, err
}
commitsBehind, err := gc.commitsDiff(ctx, branch, baseBranch)
if err != nil {
return nil, err
}
return &ergo.StatusReport{Branch: branch, BaseBranch: baseBranch, Ahead: commitsAhead, Behind: commitsBehind}, nil
}
// commitsDiff finds the differences in commits between two branches.
func (gc *RepositoryClient) commitsDiff(ctx context.Context, baseBranch, branch string) ([]*ergo.Commit, error) {
comparison, _, err := gc.client.Repositories.CompareCommits(ctx, gc.organization, gc.repo, baseBranch, branch, &github.ListOptions{})
if err != nil {
return nil, err
}
var commitsAhead []*ergo.Commit
for _, commit := range comparison.Commits {
commitAhead := &ergo.Commit{Message: *commit.Commit.Message}
commitsAhead = append(commitsAhead, commitAhead)
}
return commitsAhead, nil
}
// DiffCommits is responsible to find the diff-commits and return a StatusReport for each of
// given releaseBranches.
func (gc *RepositoryClient) DiffCommits(ctx context.Context, releaseBranches []string, baseBranch string) ([]*ergo.StatusReport, error) {
var statusReports []*ergo.StatusReport
for _, branch := range releaseBranches {
statusReport, err := gc.CompareBranch(ctx, baseBranch, branch)
if err != nil {
return nil, fmt.Errorf("error comparing base branch %s %s:%s", baseBranch, branch, err)
}
statusReports = append(statusReports, statusReport)
}
return statusReports, nil
}
// UpdateBranchFromTag is responsible to update a branch from tag.
func (gc *RepositoryClient) UpdateBranchFromTag(ctx context.Context, tag, toBranch string, force bool) error {
ref, err := gc.getRefFromGitHub(ctx, tag)
if err != nil {
return err
}
branchRef := "heads/" + toBranch
ref.Ref = &branchRef
_, _, err = gc.client.Git.UpdateRef(ctx, gc.organization, gc.repo, ref, force)
if err != nil {
return fmt.Errorf("error on update branch from tag: %v", err)
}
return nil
}
// GetRefFromTag get reference from tag.
func (gc *RepositoryClient) GetRefFromTag(ctx context.Context, tag string) (*ergo.Reference, error) {
ref, err := gc.getRefFromGitHub(ctx, tag)
if err != nil {
return nil, err
}
if ref == nil {
return nil, nil
}
return &ergo.Reference{SHA: *ref.Object.SHA, Ref: *ref.Ref}, nil
}
// getRefFromGitHub get reference from github and returns the github.Reference object.
func (gc *RepositoryClient) getRefFromGitHub(ctx context.Context, tag string) (*github.Reference, error) {
ref, _, err := gc.client.Git.GetRef(ctx, gc.organization, gc.repo, "tags/"+tag)
errResponse, ok := err.(*github.ErrorResponse)
if ok && errResponse.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("error getting tag reference %v", err)
}
return ref, nil
}
// GetRepoName return the repository name.
func (gc *RepositoryClient) GetRepoName() string {
return gc.organization + "/" + gc.repo
}