Skip to content

Commit

Permalink
Support other 'changes' for GitLab payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Mar 19, 2024
1 parent 7aeb478 commit c3cb8c6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
43 changes: 38 additions & 5 deletions gitlab/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,36 @@ type Author struct {

// Changes contains all changes associated with a GitLab issue or MR
type Changes struct {
LabelChanges LabelChanges `json:"labels"`
LabelChanges *ListChange[Label] `json:"labels"`
Draft *PropChange[bool] `json:"draft"`
StateId *PropChange[IssuableStateID] `json:"state_id"`
Assignees *ListChange[User] `json:"assignees"`
Reviewers *ListChange[User] `json:"reviewers"`
Title *PropChange[string] `json:"title"`
Description *PropChange[string] `json:"description"`

UpdatedAt *PropChange[customTime] `json:"updated_at"`
UpdatedByID *PropChange[int64] `json:"updated_by_id"`
LastEditedAt *PropChange[customTime] `json:"last_edited_at"`
LastEditedByID *PropChange[int64] `json:"last_edited_by_id"`
}

type PropChange[T comparable] struct {
Previous T `json:"previous"`
Current T `json:"current"`
}

func (p *PropChange[T]) Was(value T) bool {
return p != nil && p.Previous == value
}

// LabelChanges contains changes in labels assocatiated with a GitLab issue or MR
type LabelChanges struct {
Previous []Label `json:"previous"`
Current []Label `json:"current"`
func (p *PropChange[T]) Became(newValue T) bool {
return p != nil && p.Current == newValue
}

type ListChange[T comparable] struct {
Previous []T `json:"previous"`
Current []T `json:"current"`
}

// Label contains all of the GitLab label information
Expand All @@ -892,3 +915,13 @@ type Label struct {
Type string `json:"type"`
GroupID int64 `json:"group_id"`
}

type IssuableStateID int

// Source: https://forum.gitlab.com/t/merge-event-state-id-meanings/47433
const (
StateOpened IssuableStateID = 1
StateClosed IssuableStateID = 2
StateMerged IssuableStateID = 3
StateLocked IssuableStateID = 4
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-playground/webhooks/v6

go 1.17
go 1.18

require (
github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355
Expand Down
34 changes: 34 additions & 0 deletions testdata/gitlab/merge-request-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,22 @@
"group_id": 41
}],
"changes": {
"title": {
"previous": "MS Viewport",
"current": "MS-Viewport"
},
"description": {
"previous": "Lorem ipsum dolor sit amet",
"current": ""
},
"updated_by_id": {
"previous": null,
"current": 1
},
"draft": {
"previous": true,
"current": false
},
"updated_at": {
"previous": "2017-09-15 16:50:55 UTC",
"current":"2017-09-15 16:52:00 UTC"
Expand Down Expand Up @@ -174,6 +186,28 @@
"last_edited_by_id": {
"previous": null,
"current": 3278533
},
"assignees": {
"previous": [],
"current": [
{
"id": 6,
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
}
]
},
"reviewers": {
"previous": [],
"current": [
{
"id": 6,
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
}
]
}
},
"assignees": [
Expand Down

0 comments on commit c3cb8c6

Please sign in to comment.