diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index 50663b4..4e29cba 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -36,6 +36,7 @@ const ( BuildEvents Event = "Build Hook" JobEvents Event = "Job Hook" DeploymentEvents Event = "Deployment Hook" + ReleaseEvents Event = "Release Hook" SystemHookEvents Event = "System Hook" objectPush string = "push" objectTag string = "tag_push" @@ -353,6 +354,10 @@ func eventParsing(gitLabEvent Event, events []Event, payload []byte) (interface{ return nil, fmt.Errorf("unknown system hook event %s", gitLabEvent) } } + case ReleaseEvents: + var pl ReleaseEventPayload + err := json.Unmarshal([]byte(payload), &pl) + return pl, err default: return nil, fmt.Errorf("unknown event %s", gitLabEvent) } diff --git a/gitlab/gitlab_test.go b/gitlab/gitlab_test.go index 6ecc5bc..69e7f6c 100644 --- a/gitlab/gitlab_test.go +++ b/gitlab/gitlab_test.go @@ -249,6 +249,15 @@ func TestWebhooks(t *testing.T) { "X-Gitlab-Event": []string{"Deployment Hook"}, }, }, + { + name: "ReleaseEvent", + event: ReleaseEvents, + typ: ReleaseEventPayload{}, + filename: "../testdata/gitlab/release-event.json", + headers: http.Header{ + "X-Gitlab-Event": []string{"Release Hook"}, + }, + }, } for _, tt := range tests { diff --git a/gitlab/payload.go b/gitlab/payload.go index 7134d7a..2923b5d 100644 --- a/gitlab/payload.go +++ b/gitlab/payload.go @@ -485,6 +485,43 @@ type GroupMemberUpdatedEventPayload struct { UserID int64 `json:"user_id"` } +// ReleaseEventPayload contains the information about GitLab's release event +type ReleaseEventPayload struct { + ID int `json:"id"` + CreatedAt customTime `json:"created_at"` + Description string `json:"description"` + Name string `json:"name"` + ReleasedAt customTime `json:"released_at"` + Tag string `json:"tag"` + ObjectKind string `json:"object_kind"` + Project Project `json:"project"` + URL string `json:"url"` + Action string `json:"action"` + Assets Assets `json:"assets"` +} + +// Assets represent artefacts and links associated to a release +type Assets struct { + Count int `json:"count"` + Links []Link `json:"links"` + Sources []AssetSource `json:"sources"` +} + +// Link represent a generic html link +type Link struct { + ID int `json:"id"` + External bool `json:"external"` + LinkType string `json:"link_type"` + Name string `json:"name"` + URL string `json:"url"` +} + +// AssetSource represent the download url for an asset +type AssetSource struct { + Format string `json:"format"` + URL string `json:"url"` +} + // Issue contains all of the GitLab issue information type Issue struct { ID int64 `json:"id"` diff --git a/testdata/gitlab/release-event.json b/testdata/gitlab/release-event.json new file mode 100644 index 0000000..c900700 --- /dev/null +++ b/testdata/gitlab/release-event.json @@ -0,0 +1,70 @@ +{ + "id": 1, + "created_at": "2020-11-02 12:55:12 UTC", + "description": "v1.1 has been released", + "name": "v1.1", + "released_at": "2020-11-02 12:55:12 UTC", + "tag": "v1.1", + "object_kind": "release", + "project": { + "id": 2, + "name": "release-webhook-example", + "description": "", + "web_url": "https://example.com/gitlab-org/release-webhook-example", + "avatar_url": null, + "git_ssh_url": "ssh://git@example.com/gitlab-org/release-webhook-example.git", + "git_http_url": "https://example.com/gitlab-org/release-webhook-example.git", + "namespace": "Gitlab", + "visibility_level": 0, + "path_with_namespace": "gitlab-org/release-webhook-example", + "default_branch": "master", + "ci_config_path": null, + "homepage": "https://example.com/gitlab-org/release-webhook-example", + "url": "ssh://git@example.com/gitlab-org/release-webhook-example.git", + "ssh_url": "ssh://git@example.com/gitlab-org/release-webhook-example.git", + "http_url": "https://example.com/gitlab-org/release-webhook-example.git" + }, + "url": "https://example.com/gitlab-org/release-webhook-example/-/releases/v1.1", + "action": "create", + "assets": { + "count": 5, + "links": [ + { + "id": 1, + "external": true, + "link_type": "other", + "name": "Changelog", + "url": "https://example.net/changelog" + } + ], + "sources": [ + { + "format": "zip", + "url": "https://example.com/gitlab-org/release-webhook-example/-/archive/v1.1/release-webhook-example-v1.1.zip" + }, + { + "format": "tar.gz", + "url": "https://example.com/gitlab-org/release-webhook-example/-/archive/v1.1/release-webhook-example-v1.1.tar.gz" + }, + { + "format": "tar.bz2", + "url": "https://example.com/gitlab-org/release-webhook-example/-/archive/v1.1/release-webhook-example-v1.1.tar.bz2" + }, + { + "format": "tar", + "url": "https://example.com/gitlab-org/release-webhook-example/-/archive/v1.1/release-webhook-example-v1.1.tar" + } + ] + }, + "commit": { + "id": "ee0a3fb31ac16e11b9dbb596ad16d4af654d08f8", + "message": "Release v1.1", + "title": "Release v1.1", + "timestamp": "2020-10-31T14:58:32+11:00", + "url": "https://example.com/gitlab-org/release-webhook-example/-/commit/ee0a3fb31ac16e11b9dbb596ad16d4af654d08f8", + "author": { + "name": "Example User", + "email": "user@example.com" + } + } +} \ No newline at end of file