From f04e17270c24d27aa765b212b0a40a644775f47b Mon Sep 17 00:00:00 2001 From: Jonas Bechstein Date: Wed, 24 Aug 2022 10:23:02 +0200 Subject: [PATCH 1/3] feat: ignore GitLab Draft MRs --- server/events/event_parser.go | 24 ++- server/events/event_parser_test.go | 19 ++ .../gitlab-merge-request-event-draft.json | 174 ++++++++++++++++++ 3 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 server/events/testdata/gitlab-merge-request-event-draft.json diff --git a/server/events/event_parser.go b/server/events/event_parser.go index 87cf264672..194e5344c8 100644 --- a/server/events/event_parser.go +++ b/server/events/event_parser.go @@ -617,15 +617,23 @@ func (e *EventParser) ParseGitlabMergeRequestEvent(event gitlab.MergeEvent) (pul BaseRepo: baseRepo, } - switch event.ObjectAttributes.Action { - case "open": - eventType = models.OpenedPullEvent - case "update": - eventType = e.ParseGitlabMergeRequestUpdateEvent(event) - case "merge", "close": - eventType = models.ClosedPullEvent - default: + // If it's a draft PR we ignore it for auto-planning if configured to do so + // however it's still possible for users to run plan on it manually via a + // comment so if any draft PR is closed we still need to check if we need + // to delete its locks. + if event.ObjectAttributes.WorkInProgress && event.ObjectAttributes.Action != "close" && !e.AllowDraftPRs { eventType = models.OtherPullEvent + } else { + switch event.ObjectAttributes.Action { + case "open": + eventType = models.OpenedPullEvent + case "update": + eventType = e.ParseGitlabMergeRequestUpdateEvent(event) + case "merge", "close": + eventType = models.ClosedPullEvent + default: + eventType = models.OtherPullEvent + } } user = models.User{ diff --git a/server/events/event_parser_test.go b/server/events/event_parser_test.go index d3e979a473..10586becff 100644 --- a/server/events/event_parser_test.go +++ b/server/events/event_parser_test.go @@ -380,6 +380,25 @@ func TestParseGitlabMergeEvent(t *testing.T) { Equals(t, models.ClosedPullState, pull.State) } +func TestParseGitlabMergeEventFromDraft(t *testing.T) { + path := filepath.Join("testdata", "gitlab-merge-request-event-draft.json") + bytes, err := os.ReadFile(path) + Ok(t, err) + var event *gitlab.MergeEvent + err = json.Unmarshal(bytes, &event) + Ok(t, err) + + _, evType, _, _, _, err := parser.ParseGitlabMergeRequestEvent(*event) + Ok(t, err) + Equals(t, models.OtherPullEvent, evType) + + parser.AllowDraftPRs = true + defer func() { parser.AllowDraftPRs = false }() + _, evType, _, _, _, err = parser.ParseGitlabMergeRequestEvent(*event) + Ok(t, err) + Equals(t, models.OpenedPullEvent, evType) +} + // Should be able to parse a merge event from a repo that is in a subgroup, // i.e. instead of under an owner/repo it's under an owner/group/subgroup/repo. func TestParseGitlabMergeEvent_Subgroup(t *testing.T) { diff --git a/server/events/testdata/gitlab-merge-request-event-draft.json b/server/events/testdata/gitlab-merge-request-event-draft.json new file mode 100644 index 0000000000..a6610c190f --- /dev/null +++ b/server/events/testdata/gitlab-merge-request-event-draft.json @@ -0,0 +1,174 @@ +{ + "object_kind": "merge_request", + "event_type": "merge_request", + "user": { + "name": "Luke Kysow", + "username": "lkysow", + "avatar_url": "https://secure.gravatar.com/avatar/25fd57e71590fe28736624ff24d41c5f?s=80&d=identicon" + }, + "project": { + "id": 4580910, + "name": "atlantis-example", + "description": "", + "web_url": "https://gitlab.com/lkysow/atlantis-example", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", + "git_http_url": "https://gitlab.com/lkysow/atlantis-example.git", + "namespace": "lkysow", + "visibility_level": 20, + "path_with_namespace": "lkysow/atlantis-example", + "default_branch": "master", + "ci_config_path": null, + "homepage": "https://gitlab.com/lkysow/atlantis-example", + "url": "git@gitlab.com:lkysow/atlantis-example.git", + "ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", + "http_url": "https://gitlab.com/lkysow/atlantis-example.git" + }, + "object_attributes": { + "assignee_id": null, + "author_id": 1755902, + "created_at": "2018-12-12 16:15:21 UTC", + "description": "", + "head_pipeline_id": null, + "id": 20809239, + "iid": 12, + "last_edited_at": null, + "last_edited_by_id": null, + "merge_commit_sha": null, + "merge_error": null, + "merge_params": { + "force_remove_source_branch": false + }, + "merge_status": "unchecked", + "merge_user_id": null, + "merge_when_pipeline_succeeds": false, + "milestone_id": null, + "source_branch": "patch-1", + "source_project_id": 4580910, + "state": "opened", + "target_branch": "master", + "target_project_id": 4580910, + "time_estimate": 0, + "title": "Update main.tf", + "updated_at": "2018-12-12 16:15:21 UTC", + "updated_by_id": null, + "url": "https://gitlab.com/lkysow/atlantis-example/merge_requests/12", + "source": { + "id": 4580910, + "name": "atlantis-example", + "description": "", + "web_url": "https://gitlab.com/sourceorg/atlantis-example", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:sourceorg/atlantis-example.git", + "git_http_url": "https://gitlab.com/sourceorg/atlantis-example.git", + "namespace": "sourceorg", + "visibility_level": 20, + "path_with_namespace": "sourceorg/atlantis-example", + "default_branch": "master", + "ci_config_path": null, + "homepage": "https://gitlab.com/sourceorg/atlantis-example", + "url": "git@gitlab.com:sourceorg/atlantis-example.git", + "ssh_url": "git@gitlab.com:sourceorg/atlantis-example.git", + "http_url": "https://gitlab.com/sourceorg/atlantis-example.git" + }, + "target": { + "id": 4580910, + "name": "atlantis-example", + "description": "", + "web_url": "https://gitlab.com/lkysow/atlantis-example", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", + "git_http_url": "https://gitlab.com/lkysow/atlantis-example.git", + "namespace": "lkysow", + "visibility_level": 20, + "path_with_namespace": "lkysow/atlantis-example", + "default_branch": "master", + "ci_config_path": null, + "homepage": "https://gitlab.com/lkysow/atlantis-example", + "url": "git@gitlab.com:lkysow/atlantis-example.git", + "ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", + "http_url": "https://gitlab.com/lkysow/atlantis-example.git" + }, + "last_commit": { + "id": "d2eae324ca26242abca45d7b49d582cddb2a4f15", + "message": "Update main.tf", + "timestamp": "2018-12-12T16:15:10Z", + "url": "https://gitlab.com/lkysow/atlantis-example/commit/d2eae324ca26242abca45d7b49d582cddb2a4f15", + "author": { + "name": "Luke Kysow", + "email": "lkysow@gmail.com" + } + }, + "work_in_progress": true, + "total_time_spent": 0, + "human_total_time_spent": null, + "human_time_estimate": null, + "action": "open" + }, + "labels": [ + + ], + "changes": { + "author_id": { + "previous": null, + "current": 1755902 + }, + "created_at": { + "previous": null, + "current": "2018-12-12 16:15:21 UTC" + }, + "description": { + "previous": null, + "current": "" + }, + "id": { + "previous": null, + "current": 20809239 + }, + "iid": { + "previous": null, + "current": 12 + }, + "merge_params": { + "previous": { + }, + "current": { + "force_remove_source_branch": false + } + }, + "source_branch": { + "previous": null, + "current": "patch-1" + }, + "source_project_id": { + "previous": null, + "current": 4580910 + }, + "target_branch": { + "previous": null, + "current": "master" + }, + "target_project_id": { + "previous": null, + "current": 4580910 + }, + "title": { + "previous": null, + "current": "Update main.tf" + }, + "updated_at": { + "previous": null, + "current": "2018-12-12 16:15:21 UTC" + }, + "total_time_spent": { + "previous": null, + "current": 0 + } + }, + "repository": { + "name": "atlantis-example", + "url": "git@gitlab.com:lkysow/atlantis-example.git", + "description": "", + "homepage": "https://gitlab.com/lkysow/atlantis-example" + } +} From bc64c84d3d21855ac8b1df64a16eb6e6db6f05ff Mon Sep 17 00:00:00 2001 From: Jonas Bechstein Date: Thu, 25 Aug 2022 10:20:30 +0200 Subject: [PATCH 2/3] deepcopy and overwrite unit under test --- server/events/event_parser_test.go | 12 +- .../gitlab-merge-request-event-draft.json | 174 ------------------ 2 files changed, 8 insertions(+), 178 deletions(-) delete mode 100644 server/events/testdata/gitlab-merge-request-event-draft.json diff --git a/server/events/event_parser_test.go b/server/events/event_parser_test.go index 10586becff..2c838fd9df 100644 --- a/server/events/event_parser_test.go +++ b/server/events/event_parser_test.go @@ -381,20 +381,24 @@ func TestParseGitlabMergeEvent(t *testing.T) { } func TestParseGitlabMergeEventFromDraft(t *testing.T) { - path := filepath.Join("testdata", "gitlab-merge-request-event-draft.json") + path := filepath.Join("testdata", "gitlab-merge-request-event.json") bytes, err := os.ReadFile(path) Ok(t, err) - var event *gitlab.MergeEvent + + var event gitlab.MergeEvent err = json.Unmarshal(bytes, &event) Ok(t, err) - _, evType, _, _, _, err := parser.ParseGitlabMergeRequestEvent(*event) + testEvent := deepcopy.Copy(event).(gitlab.MergeEvent) + testEvent.ObjectAttributes.WorkInProgress = true + + _, evType, _, _, _, err := parser.ParseGitlabMergeRequestEvent(testEvent) Ok(t, err) Equals(t, models.OtherPullEvent, evType) parser.AllowDraftPRs = true defer func() { parser.AllowDraftPRs = false }() - _, evType, _, _, _, err = parser.ParseGitlabMergeRequestEvent(*event) + _, evType, _, _, _, err = parser.ParseGitlabMergeRequestEvent(testEvent) Ok(t, err) Equals(t, models.OpenedPullEvent, evType) } diff --git a/server/events/testdata/gitlab-merge-request-event-draft.json b/server/events/testdata/gitlab-merge-request-event-draft.json deleted file mode 100644 index a6610c190f..0000000000 --- a/server/events/testdata/gitlab-merge-request-event-draft.json +++ /dev/null @@ -1,174 +0,0 @@ -{ - "object_kind": "merge_request", - "event_type": "merge_request", - "user": { - "name": "Luke Kysow", - "username": "lkysow", - "avatar_url": "https://secure.gravatar.com/avatar/25fd57e71590fe28736624ff24d41c5f?s=80&d=identicon" - }, - "project": { - "id": 4580910, - "name": "atlantis-example", - "description": "", - "web_url": "https://gitlab.com/lkysow/atlantis-example", - "avatar_url": null, - "git_ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", - "git_http_url": "https://gitlab.com/lkysow/atlantis-example.git", - "namespace": "lkysow", - "visibility_level": 20, - "path_with_namespace": "lkysow/atlantis-example", - "default_branch": "master", - "ci_config_path": null, - "homepage": "https://gitlab.com/lkysow/atlantis-example", - "url": "git@gitlab.com:lkysow/atlantis-example.git", - "ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", - "http_url": "https://gitlab.com/lkysow/atlantis-example.git" - }, - "object_attributes": { - "assignee_id": null, - "author_id": 1755902, - "created_at": "2018-12-12 16:15:21 UTC", - "description": "", - "head_pipeline_id": null, - "id": 20809239, - "iid": 12, - "last_edited_at": null, - "last_edited_by_id": null, - "merge_commit_sha": null, - "merge_error": null, - "merge_params": { - "force_remove_source_branch": false - }, - "merge_status": "unchecked", - "merge_user_id": null, - "merge_when_pipeline_succeeds": false, - "milestone_id": null, - "source_branch": "patch-1", - "source_project_id": 4580910, - "state": "opened", - "target_branch": "master", - "target_project_id": 4580910, - "time_estimate": 0, - "title": "Update main.tf", - "updated_at": "2018-12-12 16:15:21 UTC", - "updated_by_id": null, - "url": "https://gitlab.com/lkysow/atlantis-example/merge_requests/12", - "source": { - "id": 4580910, - "name": "atlantis-example", - "description": "", - "web_url": "https://gitlab.com/sourceorg/atlantis-example", - "avatar_url": null, - "git_ssh_url": "git@gitlab.com:sourceorg/atlantis-example.git", - "git_http_url": "https://gitlab.com/sourceorg/atlantis-example.git", - "namespace": "sourceorg", - "visibility_level": 20, - "path_with_namespace": "sourceorg/atlantis-example", - "default_branch": "master", - "ci_config_path": null, - "homepage": "https://gitlab.com/sourceorg/atlantis-example", - "url": "git@gitlab.com:sourceorg/atlantis-example.git", - "ssh_url": "git@gitlab.com:sourceorg/atlantis-example.git", - "http_url": "https://gitlab.com/sourceorg/atlantis-example.git" - }, - "target": { - "id": 4580910, - "name": "atlantis-example", - "description": "", - "web_url": "https://gitlab.com/lkysow/atlantis-example", - "avatar_url": null, - "git_ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", - "git_http_url": "https://gitlab.com/lkysow/atlantis-example.git", - "namespace": "lkysow", - "visibility_level": 20, - "path_with_namespace": "lkysow/atlantis-example", - "default_branch": "master", - "ci_config_path": null, - "homepage": "https://gitlab.com/lkysow/atlantis-example", - "url": "git@gitlab.com:lkysow/atlantis-example.git", - "ssh_url": "git@gitlab.com:lkysow/atlantis-example.git", - "http_url": "https://gitlab.com/lkysow/atlantis-example.git" - }, - "last_commit": { - "id": "d2eae324ca26242abca45d7b49d582cddb2a4f15", - "message": "Update main.tf", - "timestamp": "2018-12-12T16:15:10Z", - "url": "https://gitlab.com/lkysow/atlantis-example/commit/d2eae324ca26242abca45d7b49d582cddb2a4f15", - "author": { - "name": "Luke Kysow", - "email": "lkysow@gmail.com" - } - }, - "work_in_progress": true, - "total_time_spent": 0, - "human_total_time_spent": null, - "human_time_estimate": null, - "action": "open" - }, - "labels": [ - - ], - "changes": { - "author_id": { - "previous": null, - "current": 1755902 - }, - "created_at": { - "previous": null, - "current": "2018-12-12 16:15:21 UTC" - }, - "description": { - "previous": null, - "current": "" - }, - "id": { - "previous": null, - "current": 20809239 - }, - "iid": { - "previous": null, - "current": 12 - }, - "merge_params": { - "previous": { - }, - "current": { - "force_remove_source_branch": false - } - }, - "source_branch": { - "previous": null, - "current": "patch-1" - }, - "source_project_id": { - "previous": null, - "current": 4580910 - }, - "target_branch": { - "previous": null, - "current": "master" - }, - "target_project_id": { - "previous": null, - "current": 4580910 - }, - "title": { - "previous": null, - "current": "Update main.tf" - }, - "updated_at": { - "previous": null, - "current": "2018-12-12 16:15:21 UTC" - }, - "total_time_spent": { - "previous": null, - "current": 0 - } - }, - "repository": { - "name": "atlantis-example", - "url": "git@gitlab.com:lkysow/atlantis-example.git", - "description": "", - "homepage": "https://gitlab.com/lkysow/atlantis-example" - } -} From 715eaa37d3073ddaa4daa8c7beebd292fee99224 Mon Sep 17 00:00:00 2001 From: Jonas Bechstein Date: Thu, 25 Aug 2022 11:22:52 +0200 Subject: [PATCH 3/3] add tests for close actions --- server/events/event_parser_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/events/event_parser_test.go b/server/events/event_parser_test.go index 2c838fd9df..6df7046158 100644 --- a/server/events/event_parser_test.go +++ b/server/events/event_parser_test.go @@ -164,11 +164,19 @@ func TestParseGithubPullEvent(t *testing.T) { } func TestParseGithubPullEventFromDraft(t *testing.T) { + // verify that close event treated as 'close' events by default + closeEvent := deepcopy.Copy(PullEvent).(github.PullRequestEvent) + closeEvent.Action = github.String("closed") + closeEvent.PullRequest.Draft = github.Bool(true) + + _, evType, _, _, _, err := parser.ParseGithubPullEvent(&closeEvent) + Ok(t, err) + Equals(t, models.ClosedPullEvent, evType) + // verify that draft PRs are treated as 'other' events by default testEvent := deepcopy.Copy(PullEvent).(github.PullRequestEvent) - draftPR := true - testEvent.PullRequest.Draft = &draftPR - _, evType, _, _, _, err := parser.ParseGithubPullEvent(&testEvent) + testEvent.PullRequest.Draft = github.Bool(true) + _, evType, _, _, _, err = parser.ParseGithubPullEvent(&testEvent) Ok(t, err) Equals(t, models.OtherPullEvent, evType) // verify that drafts are planned if requested