/
connector.go
136 lines (123 loc) · 4.42 KB
/
connector.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
package gitea
import (
"context"
"fmt"
"net/url"
"code.gitea.io/sdk/gitea"
"github.com/git-town/git-town/v11/src/config"
"github.com/git-town/git-town/v11/src/domain"
"github.com/git-town/git-town/v11/src/git/giturl"
"github.com/git-town/git-town/v11/src/hosting/common"
"github.com/git-town/git-town/v11/src/messages"
"golang.org/x/oauth2"
)
type Connector struct {
client *gitea.Client
common.Config
log common.Log
}
func (self *Connector) DefaultProposalMessage(proposal domain.Proposal) string {
return fmt.Sprintf("%s (#%d)", proposal.Title, proposal.Number)
}
func (self *Connector) FindProposal(branch, target domain.LocalBranchName) (*domain.Proposal, error) {
openPullRequests, _, err := self.client.ListRepoPullRequests(self.Organization, self.Repository, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
PageSize: 50,
},
State: gitea.StateOpen,
})
if err != nil {
return nil, err
}
pullRequests := FilterPullRequests(openPullRequests, self.Organization, branch, target)
if len(pullRequests) == 0 {
return nil, nil //nolint:nilnil
}
if len(pullRequests) > 1 {
return nil, fmt.Errorf(messages.ProposalMultipleFound, len(pullRequests), branch, target)
}
pullRequest := pullRequests[0]
return &domain.Proposal{
MergeWithAPI: pullRequest.Mergeable,
Number: int(pullRequest.Index),
Target: domain.NewLocalBranchName(pullRequest.Base.Ref),
Title: pullRequest.Title,
}, nil
}
func (self *Connector) HostingServiceName() string {
return "Gitea"
}
func (self *Connector) NewProposalURL(branch, parentBranch domain.LocalBranchName) (string, error) {
toCompare := parentBranch.String() + "..." + branch.String()
return fmt.Sprintf("%s/compare/%s", self.RepositoryURL(), url.PathEscape(toCompare)), nil
}
func (self *Connector) RepositoryURL() string {
return fmt.Sprintf("https://%s/%s/%s", self.HostnameWithStandardPort(), self.Organization, self.Repository)
}
func (self *Connector) SquashMergeProposal(number int, message string) (mergeSHA domain.SHA, err error) {
if number <= 0 {
return domain.EmptySHA(), fmt.Errorf(messages.ProposalNoNumberGiven)
}
title, body := common.CommitMessageParts(message)
_, _, err = self.client.MergePullRequest(self.Organization, self.Repository, int64(number), gitea.MergePullRequestOption{
Style: gitea.MergeStyleSquash,
Title: title,
Message: body,
})
if err != nil {
return domain.EmptySHA(), err
}
pullRequest, _, err := self.client.GetPullRequest(self.Organization, self.Repository, int64(number))
if err != nil {
return domain.EmptySHA(), err
}
return domain.NewSHA(*pullRequest.MergedCommitID), nil
}
func (self *Connector) UpdateProposalTarget(_ int, _ domain.LocalBranchName) error {
// TODO: update the client and uncomment
// if self.log != nil {
// self.log(message.HostingGiteaUpdateBasebranchViaAPI, number, target)
// }
// _, err := self.client.EditPullRequest(self.owner, self.repository, int64(number), gitea.EditPullRequestOption{
// Base: newBaseName,
// })
// return err
return fmt.Errorf(messages.HostingGiteaNotImplemented)
}
func FilterPullRequests(pullRequests []*gitea.PullRequest, organization string, branch, target domain.LocalBranchName) []*gitea.PullRequest {
result := []*gitea.PullRequest{}
headName := organization + "/" + branch.String()
for p := range pullRequests {
pullRequest := pullRequests[p]
if pullRequest.Head.Name == headName && pullRequest.Base.Name == target.String() {
result = append(result, pullRequest)
}
}
return result
}
// NewGiteaConfig provides Gitea configuration data if the current repo is hosted on Gitea,
// otherwise nil.
func NewConnector(args NewConnectorArgs) (*Connector, error) {
if args.OriginURL == nil || (args.OriginURL.Host != "gitea.com" && args.HostingService != config.HostingGitea) {
return nil, nil //nolint:nilnil
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: args.APIToken})
httpClient := oauth2.NewClient(context.Background(), tokenSource)
giteaClient := gitea.NewClientWithHTTP(fmt.Sprintf("https://%s", args.OriginURL.Host), httpClient)
return &Connector{
client: giteaClient,
Config: common.Config{
APIToken: args.APIToken,
Hostname: args.OriginURL.Host,
Organization: args.OriginURL.Org,
Repository: args.OriginURL.Repo,
},
log: args.Log,
}, nil
}
type NewConnectorArgs struct {
OriginURL *giturl.Parts
HostingService config.Hosting
APIToken string
Log common.Log
}