-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathconvert.go
182 lines (158 loc) · 5.49 KB
/
convert.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
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitbucketdatacenter
import (
"fmt"
"net/url"
"strings"
bb "github.com/neticdk/go-bitbucket/bitbucket"
"golang.org/x/oauth2"
"go.woodpecker-ci.org/woodpecker/v3/server/model"
)
func convertStatus(status model.StatusValue) bb.BuildStatusState {
switch status {
case model.StatusPending, model.StatusRunning:
return bb.BuildStatusStateInProgress
case model.StatusSuccess:
return bb.BuildStatusStateSuccessful
default:
return bb.BuildStatusStateFailed
}
}
func convertID(id uint64) model.ForgeRemoteID {
return model.ForgeRemoteID(fmt.Sprintf("%d", id))
}
func anonymizeLink(link string) (href string) {
parsed, err := url.Parse(link)
if err != nil {
return link
}
parsed.User = nil
return parsed.String()
}
func convertRepo(from *bb.Repository, perm *model.Perm, branch string) *model.Repo {
r := &model.Repo{
ForgeRemoteID: convertID(from.ID),
Name: from.Slug,
Owner: from.Project.Key,
Branch: branch,
IsSCMPrivate: true, // Since we have to use Netrc it has to always be private :/ TODO: Is this really true?
FullName: fmt.Sprintf("%s/%s", from.Project.Key, from.Slug),
Perm: perm,
PREnabled: true,
}
for _, l := range from.Links["clone"] {
if l.Name == "http" {
r.Clone = anonymizeLink(l.Href)
}
}
if l, ok := from.Links["self"]; ok && len(l) > 0 {
r.ForgeURL = l[0].Href
}
return r
}
func convertRepositoryPushEvent(ev *bb.RepositoryPushEvent, baseURL string) *model.Pipeline {
if len(ev.Changes) == 0 {
return nil
}
change := ev.Changes[0]
if change.ToHash == "0000000000000000000000000000000000000000" {
// No ToHash present - could be "DELETE"
return nil
}
if change.Type == bb.RepositoryPushEventChangeTypeDelete {
return nil
}
pipeline := &model.Pipeline{
Commit: &model.Commit{
// message is set later
SHA: change.ToHash,
ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, ev.Repository.Project.Key, ev.Repository.Slug, change.ToHash),
},
Branch: change.Ref.DisplayID,
Avatar: bitbucketAvatarURL(baseURL, ev.Actor.Slug),
Author: ev.Actor.Name,
Ref: ev.Changes[0].RefId,
// TODO this is wrong on tags
ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, ev.Repository.Project.Key, ev.Repository.Slug, change.ToHash),
}
if strings.HasPrefix(ev.Changes[0].RefId, "refs/tags/") {
pipeline.Event = model.EventTag
} else {
pipeline.Event = model.EventPush
}
return pipeline
}
func convertPullRequestEvent(ev *bb.PullRequestEvent, baseURL string) *model.Pipeline {
pipeline := &model.Pipeline{
Commit: &model.Commit{
// message is set later
SHA: ev.PullRequest.Source.Latest,
ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, ev.PullRequest.Source.Repository.Project.Key, ev.PullRequest.Source.Repository.Slug, ev.PullRequest.Source.Latest),
},
Branch: ev.PullRequest.Source.DisplayID,
Avatar: bitbucketAvatarURL(baseURL, ev.Actor.Slug),
Author: ev.Actor.Name,
Ref: fmt.Sprintf("refs/pull-requests/%d/from", ev.PullRequest.ID),
PullRequest: convertPullRequest(&ev.PullRequest),
ForgeURL: fmt.Sprintf("%s/projects/%s/repos/%s/pull-requests/%d", baseURL, ev.PullRequest.Source.Repository.Project.Key, ev.PullRequest.Source.Repository.Slug, ev.PullRequest.ID),
Refspec: fmt.Sprintf("%s:%s", ev.PullRequest.Source.DisplayID, ev.PullRequest.Target.DisplayID),
}
if ev.EventKey == bb.EventKeyPullRequestMerged || ev.EventKey == bb.EventKeyPullRequestDeclined || ev.EventKey == bb.EventKeyPullRequestDeleted {
pipeline.Event = model.EventPullClosed
} else {
pipeline.Event = model.EventPull
}
return pipeline
}
func convertUser(user *bb.User, baseURL string) *model.User {
return &model.User{
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprintf("%d", user.ID)),
Login: user.Slug,
Email: user.Email,
Avatar: bitbucketAvatarURL(baseURL, user.Slug),
}
}
func bitbucketAvatarURL(baseURL, slug string) string {
return fmt.Sprintf("%s/users/%s/avatar.png", baseURL, slug)
}
func convertListOptions(p *model.ListOptions) bb.ListOptions {
if p.All {
return bb.ListOptions{}
}
return bb.ListOptions{Limit: uint(p.PerPage), Start: uint((p.Page - 1) * p.PerPage)}
}
func updateUserCredentials(u *model.User, t *oauth2.Token) {
u.AccessToken = t.AccessToken
u.RefreshToken = t.RefreshToken
u.Expiry = t.Expiry.UTC().Unix()
}
func convertPullRequest(pr *bb.PullRequest) *model.PullRequest {
return &model.PullRequest{
FromFork: pr.Source.Repository.ID != pr.Target.Repository.ID,
Title: pr.Title,
Index: model.ForgeRemoteID(fmt.Sprint(pr.ID)),
}
}
func convertProjectsToTeams(projects []*bb.Project, client *bb.Client) []*model.Team {
teams := make([]*model.Team, 0)
for _, project := range projects {
team := &model.Team{
Login: project.Key,
Avatar: fmt.Sprintf("%s/projects/%s/avatar.png", client.BaseURL, project.Key),
}
teams = append(teams, team)
}
return teams
}