Skip to content

Commit b616136

Browse files
authored
Merge pull request #111 from aritas1/v5
handle gitlab system hook inconsistencies
2 parents 303483b + b03dc86 commit b616136

File tree

6 files changed

+216
-4
lines changed

6 files changed

+216
-4
lines changed

gitlab/gitlab.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,16 @@ func eventParsing(gitLabEvent Event, events []Event, payload []byte) (interface{
191191
case objectMergeRequest:
192192
return eventParsing(MergeRequestEvents, events, payload)
193193
default:
194-
return nil, fmt.Errorf("unknown system hook event %s", gitLabEvent)
194+
switch pl.EventName {
195+
case objectPush:
196+
return eventParsing(PushEvents, events, payload)
197+
case objectTag:
198+
return eventParsing(TagEvents, events, payload)
199+
case objectMergeRequest:
200+
return eventParsing(MergeRequestEvents, events, payload)
201+
default:
202+
return nil, fmt.Errorf("unknown system hook event %s", gitLabEvent)
203+
}
195204
}
196205
default:
197206
return nil, fmt.Errorf("unknown event %s", gitLabEvent)

gitlab/gitlab_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,19 @@ func TestSystemHooks(t *testing.T) {
286286
name: "PushEvent",
287287
event: PushEvents,
288288
typ: PushEventPayload{},
289-
filename: "../testdata/gitlab/push-event.json",
289+
filename: "../testdata/gitlab/system-push-event.json",
290290
},
291291
{
292292
name: "TagEvent",
293293
event: TagEvents,
294294
typ: TagEventPayload{},
295-
filename: "../testdata/gitlab/tag-event.json",
295+
filename: "../testdata/gitlab/system-tag-event.json",
296296
},
297297
{
298298
name: "MergeRequestEvent",
299299
event: MergeRequestEvents,
300300
typ: MergeRequestEventPayload{},
301-
filename: "../testdata/gitlab/merge-request-event.json",
301+
filename: "../testdata/gitlab/system-merge-request-event.json",
302302
},
303303
}
304304
for _, tt := range tests {

gitlab/payload.go

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ type JobEventPayload struct {
176176
// SystemHookPayload contains the ObjectKind to match with real hook events
177177
type SystemHookPayload struct {
178178
ObjectKind string `json:"object_kind"`
179+
EventName string `json:"event_name"`
179180
}
180181

181182
// Issue contains all of the GitLab issue information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"object_kind": "merge_request",
3+
"user": {
4+
"name": "Administrator",
5+
"username": "root",
6+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon"
7+
},
8+
"project": {
9+
"name": "Example",
10+
"description": "",
11+
"web_url": "http://example.com/jsmith/example",
12+
"avatar_url": null,
13+
"git_ssh_url": "[email protected]:jsmith/example.git",
14+
"git_http_url": "http://example.com/jsmith/example.git",
15+
"namespace": "Jsmith",
16+
"visibility_level": 0,
17+
"path_with_namespace": "jsmith/example",
18+
"default_branch": "master",
19+
"ci_config_path": "",
20+
"homepage": "http://example.com/jsmith/example",
21+
"url": "[email protected]:jsmith/example.git",
22+
"ssh_url": "[email protected]:jsmith/example.git",
23+
"http_url": "http://example.com/jsmith/example.git"
24+
},
25+
"object_attributes": {
26+
"id": 90,
27+
"target_branch": "master",
28+
"source_branch": "ms-viewport",
29+
"source_project_id": 14,
30+
"author_id": 51,
31+
"assignee_id": 6,
32+
"title": "MS-Viewport",
33+
"created_at": "2017-09-20T08:31:45.944Z",
34+
"updated_at": "2017-09-28T12:23:42.365Z",
35+
"milestone_id": null,
36+
"state": "opened",
37+
"merge_status": "unchecked",
38+
"target_project_id": 14,
39+
"iid": 1,
40+
"description": "",
41+
"updated_by_id": 1,
42+
"merge_error": null,
43+
"merge_params": {
44+
"force_remove_source_branch": "0"
45+
},
46+
"merge_when_pipeline_succeeds": false,
47+
"merge_user_id": null,
48+
"merge_commit_sha": null,
49+
"deleted_at": null,
50+
"in_progress_merge_commit_sha": null,
51+
"lock_version": 5,
52+
"time_estimate": 0,
53+
"last_edited_at": "2017-09-27T12:43:37.558Z",
54+
"last_edited_by_id": 1,
55+
"head_pipeline_id": 61,
56+
"ref_fetched": true,
57+
"merge_jid": null,
58+
"source": {
59+
"name": "Awesome Project",
60+
"description": "",
61+
"web_url": "http://example.com/awesome_space/awesome_project",
62+
"avatar_url": null,
63+
"git_ssh_url": "[email protected]:awesome_space/awesome_project.git",
64+
"git_http_url": "http://example.com/awesome_space/awesome_project.git",
65+
"namespace": "root",
66+
"visibility_level": 0,
67+
"path_with_namespace": "awesome_space/awesome_project",
68+
"default_branch": "master",
69+
"ci_config_path": "",
70+
"homepage": "http://example.com/awesome_space/awesome_project",
71+
"url": "http://example.com/awesome_space/awesome_project.git",
72+
"ssh_url": "[email protected]:awesome_space/awesome_project.git",
73+
"http_url": "http://example.com/awesome_space/awesome_project.git"
74+
},
75+
"target": {
76+
"name": "Awesome Project",
77+
"description": "Aut reprehenderit ut est.",
78+
"web_url": "http://example.com/awesome_space/awesome_project",
79+
"avatar_url": null,
80+
"git_ssh_url": "[email protected]:awesome_space/awesome_project.git",
81+
"git_http_url": "http://example.com/awesome_space/awesome_project.git",
82+
"namespace": "Awesome Space",
83+
"visibility_level": 0,
84+
"path_with_namespace": "awesome_space/awesome_project",
85+
"default_branch": "master",
86+
"ci_config_path": "",
87+
"homepage": "http://example.com/awesome_space/awesome_project",
88+
"url": "http://example.com/awesome_space/awesome_project.git",
89+
"ssh_url": "[email protected]:awesome_space/awesome_project.git",
90+
"http_url": "http://example.com/awesome_space/awesome_project.git"
91+
},
92+
"last_commit": {
93+
"id": "ba3e0d8ff79c80d5b0bbb4f3e2e343e0aaa662b7",
94+
"message": "fixed readme",
95+
"timestamp": "2017-09-26T16:12:57Z",
96+
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
97+
"author": {
98+
"name": "GitLab dev user",
99+
"email": "gitlabdev@dv6700.(none)"
100+
}
101+
},
102+
"work_in_progress": false,
103+
"total_time_spent": 0,
104+
"human_total_time_spent": null,
105+
"human_time_estimate": null
106+
},
107+
"labels": null,
108+
"repository": {
109+
"name": "git-gpg-test",
110+
"url": "[email protected]:awesome_space/awesome_project.git",
111+
"description": "",
112+
"homepage": "http://example.com/awesome_space/awesome_project"
113+
}
114+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"event_name": "push",
3+
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
4+
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
5+
"ref": "refs/heads/master",
6+
"checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
7+
"user_id": 4,
8+
"user_name": "John Smith",
9+
"user_email": "[email protected]",
10+
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
11+
"project_id": 15,
12+
"project":{
13+
"name":"Diaspora",
14+
"description":"",
15+
"web_url":"http://example.com/mike/diaspora",
16+
"avatar_url":null,
17+
"git_ssh_url":"[email protected]:mike/diaspora.git",
18+
"git_http_url":"http://example.com/mike/diaspora.git",
19+
"namespace":"Mike",
20+
"visibility_level":0,
21+
"path_with_namespace":"mike/diaspora",
22+
"default_branch":"master",
23+
"homepage":"http://example.com/mike/diaspora",
24+
"url":"[email protected]:mike/diaspora.git",
25+
"ssh_url":"[email protected]:mike/diaspora.git",
26+
"http_url":"http://example.com/mike/diaspora.git"
27+
},
28+
"repository":{
29+
"name": "Diaspora",
30+
"url": "[email protected]:mike/diaspora.git",
31+
"description": "",
32+
"homepage": "http://example.com/mike/diaspora",
33+
"git_http_url":"http://example.com/mike/diaspora.git",
34+
"git_ssh_url":"[email protected]:mike/diaspora.git",
35+
"visibility_level":0
36+
},
37+
"commits": [
38+
{
39+
"id": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
40+
"message": "Add simple search to projects in public area",
41+
"timestamp": "2013-05-13T18:18:08+00:00",
42+
"url": "https://dev.gitlab.org/gitlab/gitlabhq/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
43+
"author": {
44+
"name": "Example User",
45+
"email": "[email protected]"
46+
}
47+
}
48+
],
49+
"total_commits_count": 1
50+
}

testdata/gitlab/system-tag-event.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"event_name": "tag_push",
3+
"before": "0000000000000000000000000000000000000000",
4+
"after": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7",
5+
"ref": "refs/tags/v1.0.0",
6+
"checkout_sha": "5937ac0a7beb003549fc5fd26fc247adbce4a52e",
7+
"user_id": 1,
8+
"user_name": "John Smith",
9+
"user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
10+
"project_id": 1,
11+
"project":{
12+
"name":"Example",
13+
"description":"",
14+
"web_url":"http://example.com/jsmith/example",
15+
"avatar_url":null,
16+
"git_ssh_url":"[email protected]:jsmith/example.git",
17+
"git_http_url":"http://example.com/jsmith/example.git",
18+
"namespace":"Jsmith",
19+
"visibility_level":0,
20+
"path_with_namespace":"jsmith/example",
21+
"default_branch":"master",
22+
"homepage":"http://example.com/jsmith/example",
23+
"url":"[email protected]:jsmith/example.git",
24+
"ssh_url":"[email protected]:jsmith/example.git",
25+
"http_url":"http://example.com/jsmith/example.git"
26+
},
27+
"repository":{
28+
"name": "Example",
29+
"url": "ssh://[email protected]/jsmith/example.git",
30+
"description": "",
31+
"homepage": "http://example.com/jsmith/example",
32+
"git_http_url":"http://example.com/jsmith/example.git",
33+
"git_ssh_url":"[email protected]:jsmith/example.git",
34+
"visibility_level":0
35+
},
36+
"commits": [],
37+
"total_commits_count": 0
38+
}

0 commit comments

Comments
 (0)