Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1282 from 6543-forks/add-support-for-EventTypeSer…
Browse files Browse the repository at this point in the history
…viceHook

Add support for event type service hook
  • Loading branch information
svanharmelen authored Oct 26, 2021
2 parents 483aead + cdf6f67 commit 295fe0a
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 22 deletions.
31 changes: 29 additions & 2 deletions event_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
EventTypeSystemHook EventType = "System Hook"
EventTypeTagPush EventType = "Tag Push Hook"
EventTypeWikiPage EventType = "Wiki Page Hook"
EventTypeServiceHook EventType = "Service Hook"
)

const (
Expand All @@ -50,13 +51,23 @@ const (
noteableTypeSnippet = "Snippet"
)

const (
eventObjectKindPush = "push"
eventObjectKindTagPush = "tag_push"
eventObjectKindMergeRequest = "merge_request"
)

type noteEvent struct {
ObjectKind string `json:"object_kind"`
ObjectAttributes struct {
NoteableType string `json:"noteable_type"`
} `json:"object_attributes"`
}

type serviceEvent struct {
ObjectKind string `json:"object_kind"`
}

const eventTypeHeader = "X-Gitlab-Event"

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

switch e.EventName {
case "push":
case eventObjectKindPush:
event = &PushSystemEvent{}
case "tag_push":
case eventObjectKindTagPush:
event = &TagPushSystemEvent{}
case "repository_update":
event = &RepositoryUpdateSystemEvent{}
Expand Down Expand Up @@ -242,6 +253,22 @@ func ParseWebhook(eventType EventType, payload []byte) (event interface{}, err e
default:
return nil, fmt.Errorf("unexpected noteable type %s", note.ObjectAttributes.NoteableType)
}
case EventTypeServiceHook:
service := &serviceEvent{}
err := json.Unmarshal(payload, service)
if err != nil {
return nil, err
}
switch service.ObjectKind {
case eventObjectKindPush:
event = &PushEvent{}
case eventObjectKindTagPush:
event = &TagEvent{}
case eventObjectKindMergeRequest:
event = &MergeEvent{}
default:
return nil, fmt.Errorf("unexpected service type %s", service.ObjectKind)
}

default:
return nil, fmt.Errorf("unexpected event type: %s", eventType)
Expand Down
6 changes: 3 additions & 3 deletions event_parsing_systemhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestParseSystemhookPush(t *testing.T) {
if !ok {
t.Errorf("Expected PushSystemHookEvent, but parsing produced %T", parsedEvent)
}
assert.Equal(t, "push", event.EventName)
assert.Equal(t, eventObjectKindPush, event.EventName)
}

func TestParseSystemhookTagPush(t *testing.T) {
Expand All @@ -49,7 +49,7 @@ func TestParseSystemhookTagPush(t *testing.T) {
if !ok {
t.Errorf("Expected TagPushSystemHookEvent, but parsing produced %T", parsedEvent)
}
assert.Equal(t, "tag_push", event.EventName)
assert.Equal(t, eventObjectKindTagPush, event.EventName)
}

func TestParseSystemhookMergeRequest(t *testing.T) {
Expand All @@ -64,7 +64,7 @@ func TestParseSystemhookMergeRequest(t *testing.T) {
if !ok {
t.Errorf("Expected MergeRequestSystemHookEvent, but parsing produced %T", parsedEvent)
}
assert.Equal(t, "merge_request", event.ObjectKind)
assert.Equal(t, eventObjectKindMergeRequest, event.ObjectKind)
}

func TestParseSystemhookRepositoryUpdate(t *testing.T) {
Expand Down
32 changes: 28 additions & 4 deletions event_parsing_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gitlab

import (
"net/http"
"reflect"
"testing"

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

if event.ObjectKind != "push" {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "push")
if event.ObjectKind != eventObjectKindPush {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindPush)
}

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

if event.ObjectKind != "tag_push" {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "tag_push")
if event.ObjectKind != eventObjectKindTagPush {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindTagPush)
}

if event.ProjectID != 1 {
Expand Down Expand Up @@ -422,3 +423,26 @@ func TestParseWikiPageHook(t *testing.T) {
t.Errorf("Message is %v, want %v", event.ObjectAttributes.Message, "adding an awesome page to the wiki")
}
}

func TestParseServiceWebHook(t *testing.T) {
parsedEvent, err := ParseWebhook("Service Hook", loadFixture("testdata/webhooks/service_merge_request.json"))
if err != nil {
t.Errorf("Error parsing service hook merge request: %s", err)
}

switch event := parsedEvent.(type) {
case *MergeEvent:
assert.EqualValues(t, &EventUser{
ID: 2,
Name: "the test",
Username: "test",
Email: "[email protected]",
AvatarURL: "https://www.gravatar.com/avatar/dd46a756faad4727fb679320751f6dea?s=80&d=identicon",
}, event.User)
assert.EqualValues(t, "unchecked", event.ObjectAttributes.MergeStatus)
assert.EqualValues(t, "next-feature", event.ObjectAttributes.SourceBranch)
assert.EqualValues(t, "master", event.ObjectAttributes.TargetBranch)
default:
t.Errorf("unexpected event type: %s", reflect.TypeOf(parsedEvent))
}
}
4 changes: 2 additions & 2 deletions event_webhook_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func TestMergeEventUnmarshalFromGroup(t *testing.T) {
t.Errorf("Group Merge Event is null")
}

if event.ObjectKind != "merge_request" {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, "merge_request")
if event.ObjectKind != eventObjectKindMergeRequest {
t.Errorf("ObjectKind is %v, want %v", event.ObjectKind, eventObjectKindMergeRequest)
}

if event.User.Username != expectedUsername {
Expand Down
4 changes: 2 additions & 2 deletions resource_state_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/require"
)

func TestResourceStateEventsService_ListIssueStateEvents(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (
)

type Person struct {
Name string
Age int
Name string
Age int
NickNames []string
Address Address
Company *Company
Address Address
Company *Company
}

type Address struct {
Street string
City string
Street string
City string
Province string
Country string
Country string
}

type Company struct {
Name string
Name string
Address Address
Country string
}
Expand All @@ -33,7 +33,7 @@ func TestStringify_nil(t *testing.T) {
}

func TestStringify(t *testing.T) {
person := &Person{"name", 16, []string {"n", "a", "m", "e"}, Address{}, nil}
person := &Person{"name", 16, []string{"n", "a", "m", "e"}, Address{}, nil}
resp := Stringify(person)
want := "gitlab.Person{Name:\"name\", Age:16, NickNames:[\"n\" \"a\" \"m\" \"e\"], Address:gitlab.Address{Street:\"\", City:\"\", Province:\"\", Country:\"\"}}"
assert.Equal(t, want, resp)
Expand Down
129 changes: 129 additions & 0 deletions testdata/webhooks/service_merge_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"object_kind": "merge_request",
"event_type": "merge_request",
"user": {
"id": 2,
"name": "the test",
"username": "test",
"avatar_url": "https://www.gravatar.com/avatar/dd46a756faad4727fb679320751f6dea?s=80&d=identicon",
"email": "[email protected]"
},
"project": {
"id": 2,
"name": "Woodpecker",
"description": "",
"web_url": "http://10.40.8.5:3200/test/woodpecker",
"avatar_url": null,
"git_ssh_url": "[email protected]:test/woodpecker.git",
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
"namespace": "the test",
"visibility_level": 20,
"path_with_namespace": "test/woodpecker",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://10.40.8.5:3200/test/woodpecker",
"url": "[email protected]:test/woodpecker.git",
"ssh_url": "[email protected]:test/woodpecker.git",
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
},
"object_attributes": {
"assignee_id": null,
"author_id": 2,
"created_at": "2021-09-27 05:00:01 UTC",
"description": "",
"head_pipeline_id": 5,
"id": 2,
"iid": 2,
"last_edited_at": null,
"last_edited_by_id": null,
"merge_commit_sha": null,
"merge_error": null,
"merge_params": {
"force_remove_source_branch": "1"
},
"merge_status": "unchecked",
"merge_user_id": null,
"merge_when_pipeline_succeeds": false,
"milestone_id": null,
"source_branch": "next-feature",
"source_project_id": 2,
"state_id": 1,
"target_branch": "master",
"target_project_id": 2,
"time_estimate": 0,
"title": "Update client.go 🎉",
"updated_at": "2021-09-27 05:01:21 UTC",
"updated_by_id": null,
"url": "http://10.40.8.5:3200/test/woodpecker/-/merge_requests/2",
"source": {
"id": 2,
"name": "Woodpecker",
"description": "",
"web_url": "http://10.40.8.5:3200/test/woodpecker",
"avatar_url": "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
"git_ssh_url": "[email protected]:test/woodpecker.git",
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
"namespace": "the test",
"visibility_level": 20,
"path_with_namespace": "test/woodpecker",
"default_branch": "develop",
"ci_config_path": null,
"homepage": "http://10.40.8.5:3200/test/woodpecker",
"url": "[email protected]:test/woodpecker.git",
"ssh_url": "[email protected]:test/woodpecker.git",
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
},
"target": {
"id": 2,
"name": "Woodpecker",
"description": "",
"web_url": "http://10.40.8.5:3200/test/woodpecker",
"avatar_url": "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg",
"git_ssh_url": "[email protected]:test/woodpecker.git",
"git_http_url": "http://10.40.8.5:3200/test/woodpecker.git",
"namespace": "the test",
"visibility_level": 20,
"path_with_namespace": "test/woodpecker",
"default_branch": "develop",
"ci_config_path": null,
"homepage": "http://10.40.8.5:3200/test/woodpecker",
"url": "[email protected]:test/woodpecker.git",
"ssh_url": "[email protected]:test/woodpecker.git",
"http_url": "http://10.40.8.5:3200/test/woodpecker.git"
},
"last_commit": {
"id": "0ab96a10266b95b4b533dcfd98738015fbe70889",
"message": "Update state.go",
"title": "Update state.go",
"timestamp": "2021-09-27T05:01:20+00:00",
"url": "http://10.40.8.5:3200/test/woodpecker/-/commit/0ab96a10266b95b4b533dcfd98738015fbe70889",
"author": {
"name": "the test",
"email": "[email protected]"
}
},
"work_in_progress": false,
"total_time_spent": 0,
"time_change": 0,
"human_total_time_spent": null,
"human_time_change": null,
"human_time_estimate": null,
"assignee_ids": [],
"state": "opened",
"action": "update",
"oldrev": "6ef047571374c96a2bf13c361efd1fb008b0063e"
},
"labels": [],
"changes": {
"updated_at": {
"previous": "2021-09-27 05:00:01 UTC",
"current": "2021-09-27 05:01:21 UTC"
}
},
"repository": {
"name": "Woodpecker",
"url": "[email protected]:test/woodpecker.git",
"description": "",
"homepage": "http://10.40.8.5:3200/test/woodpecker"
}
}

0 comments on commit 295fe0a

Please sign in to comment.