forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
229 lines (202 loc) · 5.89 KB
/
hook.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
package coding
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/drone/drone/model"
)
const (
hookEvent = "X-Coding-Event"
hookPush = "push"
hookPR = "pull_request"
hookMR = "merge_request"
)
type User struct {
GlobalKey string `json:"global_key"`
Avatar string `json:"avatar"`
}
type Repository struct {
Name string `json:"name"`
HttpsURL string `json:"https_url"`
SshURL string `json:"ssh_url"`
WebURL string `json:"web_url"`
Owner *User `json:"owner"`
}
type Committer struct {
Email string `json:"email"`
Name string `json:"name"`
}
type Commit struct {
SHA string `json:"sha"`
ShortMessage string `json:"short_message"`
Committer *Committer `json:"committer"`
}
type PullRequest MergeRequest
type MergeRequest struct {
SourceBranch string `json:"source_branch"`
TargetBranch string `json:"target_branch"`
CommitSHA string `json:"merge_commit_sha"`
Status string `json:"status"`
Action string `json:"action"`
Number float64 `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
WebURL string `json:"web_url"`
User *User `json:"user"`
}
type PushHook struct {
Event string `json:"event"`
Repository *Repository `json:"repository"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Commits []*Commit `json:"commits"`
User *User `json:"user"`
}
type PullRequestHook struct {
Event string `json:"event"`
Repository *Repository `json:"repository"`
PullRequest *PullRequest `json:"pull_request"`
}
type MergeRequestHook struct {
Event string `json:"event"`
Repository *Repository `json:"repository"`
MergeRequest *MergeRequest `json:"merge_request"`
}
func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
raw, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return nil, nil, err
}
switch r.Header.Get(hookEvent) {
case hookPush:
return parsePushHook(raw)
case hookPR:
return parsePullRequestHook(raw)
case hookMR:
return parseMergeReuqestHook(raw)
}
return nil, nil, nil
}
func findLastCommit(commits []*Commit, sha string) *Commit {
var lastCommit *Commit
for _, commit := range commits {
if commit.SHA == sha {
lastCommit = commit
break
}
}
if lastCommit == nil {
lastCommit = &Commit{}
}
if lastCommit.Committer == nil {
lastCommit.Committer = &Committer{}
}
return lastCommit
}
func convertRepository(repo *Repository) (*model.Repo, error) {
// tricky stuff for a team project without a team owner instead of a user owner
re := regexp.MustCompile(`git@.+:([^/]+)/.+\.git`)
matches := re.FindStringSubmatch(repo.SshURL)
if len(matches) != 2 {
return nil, fmt.Errorf("Unable to resolve owner from ssh url %q", repo.SshURL)
}
return &model.Repo{
Owner: matches[1],
Name: repo.Name,
FullName: projectFullName(repo.Owner.GlobalKey, repo.Name),
Link: repo.WebURL,
Kind: model.RepoGit,
}, nil
}
func parsePushHook(raw []byte) (*model.Repo, *model.Build, error) {
hook := &PushHook{}
err := json.Unmarshal(raw, hook)
if err != nil {
return nil, nil, err
}
// no build triggered when removing ref
if hook.After == "0000000000000000000000000000000000000000" {
return nil, nil, nil
}
repo, err := convertRepository(hook.Repository)
if err != nil {
return nil, nil, err
}
lastCommit := findLastCommit(hook.Commits, hook.After)
build := &model.Build{
Event: model.EventPush,
Commit: hook.After,
Ref: hook.Ref,
Link: fmt.Sprintf("%s/git/commit/%s", hook.Repository.WebURL, hook.After),
Branch: strings.Replace(hook.Ref, "refs/heads/", "", -1),
Message: lastCommit.ShortMessage,
Email: lastCommit.Committer.Email,
Avatar: hook.User.Avatar,
Author: hook.User.GlobalKey,
Remote: hook.Repository.HttpsURL,
}
return repo, build, nil
}
func parsePullRequestHook(raw []byte) (*model.Repo, *model.Build, error) {
hook := &PullRequestHook{}
err := json.Unmarshal(raw, hook)
if err != nil {
return nil, nil, err
}
if hook.PullRequest.Status != "CANMERGE" ||
(hook.PullRequest.Action != "create" && hook.PullRequest.Action != "synchronize") {
return nil, nil, nil
}
repo, err := convertRepository(hook.Repository)
if err != nil {
return nil, nil, err
}
build := &model.Build{
Event: model.EventPull,
Commit: hook.PullRequest.CommitSHA,
Link: hook.PullRequest.WebURL,
Ref: fmt.Sprintf("refs/pull/%d/MERGE", int(hook.PullRequest.Number)),
Branch: hook.PullRequest.TargetBranch,
Message: hook.PullRequest.Body,
Author: hook.PullRequest.User.GlobalKey,
Avatar: hook.PullRequest.User.Avatar,
Title: hook.PullRequest.Title,
Remote: hook.Repository.HttpsURL,
Refspec: fmt.Sprintf("%s:%s", hook.PullRequest.SourceBranch, hook.PullRequest.TargetBranch),
}
return repo, build, nil
}
func parseMergeReuqestHook(raw []byte) (*model.Repo, *model.Build, error) {
hook := &MergeRequestHook{}
err := json.Unmarshal(raw, hook)
if err != nil {
return nil, nil, err
}
if hook.MergeRequest.Status != "CANMERGE" ||
(hook.MergeRequest.Action != "create" && hook.MergeRequest.Action != "synchronize") {
return nil, nil, nil
}
repo, err := convertRepository(hook.Repository)
if err != nil {
return nil, nil, err
}
build := &model.Build{
Event: model.EventPull,
Commit: hook.MergeRequest.CommitSHA,
Link: hook.MergeRequest.WebURL,
Ref: fmt.Sprintf("refs/merge/%d/MERGE", int(hook.MergeRequest.Number)),
Branch: hook.MergeRequest.TargetBranch,
Message: hook.MergeRequest.Body,
Author: hook.MergeRequest.User.GlobalKey,
Avatar: hook.MergeRequest.User.Avatar,
Title: hook.MergeRequest.Title,
Remote: hook.Repository.HttpsURL,
Refspec: fmt.Sprintf("%s:%s", hook.MergeRequest.SourceBranch, hook.MergeRequest.TargetBranch),
}
return repo, build, nil
}