Skip to content

Commit

Permalink
Gitlab deployment event (#156)
Browse files Browse the repository at this point in the history
Added Gitlab deployment event

Co-authored-by: Dean Karn <[email protected]>
  • Loading branch information
exileed and Dean Karn authored May 21, 2023
1 parent 659b2a2 commit 0d87ac3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
PipelineEvents Event = "Pipeline Hook"
BuildEvents Event = "Build Hook"
JobEvents Event = "Job Hook"
DeploymentEvents Event = "Deployment Hook"
SystemHookEvents Event = "System Hook"
objectPush string = "push"
objectTag string = "tag_push"
Expand Down Expand Up @@ -208,6 +209,14 @@ func eventParsing(gitLabEvent Event, events []Event, payload []byte) (interface{
}
return pl, nil

case DeploymentEvents:
var pl DeploymentEventPayload
err := json.Unmarshal([]byte(payload), &pl)
if err != nil {
return nil, err
}
return pl, nil

case SystemHookEvents:
var pl SystemHookPayload
err := json.Unmarshal([]byte(payload), &pl)
Expand Down
9 changes: 9 additions & 0 deletions gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ func TestWebhooks(t *testing.T) {
"X-Gitlab-Event": []string{"Build Hook"},
},
},
{
name: "DeploymentEvent",
event: DeploymentEvents,
typ: DeploymentEventPayload{},
filename: "../testdata/gitlab/deployment-event.json",
headers: http.Header{
"X-Gitlab-Event": []string{"Deployment Hook"},
},
},
}

for _, tt := range tests {
Expand Down
17 changes: 17 additions & 0 deletions gitlab/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ type JobEventPayload struct {
Runner Runner `json:"runner"`
}

// DeploymentEventPayload contains the information for GitLab's triggered when a deployment
type DeploymentEventPayload struct {
ObjectKind string `json:"object_kind"`
Status string `json:"status"`
StatusChangeAt string `json:"status_changed_at"`
DeploymentId int64 `json:"deployment_id"`
DeployableId int64 `json:"deployable_id"`
DeployableUrl string `json:"deployable_url"`
Environment string `json:"environment"`
Project Project `json:"project"`
ShortSha string `json:"short_sha"`
User User `json:"user"`
UserUrl string `json:"user_url"`
CommitUrl string `json:"commit_url"`
CommitTitle string `json:"commit_title"`
}

// SystemHookPayload contains the ObjectKind to match with real hook events
type SystemHookPayload struct {
ObjectKind string `json:"object_kind"`
Expand Down
38 changes: 38 additions & 0 deletions testdata/gitlab/deployment-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"object_kind": "deployment",
"status": "success",
"status_changed_at":"2021-04-28 21:50:00 +0200",
"deployment_id": 15,
"deployable_id": 796,
"deployable_url": "http://10.126.0.2:3000/root/test-deployment-webhooks/-/jobs/796",
"environment": "staging",
"project": {
"id": 30,
"name": "test-deployment-webhooks",
"description": "",
"web_url": "http://10.126.0.2:3000/root/test-deployment-webhooks",
"avatar_url": null,
"git_ssh_url": "ssh://[email protected]:2222/root/test-deployment-webhooks.git",
"git_http_url": "http://10.126.0.2:3000/root/test-deployment-webhooks.git",
"namespace": "Administrator",
"visibility_level": 0,
"path_with_namespace": "root/test-deployment-webhooks",
"default_branch": "master",
"ci_config_path": "",
"homepage": "http://10.126.0.2:3000/root/test-deployment-webhooks",
"url": "ssh://[email protected]:2222/root/test-deployment-webhooks.git",
"ssh_url": "ssh://[email protected]:2222/root/test-deployment-webhooks.git",
"http_url": "http://10.126.0.2:3000/root/test-deployment-webhooks.git"
},
"short_sha": "279484c0",
"user": {
"id": 1,
"name": "Administrator",
"username": "root",
"avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"email": "[email protected]"
},
"user_url": "http://10.126.0.2:3000/root",
"commit_url": "http://10.126.0.2:3000/root/test-deployment-webhooks/-/commit/279484c09fbe69ededfced8c1bb6e6d24616b468",
"commit_title": "Add new file"
}

0 comments on commit 0d87ac3

Please sign in to comment.