From 7aeb47857c36eb06771baf5242365f8ae3b4d26b Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 19 Mar 2024 14:24:37 -0400 Subject: [PATCH 1/3] Reuse User type for assignees and reviewers --- gitlab/payload.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/gitlab/payload.go b/gitlab/payload.go index 7f7d690..42fde5c 100644 --- a/gitlab/payload.go +++ b/gitlab/payload.go @@ -37,8 +37,8 @@ type IssueEventPayload struct { Project Project `json:"project"` Repository Repository `json:"repository"` ObjectAttributes ObjectAttributes `json:"object_attributes"` - Assignee Assignee `json:"assignee"` - Assignees []Assignee `json:"assignees"` + Assignee User `json:"assignee"` + Assignees []User `json:"assignees"` Changes Changes `json:"changes"` } @@ -59,17 +59,13 @@ type MergeRequestEventPayload struct { Project Project `json:"project"` Repository Repository `json:"repository"` Labels []Label `json:"labels"` - Assignees []Assignee `json:"assignees"` - Reviewers []Reviewers `json:"reviewers"` + Assignees []User `json:"assignees"` + Reviewers []User `json:"reviewers"` } // Reviewers contains all of the GitLab reviewers information -type Reviewers struct { - ID int `json:"id"` - Name string `json:"name"` - Username string `json:"username"` - AvatarURL string `json:"avatar_url"` -} +// Deprecated: Use User instead +type Reviewers User // PushEventPayload contains the information for GitLab's push event type PushEventPayload struct { @@ -728,7 +724,7 @@ type ObjectAttributes struct { Source Source `json:"source"` Target Target `json:"target"` LastCommit LastCommit `json:"last_commit"` - Assignee Assignee `json:"assignee"` + Assignee User `json:"assignee"` } // PipelineObjectAttributes contains pipeline specific GitLab object attributes information @@ -800,18 +796,13 @@ type MergeRequest struct { Target Target `json:"target"` LastCommit LastCommit `json:"last_commit"` WorkInProgress bool `json:"work_in_progress"` - Assignee Assignee `json:"assignee"` + Assignee User `json:"assignee"` URL string `json:"url"` } // Assignee contains all of the GitLab assignee information -type Assignee struct { - ID int64 `json:"id"` - Name string `json:"name"` - Username string `json:"username"` - AvatarURL string `json:"avatar_url"` - Email string `json:"email"` -} +// Deprecated: Use User instead +type Assignee User // StDiff contains all of the GitLab diff information type StDiff struct { From cb75564de147ae82436f2a1ab44bb4b71e04ff86 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 19 Mar 2024 14:28:23 -0400 Subject: [PATCH 2/3] Support other 'changes' for GitLab payloads --- .github/workflows/workflow.yml | 2 +- gitlab/payload.go | 43 +++++++++++++++++++++--- go.mod | 2 +- testdata/gitlab/merge-request-event.json | 34 +++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index dac0a63..16a5e0c 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -23,7 +23,7 @@ jobs: name: Test strategy: matrix: - go-version: [1.17.x, 1.18.x, 1.20.x] + go-version: [1.18.x, 1.20.x] os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: diff --git a/gitlab/payload.go b/gitlab/payload.go index 42fde5c..e9d073a 100644 --- a/gitlab/payload.go +++ b/gitlab/payload.go @@ -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 @@ -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 +) diff --git a/go.mod b/go.mod index bb0fa5a..a63b5d8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/testdata/gitlab/merge-request-event.json b/testdata/gitlab/merge-request-event.json index d873114..26e5333 100644 --- a/testdata/gitlab/merge-request-event.json +++ b/testdata/gitlab/merge-request-event.json @@ -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" @@ -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": [ From fbf0071a16c0c508de2a5215eb8962bb578b803b Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Fri, 22 Mar 2024 16:28:07 -0400 Subject: [PATCH 3/3] Add Draft field --- gitlab/payload.go | 1 + testdata/gitlab/merge-request-event.json | 1 + 2 files changed, 2 insertions(+) diff --git a/gitlab/payload.go b/gitlab/payload.go index e9d073a..cce3a6e 100644 --- a/gitlab/payload.go +++ b/gitlab/payload.go @@ -725,6 +725,7 @@ type ObjectAttributes struct { Target Target `json:"target"` LastCommit LastCommit `json:"last_commit"` Assignee User `json:"assignee"` + Draft bool `json:"draft"` } // PipelineObjectAttributes contains pipeline specific GitLab object attributes information diff --git a/testdata/gitlab/merge-request-event.json b/testdata/gitlab/merge-request-event.json index 26e5333..65ce9b5 100644 --- a/testdata/gitlab/merge-request-event.json +++ b/testdata/gitlab/merge-request-event.json @@ -50,6 +50,7 @@ "milestone_id": null, "state_id": 1, "state": "opened", + "draft": false, "blocking_discussions_resolved": true, "work_in_progress": false, "first_contribution": true,