- Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathrepo_hook.go
345 lines (291 loc) · 10.4 KB
/
repo_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gogs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
var (
ErrInvalidReceiveHook=errors.New("invalid JSON payload received over webhook")
)
typeHookstruct {
IDint64`json:"id"`
Typestring`json:"type"`
URLstring`json:"-"`
Configmap[string]string`json:"config"`
Events []string`json:"events"`
Activebool`json:"active"`
Updated time.Time`json:"updated_at"`
Created time.Time`json:"created_at"`
}
func (c*Client) ListRepoHooks(user, repostring) ([]*Hook, error) {
hooks:=make([]*Hook, 0, 10)
returnhooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
}
typeCreateHookOptionstruct {
Typestring`json:"type" binding:"Required"`
Configmap[string]string`json:"config" binding:"Required"`
Events []string`json:"events"`
Activebool`json:"active"`
}
func (c*Client) CreateRepoHook(user, repostring, optCreateHookOption) (*Hook, error) {
body, err:=json.Marshal(&opt)
iferr!=nil {
returnnil, err
}
h:=new(Hook)
returnh, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
}
typeEditHookOptionstruct {
Configmap[string]string`json:"config"`
Events []string`json:"events"`
Active*bool`json:"active"`
}
func (c*Client) EditRepoHook(user, repostring, idint64, optEditHookOption) error {
body, err:=json.Marshal(&opt)
iferr!=nil {
returnerr
}
_, err=c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
returnerr
}
func (c*Client) DeleteRepoHook(user, repostring, idint64) error {
_, err:=c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
returnerr
}
typePayloaderinterface {
JSONPayload() ([]byte, error)
}
typePayloadUserstruct {
Namestring`json:"name"`
Emailstring`json:"email"`
UserNamestring`json:"username"`
}
// FIXME: consider use same format as API when commits API are added.
typePayloadCommitstruct {
IDstring`json:"id"`
Messagestring`json:"message"`
URLstring`json:"url"`
Author*PayloadUser`json:"author"`
Committer*PayloadUser`json:"committer"`
Added []string`json:"added"`
Removed []string`json:"removed"`
Modified []string`json:"modified"`
Timestamp time.Time`json:"timestamp"`
}
var (
_Payloader=&CreatePayload{}
_Payloader=&DeletePayload{}
_Payloader=&ForkPayload{}
_Payloader=&PushPayload{}
_Payloader=&IssuesPayload{}
_Payloader=&IssueCommentPayload{}
_Payloader=&PullRequestPayload{}
)
// _________ __
// \_ ___ \_______ ____ _____ _/ |_ ____
// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
// \ \____| | \/\ ___/ / __ \| | \ ___/
// \______ /|__| \___ >____ /__| \___ >
// \/ \/ \/ \/
typeCreatePayloadstruct {
Refstring`json:"ref"`
RefTypestring`json:"ref_type"`
Shastring`json:"sha"`
DefaultBranchstring`json:"default_branch"`
Repo*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*CreatePayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// ParseCreateHook parses create event hook content.
funcParseCreateHook(raw []byte) (*CreatePayload, error) {
hook:=new(CreatePayload)
iferr:=json.Unmarshal(raw, hook); err!=nil {
returnnil, err
}
// it is possible the JSON was parsed, however,
// was not from Gogs (maybe was from Bitbucket)
// So we'll check to be sure certain key fields
// were populated
switch {
casehook.Repo==nil:
returnnil, ErrInvalidReceiveHook
caselen(hook.Ref) ==0:
returnnil, ErrInvalidReceiveHook
}
returnhook, nil
}
// ________ .__ __
// \______ \ ____ | | _____/ |_ ____
// | | \_/ __ \| | _/ __ \ __\/ __ \
// | ` \ ___/| |_\ ___/| | \ ___/
// /_______ /\___ >____/\___ >__| \___ >
// \/ \/ \/ \/
typePusherTypestring
const (
PUSHER_TYPE_USERPusherType="user"
)
typeDeletePayloadstruct {
Refstring`json:"ref"`
RefTypestring`json:"ref_type"`
PusherTypePusherType`json:"pusher_type"`
Repo*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*DeletePayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// ___________ __
// \_ _____/__________| | __
// | __)/ _ \_ __ \ |/ /
// | \( <_> ) | \/ <
// \___ / \____/|__| |__|_ \
// \/ \/
typeForkPayloadstruct {
Forkee*Repository`json:"forkee"`
Repo*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*ForkPayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// __________ .__
// \______ \__ __ _____| |__
// | ___/ | \/ ___/ | \
// | | | | /\___ \| Y \
// |____| |____//____ >___| /
// \/ \/
// PushPayload represents a payload information of push event.
typePushPayloadstruct {
Refstring`json:"ref"`
Beforestring`json:"before"`
Afterstring`json:"after"`
CompareURLstring`json:"compare_url"`
Commits []*PayloadCommit`json:"commits"`
Repo*Repository`json:"repository"`
Pusher*User`json:"pusher"`
Sender*User`json:"sender"`
}
func (p*PushPayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// ParsePushHook parses push event hook content.
funcParsePushHook(raw []byte) (*PushPayload, error) {
hook:=new(PushPayload)
iferr:=json.Unmarshal(raw, hook); err!=nil {
returnnil, err
}
switch {
casehook.Repo==nil:
returnnil, ErrInvalidReceiveHook
caselen(hook.Ref) ==0:
returnnil, ErrInvalidReceiveHook
}
returnhook, nil
}
// Branch returns branch name from a payload
func (p*PushPayload) Branch() string {
returnstrings.Replace(p.Ref, "refs/heads/", "", -1)
}
// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \
// | |\___ \ \___ \| | /\ ___/
// |___/____ >____ >____/ \___ >
// \/ \/ \/
typeHookIssueActionstring
const (
HOOK_ISSUE_OPENEDHookIssueAction="opened"
HOOK_ISSUE_CLOSEDHookIssueAction="closed"
HOOK_ISSUE_REOPENEDHookIssueAction="reopened"
HOOK_ISSUE_EDITEDHookIssueAction="edited"
HOOK_ISSUE_ASSIGNEDHookIssueAction="assigned"
HOOK_ISSUE_UNASSIGNEDHookIssueAction="unassigned"
HOOK_ISSUE_LABEL_UPDATEDHookIssueAction="label_updated"
HOOK_ISSUE_LABEL_CLEAREDHookIssueAction="label_cleared"
HOOK_ISSUE_MILESTONEDHookIssueAction="milestoned"
HOOK_ISSUE_DEMILESTONEDHookIssueAction="demilestoned"
HOOK_ISSUE_SYNCHRONIZEDHookIssueAction="synchronized"
)
typeChangesFromPayloadstruct {
Fromstring`json:"from"`
}
typeChangesPayloadstruct {
Title*ChangesFromPayload`json:"title,omitempty"`
Body*ChangesFromPayload`json:"body,omitempty"`
}
// IssuesPayload represents a payload information of issues event.
typeIssuesPayloadstruct {
ActionHookIssueAction`json:"action"`
Indexint64`json:"number"`
Issue*Issue`json:"issue"`
Changes*ChangesPayload`json:"changes,omitempty"`
Repository*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*IssuesPayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
typeHookIssueCommentActionstring
const (
HOOK_ISSUE_COMMENT_CREATEDHookIssueCommentAction="created"
HOOK_ISSUE_COMMENT_EDITEDHookIssueCommentAction="edited"
HOOK_ISSUE_COMMENT_DELETEDHookIssueCommentAction="deleted"
)
// IssueCommentPayload represents a payload information of issue comment event.
typeIssueCommentPayloadstruct {
ActionHookIssueCommentAction`json:"action"`
Issue*Issue`json:"issue"`
Comment*Comment`json:"comment"`
Changes*ChangesPayload`json:"changes,omitempty"`
Repository*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*IssueCommentPayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// __________ .__ .__ __________ __
// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
// \/ \/ |__| \/ \/
// PullRequestPayload represents a payload information of pull request event.
typePullRequestPayloadstruct {
ActionHookIssueAction`json:"action"`
Indexint64`json:"number"`
PullRequest*PullRequest`json:"pull_request"`
Changes*ChangesPayload`json:"changes,omitempty"`
Repository*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*PullRequestPayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}
// __________ .__
// \______ \ ____ | | ____ _____ ______ ____
// | _// __ \| | _/ __ \\__ \ / ___// __ \
// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
// |____|_ /\___ >____/\___ >____ /____ >\___ >
// \/ \/ \/ \/ \/ \/
typeHookReleaseActionstring
const (
HOOK_RELEASE_PUBLISHEDHookReleaseAction="published"
)
// ReleasePayload represents a payload information of release event.
typeReleasePayloadstruct {
ActionHookReleaseAction`json:"action"`
Release*Release`json:"release"`
Repository*Repository`json:"repository"`
Sender*User`json:"sender"`
}
func (p*ReleasePayload) JSONPayload() ([]byte, error) {
returnjson.MarshalIndent(p, "", " ")
}