forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
189 lines (164 loc) · 5.74 KB
/
gitlab.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
package drivers
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/pipeline/providers"
"github.com/rancher/rancher/pkg/pipeline/remote/model"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/xanzy/go-gitlab"
"io/ioutil"
"net/http"
"strings"
)
const (
GitlabWebhookHeader = "X-Gitlab-Event"
GitlabTokenHeader = "X-Gitlab-Token"
gitlabPushEvent = "Push Hook"
gitlabMREvent = "Merge Request Hook"
gitlabTagEvent = "Tag Push Hook"
gitlabActionOpen = "open"
gitlabActionUpdate = "update"
gitlabStateOpen = "opened"
)
type GitlabDriver struct {
PipelineLister v3.PipelineLister
PipelineExecutions v3.PipelineExecutionInterface
SourceCodeCredentialLister v3.SourceCodeCredentialLister
}
func (g GitlabDriver) Execute(req *http.Request) (int, error) {
var signature string
if signature = req.Header.Get(GitlabTokenHeader); len(signature) == 0 {
return http.StatusUnprocessableEntity, errors.New("gitlab webhook missing token")
}
event := req.Header.Get(GitlabWebhookHeader)
if event != gitlabPushEvent && event != gitlabMREvent && event != gitlabTagEvent {
return http.StatusUnprocessableEntity, fmt.Errorf("not trigger for event:%s", event)
}
pipelineID := req.URL.Query().Get("pipelineId")
ns, name := ref.Parse(pipelineID)
pipeline, err := g.PipelineLister.Get(ns, name)
if err != nil {
return http.StatusInternalServerError, err
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return http.StatusUnprocessableEntity, err
}
if pipeline.Status.Token != signature {
return http.StatusUnprocessableEntity, errors.New("gitlab webhook invalid token")
}
if pipeline.Status.PipelineState == "inactive" {
return http.StatusUnavailableForLegalReasons, errors.New("pipeline is not active")
}
if (event == gitlabPushEvent && !pipeline.Spec.TriggerWebhookPush) ||
(event == gitlabMREvent && !pipeline.Spec.TriggerWebhookPr) ||
(event == gitlabTagEvent && !pipeline.Spec.TriggerWebhookTag) {
return http.StatusUnavailableForLegalReasons, fmt.Errorf("trigger for event '%s' is disabled", event)
}
info := &model.BuildInfo{}
if event == gitlabPushEvent {
info, err = gitlabParsePushPayload(body)
if err != nil {
return http.StatusUnprocessableEntity, err
}
} else if event == gitlabMREvent {
info, err = gitlabParseMergeRequestPayload(body)
if err != nil {
return http.StatusUnprocessableEntity, err
}
} else if event == gitlabTagEvent {
info, err = gitlabParseTagPayload(body)
if err != nil {
return http.StatusUnprocessableEntity, err
}
}
pipelineConfig, err := providers.GetPipelineConfigByBranch(g.SourceCodeCredentialLister, pipeline, info.Branch)
if err != nil {
return http.StatusInternalServerError, err
}
if pipelineConfig == nil {
//no pipeline config to run
return http.StatusOK, nil
}
if !utils.Match(pipelineConfig.Branch, info.Branch) {
return http.StatusUnavailableForLegalReasons, fmt.Errorf("skipped branch '%s'", info.Branch)
}
if _, err := utils.GenerateExecution(g.PipelineExecutions, pipeline, pipelineConfig, info); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func gitlabParsePushPayload(raw []byte) (*model.BuildInfo, error) {
payload := &gitlab.PushEvent{}
if err := json.Unmarshal(raw, payload); err != nil {
return nil, err
}
info := &model.BuildInfo{}
if err := json.Unmarshal(raw, payload); err != nil {
return nil, err
}
info.TriggerType = utils.TriggerTypeWebhook
info.Event = utils.WebhookEventPush
info.Commit = payload.After
info.Branch = strings.TrimPrefix(payload.Ref, RefsBranchPrefix)
info.Ref = payload.Ref
info.HTMLLink = payload.Repository.HTTPURL
lastCommit := payload.Commits[len(payload.Commits)-1]
info.Message = lastCommit.Message
info.Email = payload.UserEmail
info.AvatarURL = payload.UserAvatar
info.Author = payload.UserName
info.Sender = payload.UserName
return info, nil
}
func gitlabParseMergeRequestPayload(raw []byte) (*model.BuildInfo, error) {
info := &model.BuildInfo{}
payload := &gitlab.MergeEvent{}
if err := json.Unmarshal(raw, payload); err != nil {
return nil, err
}
action := payload.ObjectAttributes.Action
oldRev := payload.ObjectAttributes.OldRev
if action != gitlabActionOpen && action != gitlabActionUpdate {
return nil, fmt.Errorf("no trigger for %s action", action)
}
if action == gitlabActionUpdate && oldRev == "" {
return nil, fmt.Errorf("no trigger for metadata update action")
}
if payload.ObjectAttributes.State != gitlabStateOpen {
return nil, fmt.Errorf("no trigger for closed merge requests")
}
info.TriggerType = utils.TriggerTypeWebhook
info.Event = utils.WebhookEventPullRequest
info.Branch = payload.ObjectAttributes.TargetBranch
info.Ref = fmt.Sprintf("refs/merge-requests/%d/head", payload.ObjectAttributes.IID)
info.HTMLLink = payload.ObjectAttributes.URL
info.Title = payload.ObjectAttributes.Title
info.Message = payload.ObjectAttributes.LastCommit.Message
info.Commit = payload.ObjectAttributes.LastCommit.ID
info.Author = payload.User.Name
info.AvatarURL = payload.User.AvatarURL
info.Email = payload.User.Email
info.Sender = payload.User.Name
return info, nil
}
func gitlabParseTagPayload(raw []byte) (*model.BuildInfo, error) {
info := &model.BuildInfo{}
payload := &gitlab.TagEvent{}
if err := json.Unmarshal(raw, payload); err != nil {
return nil, err
}
info.TriggerType = utils.TriggerTypeWebhook
info.Event = utils.WebhookEventTag
info.Ref = payload.Ref
tag := strings.TrimPrefix(payload.Ref, RefsTagPrefix)
info.Message = "tag " + tag
info.Branch = tag
info.Commit = payload.After
info.Author = payload.UserName
info.AvatarURL = payload.UserAvatar
return info, nil
}