Skip to content

Commit 295fe0a

Browse files
authored
Merge pull request xanzy#1282 from 6543-forks/add-support-for-EventTypeServiceHook
Add support for event type service hook
2 parents 483aead + cdf6f67 commit 295fe0a

7 files changed

+202
-22
lines changed

event_parsing.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
EventTypeSystemHook EventType = "System Hook"
4242
EventTypeTagPush EventType = "Tag Push Hook"
4343
EventTypeWikiPage EventType = "Wiki Page Hook"
44+
EventTypeServiceHook EventType = "Service Hook"
4445
)
4546

4647
const (
@@ -50,13 +51,23 @@ const (
5051
noteableTypeSnippet = "Snippet"
5152
)
5253

54+
const (
55+
eventObjectKindPush = "push"
56+
eventObjectKindTagPush = "tag_push"
57+
eventObjectKindMergeRequest = "merge_request"
58+
)
59+
5360
type noteEvent struct {
5461
ObjectKind string `json:"object_kind"`
5562
ObjectAttributes struct {
5663
NoteableType string `json:"noteable_type"`
5764
} `json:"object_attributes"`
5865
}
5966

67+
type serviceEvent struct {
68+
ObjectKind string `json:"object_kind"`
69+
}
70+
6071
const eventTypeHeader = "X-Gitlab-Event"
6172

6273
// HookEventType returns the event type for the given request.
@@ -119,9 +130,9 @@ func ParseSystemhook(payload []byte) (event interface{}, err error) {
119130
}
120131

121132
switch e.EventName {
122-
case "push":
133+
case eventObjectKindPush:
123134
event = &PushSystemEvent{}
124-
case "tag_push":
135+
case eventObjectKindTagPush:
125136
event = &TagPushSystemEvent{}
126137
case "repository_update":
127138
event = &RepositoryUpdateSystemEvent{}
@@ -242,6 +253,22 @@ func ParseWebhook(eventType EventType, payload []byte) (event interface{}, err e
242253
default:
243254
return nil, fmt.Errorf("unexpected noteable type %s", note.ObjectAttributes.NoteableType)
244255
}
256+
case EventTypeServiceHook:
257+
service := &serviceEvent{}
258+
err := json.Unmarshal(payload, service)
259+
if err != nil {
260+
return nil, err
261+
}
262+
switch service.ObjectKind {
263+
case eventObjectKindPush:
264+
event = &PushEvent{}
265+
case eventObjectKindTagPush:
266+
event = &TagEvent{}
267+
case eventObjectKindMergeRequest:
268+
event = &MergeEvent{}
269+
default:
270+
return nil, fmt.Errorf("unexpected service type %s", service.ObjectKind)
271+
}
245272

246273
default:
247274
return nil, fmt.Errorf("unexpected event type: %s", eventType)

event_parsing_systemhook_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestParseSystemhookPush(t *testing.T) {
3434
if !ok {
3535
t.Errorf("Expected PushSystemHookEvent, but parsing produced %T", parsedEvent)
3636
}
37-
assert.Equal(t, "push", event.EventName)
37+
assert.Equal(t, eventObjectKindPush, event.EventName)
3838
}
3939

4040
func TestParseSystemhookTagPush(t *testing.T) {
@@ -49,7 +49,7 @@ func TestParseSystemhookTagPush(t *testing.T) {
4949
if !ok {
5050
t.Errorf("Expected TagPushSystemHookEvent, but parsing produced %T", parsedEvent)
5151
}
52-
assert.Equal(t, "tag_push", event.EventName)
52+
assert.Equal(t, eventObjectKindTagPush, event.EventName)
5353
}
5454

5555
func TestParseSystemhookMergeRequest(t *testing.T) {
@@ -64,7 +64,7 @@ func TestParseSystemhookMergeRequest(t *testing.T) {
6464
if !ok {
6565
t.Errorf("Expected MergeRequestSystemHookEvent, but parsing produced %T", parsedEvent)
6666
}
67-
assert.Equal(t, "merge_request", event.ObjectKind)
67+
assert.Equal(t, eventObjectKindMergeRequest, event.ObjectKind)
6868
}
6969

7070
func TestParseSystemhookRepositoryUpdate(t *testing.T) {

event_parsing_webhook_test.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package gitlab
1818

1919
import (
2020
"net/http"
21+
"reflect"
2122
"testing"
2223

2324
"github.com/stretchr/testify/assert"
@@ -286,8 +287,8 @@ func TestParsePushHook(t *testing.T) {
286287
t.Errorf("Expected PushEvent, but parsing produced %T", parsedEvent)
287288
}
288289

289-
if event.ObjectKind != "push" {
290-
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "push")
290+
if event.ObjectKind != eventObjectKindPush {
291+
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindPush)
291292
}
292293

293294
if event.ProjectID != 15 {
@@ -372,8 +373,8 @@ func TestParseTagHook(t *testing.T) {
372373
t.Errorf("Expected TagEvent, but parsing produced %T", parsedEvent)
373374
}
374375

375-
if event.ObjectKind != "tag_push" {
376-
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "tag_push")
376+
if event.ObjectKind != eventObjectKindTagPush {
377+
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindTagPush)
377378
}
378379

379380
if event.ProjectID != 1 {
@@ -422,3 +423,26 @@ func TestParseWikiPageHook(t *testing.T) {
422423
t.Errorf("Message is %v, want %v", event.ObjectAttributes.Message, "adding an awesome page to the wiki")
423424
}
424425
}
426+
427+
func TestParseServiceWebHook(t *testing.T) {
428+
parsedEvent, err := ParseWebhook("Service Hook", loadFixture("testdata/webhooks/service_merge_request.json"))
429+
if err != nil {
430+
t.Errorf("Error parsing service hook merge request: %s", err)
431+
}
432+
433+
switch event := parsedEvent.(type) {
434+
case *MergeEvent:
435+
assert.EqualValues(t, &EventUser{
436+
ID: 2,
437+
Name: "the test",
438+
Username: "test",
439+
440+
AvatarURL: "https://www.gravatar.com/avatar/dd46a756faad4727fb679320751f6dea?s=80&d=identicon",
441+
}, event.User)
442+
assert.EqualValues(t, "unchecked", event.ObjectAttributes.MergeStatus)
443+
assert.EqualValues(t, "next-feature", event.ObjectAttributes.SourceBranch)
444+
assert.EqualValues(t, "master", event.ObjectAttributes.TargetBranch)
445+
default:
446+
t.Errorf("unexpected event type: %s", reflect.TypeOf(parsedEvent))
447+
}
448+
}

event_webhook_types_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ func TestMergeEventUnmarshalFromGroup(t *testing.T) {
241241
t.Errorf("Group Merge Event is null")
242242
}
243243

244-
if event.ObjectKind != "merge_request" {
245-
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "merge_request")
244+
if event.ObjectKind != eventObjectKindMergeRequest {
245+
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindMergeRequest)
246246
}
247247

248248
if event.User.Username != expectedUsername {

resource_state_events_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"net/http"
66
"testing"
7-
8-
"github.com/stretchr/testify/require"
7+
8+
"github.com/stretchr/testify/require"
99
)
1010

1111
func TestResourceStateEventsService_ListIssueStateEvents(t *testing.T) {

strings_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66
)
77

88
type Person struct {
9-
Name string
10-
Age int
9+
Name string
10+
Age int
1111
NickNames []string
12-
Address Address
13-
Company *Company
12+
Address Address
13+
Company *Company
1414
}
1515

1616
type Address struct {
17-
Street string
18-
City string
17+
Street string
18+
City string
1919
Province string
20-
Country string
20+
Country string
2121
}
2222

2323
type Company struct {
24-
Name string
24+
Name string
2525
Address Address
2626
Country string
2727
}
@@ -33,7 +33,7 @@ func TestStringify_nil(t *testing.T) {
3333
}
3434

3535
func TestStringify(t *testing.T) {
36-
person := &Person{"name", 16, []string {"n", "a", "m", "e"}, Address{}, nil}
36+
person := &Person{"name", 16, []string{"n", "a", "m", "e"}, Address{}, nil}
3737
resp := Stringify(person)
3838
want := "gitlab.Person{Name:\"name\", Age:16, NickNames:[\"n\" \"a\" \"m\" \"e\"], Address:gitlab.Address{Street:\"\", City:\"\", Province:\"\", Country:\"\"}}"
3939
assert.Equal(t, want, resp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"object_kind": "merge_request",
3+
"event_type": "merge_request",
4+
"user": {
5+
"id": 2,
6+
"name": "the test",
7+
"username": "test",
8+
"avatar_url": "https://www.gravatar.com/avatar/dd46a756faad4727fb679320751f6dea?s=80&d=identicon",
9+
"email": "[email protected]"
10+
},
11+
"project": {
12+
"id": 2,
13+
"name": "Woodpecker",
14+
"description": "",
15+
"web_url": "http://10.40.8.5:3200/test/woodpecker",
16+
"avatar_url": null,
17+
"git_ssh_url": "[email protected]:test/woodpecker.git",
18+
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
19+
"namespace": "the test",
20+
"visibility_level": 20,
21+
"path_with_namespace": "test/woodpecker",
22+
"default_branch": "master",
23+
"ci_config_path": null,
24+
"homepage": "http://10.40.8.5:3200/test/woodpecker",
25+
"url": "[email protected]:test/woodpecker.git",
26+
"ssh_url": "[email protected]:test/woodpecker.git",
27+
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
28+
},
29+
"object_attributes": {
30+
"assignee_id": null,
31+
"author_id": 2,
32+
"created_at": "2021-09-27 05:00:01 UTC",
33+
"description": "",
34+
"head_pipeline_id": 5,
35+
"id": 2,
36+
"iid": 2,
37+
"last_edited_at": null,
38+
"last_edited_by_id": null,
39+
"merge_commit_sha": null,
40+
"merge_error": null,
41+
"merge_params": {
42+
"force_remove_source_branch": "1"
43+
},
44+
"merge_status": "unchecked",
45+
"merge_user_id": null,
46+
"merge_when_pipeline_succeeds": false,
47+
"milestone_id": null,
48+
"source_branch": "next-feature",
49+
"source_project_id": 2,
50+
"state_id": 1,
51+
"target_branch": "master",
52+
"target_project_id": 2,
53+
"time_estimate": 0,
54+
"title": "Update client.go 🎉",
55+
"updated_at": "2021-09-27 05:01:21 UTC",
56+
"updated_by_id": null,
57+
"url": "http://10.40.8.5:3200/test/woodpecker/-/merge_requests/2",
58+
"source": {
59+
"id": 2,
60+
"name": "Woodpecker",
61+
"description": "",
62+
"web_url": "http://10.40.8.5:3200/test/woodpecker",
63+
"avatar_url": "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
64+
"git_ssh_url": "[email protected]:test/woodpecker.git",
65+
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
66+
"namespace": "the test",
67+
"visibility_level": 20,
68+
"path_with_namespace": "test/woodpecker",
69+
"default_branch": "develop",
70+
"ci_config_path": null,
71+
"homepage": "http://10.40.8.5:3200/test/woodpecker",
72+
"url": "[email protected]:test/woodpecker.git",
73+
"ssh_url": "[email protected]:test/woodpecker.git",
74+
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
75+
},
76+
"target": {
77+
"id": 2,
78+
"name": "Woodpecker",
79+
"description": "",
80+
"web_url": "http://10.40.8.5:3200/test/woodpecker",
81+
"avatar_url": "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
82+
"git_ssh_url": "[email protected]:test/woodpecker.git",
83+
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
84+
"namespace": "the test",
85+
"visibility_level": 20,
86+
"path_with_namespace": "test/woodpecker",
87+
"default_branch": "develop",
88+
"ci_config_path": null,
89+
"homepage": "http://10.40.8.5:3200/test/woodpecker",
90+
"url": "[email protected]:test/woodpecker.git",
91+
"ssh_url": "[email protected]:test/woodpecker.git",
92+
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
93+
},
94+
"last_commit": {
95+
"id": "0ab96a10266b95b4b533dcfd98738015fbe70889",
96+
"message": "Update state.go",
97+
"title": "Update state.go",
98+
"timestamp": "2021-09-27T05:01:20+00:00",
99+
"url": "http://10.40.8.5:3200/test/woodpecker/-/commit/0ab96a10266b95b4b533dcfd98738015fbe70889",
100+
"author": {
101+
"name": "the test",
102+
"email": "[email protected]"
103+
}
104+
},
105+
"work_in_progress": false,
106+
"total_time_spent": 0,
107+
"time_change": 0,
108+
"human_total_time_spent": null,
109+
"human_time_change": null,
110+
"human_time_estimate": null,
111+
"assignee_ids": [],
112+
"state": "opened",
113+
"action": "update",
114+
"oldrev": "6ef047571374c96a2bf13c361efd1fb008b0063e"
115+
},
116+
"labels": [],
117+
"changes": {
118+
"updated_at": {
119+
"previous": "2021-09-27 05:00:01 UTC",
120+
"current": "2021-09-27 05:01:21 UTC"
121+
}
122+
},
123+
"repository": {
124+
"name": "Woodpecker",
125+
"url": "[email protected]:test/woodpecker.git",
126+
"description": "",
127+
"homepage": "http://10.40.8.5:3200/test/woodpecker"
128+
}
129+
}

0 commit comments

Comments
 (0)