-
Notifications
You must be signed in to change notification settings - Fork 168
/
webhooks.go
134 lines (124 loc) · 4.08 KB
/
webhooks.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
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/caicloud/nirvana/log"
"github.com/caicloud/cyclone/pkg/server/biz/scm"
)
const (
tag = "TAG"
branch = "BRANCH"
add = "ADD"
update = "UPDATE"
releaseRefPrefix = "refs/heads/release/"
// pullRefTemplate represents reference template for pull request.
pullRefTemplate = "refs/pull-requests/%d/merge"
)
var supportEventMap = map[string]bool{
RefsChanged: true,
PrOpened: true,
PrModified: true,
PrCommentAdded: true,
}
// EventPayload represents the event information.
type EventPayload struct {
EventKey string `json:"eventKey"`
Repository Repository `json:"repository"`
Changes []Change `json:"changes"`
PullRequest PullRequest `json:"pullRequest"`
Comment Comment `json:"comment"`
}
// Change represents the details of refs_changed.
type Change struct {
Ref struct {
ID string `json:"id"`
Type string `json:"type"`
DisplayID string `json:"displayId"`
} `json:"ref"`
RefID string `json:"refId"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
Type string `json:"type"`
}
// Comment represents the comment in the pull request.
type Comment struct {
Text string `json:"text"`
}
// ParseEvent parses data from Bitbucket Server events.
func ParseEvent(request *http.Request) *scm.EventData {
eventKey := request.Header.Get("X-Event-Key")
if !supportEventMap[eventKey] {
log.Errorf("unsupported bitbucket server event: %s", eventKey)
return nil
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Errorln(err)
return nil
}
var payload EventPayload
err = json.Unmarshal(body, &payload)
if err != nil {
log.Errorln(err)
return nil
}
switch eventKey {
case RefsChanged:
for _, change := range payload.Changes {
if change.Ref.Type == tag && change.Type == add {
return &scm.EventData{
Type: scm.TagReleaseEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.Repository.Project.Key), payload.Repository.Slug),
Ref: change.RefID,
}
} else if change.Ref.Type == branch && strings.HasPrefix(change.RefID, releaseRefPrefix) && change.Type == add {
return &scm.EventData{
Type: scm.TagReleaseEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.Repository.Project.Key), payload.Repository.Slug),
Ref: change.RefID,
}
} else if change.Type == update {
return &scm.EventData{
Type: scm.PushEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.Repository.Project.Key), payload.Repository.Slug),
Ref: change.RefID,
Branch: change.RefID,
}
}
}
case PrOpened:
return &scm.EventData{
Type: scm.PullRequestEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.PullRequest.ToRef.Repository.Project.Key), payload.PullRequest.ToRef.Repository.Slug),
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
Branch: payload.PullRequest.ToRef.DisplayID,
CreatedAt: time.Unix(payload.PullRequest.CreatedDate/1000, 0),
}
case PrModified:
return &scm.EventData{
Type: scm.PullRequestEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.PullRequest.ToRef.Repository.Project.Key), payload.PullRequest.ToRef.Repository.Slug),
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
CreatedAt: time.Unix(payload.PullRequest.UpdatedDate/1000, 0),
}
case PrCommentAdded:
return &scm.EventData{
Type: scm.PullRequestCommentEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.PullRequest.ToRef.Repository.Project.Key), payload.PullRequest.ToRef.Repository.Slug),
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
Comment: payload.Comment.Text,
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
CreatedAt: time.Unix(payload.PullRequest.CreatedDate/1000, 0),
}
default:
log.Warningln("Skip unsupported Bitbucket Server event")
return nil
}
return nil
}