/
gitea.go
190 lines (175 loc) · 5.83 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
package hosting
import (
"context"
"fmt"
"net/url"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/git-town/git-town/v7/src/giturl"
"golang.org/x/oauth2"
)
// GiteaDriver provides access to the API of Gitea installations.
type GiteaDriver struct {
apiToken string
client *gitea.Client
hostname string
log logFn
originURL string
owner string
repository string
}
// NewGiteaDriver provides a Gitea driver instance if the given repo configuration is for a Gitea repo,
// otherwise nil.
func NewGiteaDriver(url giturl.Parts, config config, log logFn) *GiteaDriver {
driverType := config.HostingService()
manualHostName := config.OriginOverride()
if manualHostName != "" {
url.Host = manualHostName
}
if driverType != "gitea" && url.Host != "gitea.com" {
return nil
}
return &GiteaDriver{
originURL: config.OriginURL(),
hostname: url.Host,
apiToken: config.GiteaToken(),
log: log,
owner: url.Org,
repository: url.Repo,
}
}
func (d *GiteaDriver) LoadPullRequestInfo(branch, parentBranch string) (PullRequestInfo, error) {
if d.apiToken == "" {
return PullRequestInfo{}, nil
}
d.connect()
openPullRequests, err := d.client.ListRepoPullRequests(d.owner, d.repository, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
PageSize: 50,
},
State: gitea.StateOpen,
})
if err != nil {
return PullRequestInfo{}, err
}
baseName := parentBranch
headName := d.owner + "/" + branch
pullRequests := filterPullRequests(openPullRequests, baseName, headName)
if len(pullRequests) != 1 {
return PullRequestInfo{}, nil
}
pullRequest := pullRequests[0]
if !pullRequest.Mergeable {
return PullRequestInfo{}, nil
}
result := PullRequestInfo{
CanMergeWithAPI: true,
DefaultCommitMessage: createDefaultCommitMessage(pullRequest),
PullRequestNumber: pullRequest.Index,
}
return result, nil
}
func (d *GiteaDriver) NewPullRequestURL(branch string, parentBranch string) (string, error) {
toCompare := parentBranch + "..." + branch
return fmt.Sprintf("%s/compare/%s", d.RepositoryURL(), url.PathEscape(toCompare)), nil
}
func (d *GiteaDriver) RepositoryURL() string {
return fmt.Sprintf("https://%s/%s/%s", d.hostname, d.owner, d.repository)
}
func (d *GiteaDriver) HostingServiceName() string {
return "Gitea"
}
//nolint:nonamedreturns // return value isn't obvious from function name
func (d *GiteaDriver) MergePullRequest(options MergePullRequestOptions) (mergeSha string, err error) {
d.connect()
openPullRequests, err := d.client.ListRepoPullRequests(d.owner, d.repository, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
PageSize: 50,
},
State: gitea.StateOpen,
})
if err != nil {
return "", err
}
baseName := options.Branch
newBaseName := options.ParentBranch
err = d.apiRetargetPullRequests(filterPullRequests(openPullRequests, baseName, ""), newBaseName)
if err != nil {
return "", err
}
commitMessageParts := strings.SplitN(options.CommitMessage, "\n", 2)
commitTitle := commitMessageParts[0]
commitMessage := ""
if len(commitMessageParts) == 2 {
commitMessage = commitMessageParts[1]
}
return d.apiMergePullRequest(options.PullRequestNumber, commitTitle, commitMessage)
}
// Helper
func (d *GiteaDriver) connect() {
if d.client == nil {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: d.apiToken},
)
tc := oauth2.NewClient(context.Background(), ts)
d.client = gitea.NewClientWithHTTP(fmt.Sprintf("https://%s", d.hostname), tc)
}
}
func createDefaultCommitMessage(pullRequest *gitea.PullRequest) string {
return fmt.Sprintf("%s (#%d)", pullRequest.Title, pullRequest.Index)
}
func filterPullRequests(pullRequests []*gitea.PullRequest, baseName, headName string) []*gitea.PullRequest {
pullRequestsFiltered := []*gitea.PullRequest{}
for _, pullRequest := range pullRequests {
if pullRequest.Base.Name != baseName {
break
}
if headName != "" && pullRequest.Head.Name != headName {
break
}
pullRequestsFiltered = append(pullRequestsFiltered, pullRequest)
}
return pullRequestsFiltered
}
//nolint:nonamedreturns // return value isn't obvious from function name
func (d *GiteaDriver) apiMergePullRequest(pullRequestNumber int64, commitTitle, commitMessage string) (mergeSha string, err error) {
_, err = d.client.MergePullRequest(d.owner, d.repository, pullRequestNumber, gitea.MergePullRequestOption{
Style: gitea.MergeStyleSquash,
Title: commitTitle,
Message: commitMessage,
})
if err != nil {
return "", err
}
pullRequest, err := d.client.GetPullRequest(d.owner, d.repository, pullRequestNumber)
if err != nil {
return "", err
}
return *pullRequest.MergedCommitID, nil
}
// retargetPullRequests retargets pullrequests onto a new base branch
// this comes in handy when an ancestor got merged, so that children can be retargeted to the ancestor's own target branch
// example:
//
// ancestor -> initial
// children1 -> ancestor --> initial (retargeted to initial after merge)
// children2 -> ancestor --> initial (retargeted to initial after merge)
//
//nolint:unparam
func (d *GiteaDriver) apiRetargetPullRequests(pullRequests []*gitea.PullRequest, newBaseName string) error {
for _, pullRequest := range pullRequests {
// RE-ENABLE AFTER https://github.com/go-gitea/gitea/issues/11552 and remove the nolint above
// if options.LogRequests {
// helpers.PrintLog(fmt.Sprintf("Gitea API: Updating base branch for PR #%d to #%s", *pullRequest.Index, newBaseName))
// }
d.log("Gitea API: Updating base branch for PR #%d to #%s", 1, newBaseName)
d.log("The Gitea API currently does not support retargeting, please restarget #%d manually, see https://github.com/go-gitea/gitea/issues/11552", pullRequest.Index)
// _, err = d.client.EditPullRequest(d.owner, d.repository, *pullRequest.Index, &gitea.EditPullRequestOption{
// Base: newBaseName
// })
// if err != nil {
// return err
// }
}
return nil
}