-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitea.go
291 lines (246 loc) · 6.66 KB
/
gitea.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package resource
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_gitea.go . Gitea
type Gitea interface {
ListPullRequests(gitea.StateType) ([]*PullRequest, error)
ListModifiedFiles(int64) ([]string, error)
PostComment(string, string) error
GetPullRequest(string, string) (*PullRequest, error)
UpdateCommitStatus(string, string, string, string, string, string) error
}
// GiteaClient for handling API requests.
type GiteaClient struct {
Client *gitea.Client
Repository string
Owner string
}
func NewGiteaClient(s *Source) (*GiteaClient, error) {
owner, repository, err := parseRepository(s.Repository)
if err != nil {
return nil, err
}
client, err := gitea.NewClient(s.Endpoint, gitea.SetToken(s.AccessToken))
if err != nil {
return nil, err
}
return &GiteaClient{
Client: client,
Repository: repository,
Owner: owner,
}, nil
}
func (manager *GiteaClient) ListPullRequests(prStateFilter gitea.StateType) ([]*PullRequest, error) {
var response []*PullRequest
count := 0
totalCount := -1
page := 1
for {
prs, httpresponse, err := manager.Client.ListRepoPullRequests(
manager.Owner,
manager.Repository,
gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
State: prStateFilter,
Sort: "recentupdate",
},
)
if err != nil {
return nil, fmt.Errorf("failed to list pull requests: %s", err)
}
for _, pr := range prs {
commit, err := manager.getLatestCommitForPR(pr.Index)
if err != nil {
return nil, fmt.Errorf("failed to get latest commit for PR: %s", err)
}
response = append(response, &PullRequest{
PullRequest: *pr,
Tip: *commit,
})
}
count += len(prs)
if page == 1 {
xTotalCount := httpresponse.Header.Get("x-total-count")
if xTotalCount == "" {
return nil, errors.New("missing x-total-count header in Gitea API response")
}
totalCount, err = strconv.Atoi(xTotalCount)
if err != nil {
return nil, errors.New("failed to parse x-total-count header in Gitea API response")
}
}
if count >= totalCount {
break
}
page += 1
}
return response, nil
}
func (manager *GiteaClient) ListModifiedFiles(prNum int64) ([]string, error) {
var files []string
count := 0
totalCount := -1
page := 1
for {
changedFiles, httpresponse, err := manager.Client.ListPullRequestFiles(
manager.Owner,
manager.Repository,
prNum,
gitea.ListPullRequestFilesOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
},
)
if err != nil {
return nil, fmt.Errorf("failed to list changed files in pull request: %s", err)
}
for _, file := range changedFiles {
files = append(files, file.Filename)
}
count += len(changedFiles)
if page == 1 {
xTotalCount := httpresponse.Header.Get("x-total-count")
if xTotalCount == "" {
return nil, errors.New("missing x-total-count header in Gitea API response")
}
totalCount, err = strconv.Atoi(xTotalCount)
if err != nil {
return nil, errors.New("failed to parse x-total-count header in Gitea API response")
}
}
if count >= totalCount {
break
}
page += 1
}
return files, nil
}
func (manager *GiteaClient) GetPullRequest(prNumber, commitRef string) (*PullRequest, error) {
prIndex, err := strconv.ParseInt(prNumber, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to convert pull request number to int: %s", err)
}
pr, _, err := manager.Client.GetPullRequest(manager.Owner, manager.Repository, prIndex)
if err != nil {
return nil, fmt.Errorf("failed to retrieve pull request: %s", err)
}
count := 0
page := 1
totalCount := -1
for {
commits, httpResponse, err := manager.Client.ListPullRequestCommits(
manager.Owner,
manager.Repository,
prIndex,
gitea.ListPullRequestCommitsOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 100,
},
},
)
if err != nil {
return nil, fmt.Errorf("failed to retrieve pull request commits: %s", err)
}
for _, commit := range commits {
if commit.SHA == commitRef {
return &PullRequest{
PullRequest: *pr,
Tip: *commit,
}, nil
}
}
count += len(commits)
if page == 1 {
xTotalCount := httpResponse.Header.Get("x-total-count")
if xTotalCount == "" {
return nil, errors.New("missing x-total-count header in Gitea API response")
}
totalCount, err = strconv.Atoi(xTotalCount)
if err != nil {
return nil, errors.New("failed to parse x-total-count header in Gitea API response")
}
}
if count >= totalCount {
break
}
page += 1
}
// Return an error if the commit was not found
return nil, fmt.Errorf("commit with ref '%s' does not exist", commitRef)
}
// PostComment to a pull request or issue.
func (manager *GiteaClient) PostComment(prNumber, comment string) error {
prNum, err := strconv.ParseInt(prNumber, 10, 64)
if err != nil {
return fmt.Errorf("failed to convert pull request number to int: %s", err)
}
_, _, err = manager.Client.CreateIssueComment(
manager.Owner,
manager.Repository,
prNum,
gitea.CreateIssueCommentOption{
Body: comment,
},
)
return err
}
// UpdateCommitStatus for a given commit (not supported by V4 API).
func (manager *GiteaClient) UpdateCommitStatus(commitRef, baseContext, statusContext, status, targetURL, description string) error {
if baseContext == "" {
baseContext = "concourse-ci"
}
if statusContext == "" {
statusContext = "status"
}
if targetURL == "" {
targetURL = strings.Join([]string{os.Getenv("ATC_EXTERNAL_URL"), "builds", os.Getenv("BUILD_ID")}, "/")
}
if description == "" {
description = fmt.Sprintf("Concourse CI build %s", status)
}
_, _, err := manager.Client.CreateStatus(
manager.Owner,
manager.Repository,
commitRef,
gitea.CreateStatusOption{
State: gitea.StatusState(strings.ToLower(status)),
TargetURL: targetURL,
Description: description,
Context: path.Join(baseContext, statusContext),
})
return err
}
func (manager GiteaClient) getLatestCommitForPR(prIndex int64) (*gitea.Commit, error) {
commits, _, err := manager.Client.ListPullRequestCommits(
manager.Owner,
manager.Repository,
prIndex,
gitea.ListPullRequestCommitsOptions{
ListOptions: gitea.ListOptions{},
},
)
if err != nil {
return nil, fmt.Errorf("failed to retrieve pull request commits: %s", err)
}
return commits[0], nil
}
func parseRepository(s string) (string, string, error) {
parts := strings.Split(s, "/")
if len(parts) != 2 {
return "", "", errors.New("malformed repository string")
}
return parts[0], parts[1], nil
}