Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further parse Gitlab MergeRequest Update events #1301

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ type EventParsing interface {
pull models.PullRequest, pullEventType models.PullRequestEventType,
baseRepo models.Repo, headRepo models.Repo, user models.User, err error)

// ParseGitlabMergeRequestUpdateEvent dives deeper into Gitlab merge request update events to check
// if Atlantis should handle events or not. Atlantis should ignore events which dont change the MR content
// We assume that 1 event carries multiple events, so firstly need to check for events triggering Atlantis planning
// Default 'unknown' event to 'models.UpdatedPullEvent'
ParseGitlabMergeRequestUpdateEvent(event gitlab.MergeEvent) models.PullRequestEventType

// ParseGitlabMergeRequestCommentEvent parses GitLab merge request comment
// events.
// baseRepo is the repo the merge request will be merged into.
Expand Down Expand Up @@ -515,6 +521,36 @@ func (e *EventParser) ParseGithubRepo(ghRepo *github.Repository) (models.Repo, e
return models.NewRepo(models.Github, ghRepo.GetFullName(), ghRepo.GetCloneURL(), e.GithubUser, e.GithubToken)
}

// ParseGitlabMergeRequestUpdateEvent dives deeper into Gitlab merge request update events
func (e *EventParser) ParseGitlabMergeRequestUpdateEvent(event gitlab.MergeEvent) models.PullRequestEventType {
// New commit to opened MR
if len(event.ObjectAttributes.OldRev) > 0 {
return models.UpdatedPullEvent
}

// Update Assignee
if len(event.Changes.Assignees.Previous) > 0 || len(event.Changes.Assignees.Current) > 0 {
return models.OtherPullEvent
}

// Update Description
if len(event.Changes.Description.Previous) > 0 || len(event.Changes.Description.Current) > 0 {
return models.OtherPullEvent
}

// Update Labels
if len(event.Changes.Labels.Previous) > 0 || len(event.Changes.Labels.Current) > 0 {
return models.OtherPullEvent
}

//Update Title
if len(event.Changes.Title.Previous) > 0 || len(event.Changes.Title.Current) > 0 {
return models.OtherPullEvent
}

return models.UpdatedPullEvent
}

// ParseGitlabMergeRequestEvent parses GitLab merge request events.
// pull is the parsed merge request.
// See EventParsing for return value docs.
Expand Down Expand Up @@ -550,7 +586,7 @@ func (e *EventParser) ParseGitlabMergeRequestEvent(event gitlab.MergeEvent) (pul
case "open":
eventType = models.OpenedPullEvent
case "update":
eventType = models.UpdatedPullEvent
eventType = e.ParseGitlabMergeRequestUpdateEvent(event)
case "merge", "close":
eventType = models.ClosedPullEvent
default:
Expand Down
55 changes: 51 additions & 4 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,57 @@ func TestParseGitlabMergeEvent_Subgroup(t *testing.T) {
Equals(t, models.User{Username: "lkysow"}, actUser)
}

func TestParseGitlabMergeEvent_Update_ActionType(t *testing.T) {
cases := []struct {
filename string
exp models.PullRequestEventType
}{
{
filename: "gitlab-merge-request-event-update-title.json",
exp: models.OtherPullEvent,
},
{
filename: "gitlab-merge-request-event-update-new-commit.json",
exp: models.UpdatedPullEvent,
},
{
filename: "gitlab-merge-request-event-update-labels.json",
exp: models.OtherPullEvent,
},
{
filename: "gitlab-merge-request-event-update-description.json",
exp: models.OtherPullEvent,
},
{
filename: "gitlab-merge-request-event-update-assignee.json",
exp: models.OtherPullEvent,
},
{
filename: "gitlab-merge-request-event-update-mixed.json",
exp: models.OtherPullEvent,
},
{
filename: "gitlab-merge-request-event-update-target-branch.json",
exp: models.UpdatedPullEvent,
},
}

for _, c := range cases {
t.Run(c.filename, func(t *testing.T) {
path := filepath.Join("testdata", c.filename)
bytes, err := ioutil.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, c.exp, evType)
})
}
}

func TestParseGitlabMergeEvent_ActionType(t *testing.T) {
cases := []struct {
action string
Expand All @@ -438,10 +489,6 @@ func TestParseGitlabMergeEvent_ActionType(t *testing.T) {
action: "open",
exp: models.OpenedPullEvent,
},
{
action: "update",
exp: models.UpdatedPullEvent,
},
{
action: "merge",
exp: models.ClosedPullEvent,
Expand Down
47 changes: 44 additions & 3 deletions server/events/mocks/mock_event_parsing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 156 additions & 0 deletions server/events/testdata/gitlab-merge-request-event-update-assignee.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"object_kind": "merge_request",
"event_type": "merge_request",
"user": {
"name": "Quan Hoang",
"username": "quan.hoang",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/255/avatar.png",
"email": "[email protected]"
},
"project": {
"id": 910,
"name": "Test Gitlab Webhook",
"description": "",
"web_url": "https://gitlab.com/quan.hoang/atlantis-example",
"avatar_url": null,
"git_ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"git_http_url": "https://gitlab.com/quan.hoang/atlantis-example.git",
"namespace": "Quan Hoang",
"visibility_level": 0,
"path_with_namespace": "quan.hoang/atlantis-example",
"default_branch": "master",
"ci_config_path": null,
"homepage": "https://gitlab.com/quan.hoang/atlantis-example",
"url": "[email protected]:quan.hoang/atlantis-example.git",
"ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"http_url": "https://gitlab.com/quan.hoang/atlantis-example.git"
},
"object_attributes": {
"assignee_id": 255,
"author_id": 255,
"created_at": "2020-12-08 04:04:23 UTC",
"description": "New description",
"head_pipeline_id": null,
"id": 41728,
"iid": 3,
"last_edited_at": "2020-12-08 04:05:16 UTC",
"last_edited_by_id": 255,
"merge_commit_sha": null,
"merge_error": null,
"merge_params": {
"force_remove_source_branch": "1"
},
"merge_status": "can_be_merged",
"merge_user_id": null,
"merge_when_pipeline_succeeds": false,
"milestone_id": null,
"source_branch": "get-event-payload",
"source_project_id": 910,
"state_id": 1,
"target_branch": "master",
"target_project_id": 910,
"time_estimate": 0,
"title": "Add new file",
"updated_at": "2020-12-08 04:05:58 UTC",
"updated_by_id": 255,
"url": "https://gitlab.com/quan.hoang/atlantis-example/-/merge_requests/3",
"source": {
"id": 910,
"name": "Test Gitlab Webhook",
"description": "",
"web_url": "https://gitlab.com/quan.hoang/atlantis-example",
"avatar_url": null,
"git_ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"git_http_url": "https://gitlab.com/quan.hoang/atlantis-example.git",
"namespace": "Quan Hoang",
"visibility_level": 0,
"path_with_namespace": "quan.hoang/atlantis-example",
"default_branch": "master",
"ci_config_path": null,
"homepage": "https://gitlab.com/quan.hoang/atlantis-example",
"url": "[email protected]:quan.hoang/atlantis-example.git",
"ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"http_url": "https://gitlab.com/quan.hoang/atlantis-example.git"
},
"target": {
"id": 910,
"name": "Test Gitlab Webhook",
"description": "",
"web_url": "https://gitlab.com/quan.hoang/atlantis-example",
"avatar_url": null,
"git_ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"git_http_url": "https://gitlab.com/quan.hoang/atlantis-example.git",
"namespace": "Quan Hoang",
"visibility_level": 0,
"path_with_namespace": "quan.hoang/atlantis-example",
"default_branch": "master",
"ci_config_path": null,
"homepage": "https://gitlab.com/quan.hoang/atlantis-example",
"url": "[email protected]:quan.hoang/atlantis-example.git",
"ssh_url": "[email protected]:quan.hoang/atlantis-example.git",
"http_url": "https://gitlab.com/quan.hoang/atlantis-example.git"
},
"last_commit": {
"id": "1ca7e340460796fe3eb17e2ea1b383ac4a1c95b7",
"message": "Add abc.txt\n",
"title": "Add abc.txt",
"timestamp": "2020-12-08T11:03:29+07:00",
"url": "https://gitlab.com/quan.hoang/atlantis-example/-/commit/1ca7e340460796fe3eb17e2ea1b383ac4a1c95b7",
"author": {
"name": "Quan Hoang",
"email": "[email protected]"
}
},
"work_in_progress": false,
"total_time_spent": 0,
"human_total_time_spent": null,
"human_time_estimate": null,
"assignee_ids": [
255,
19
],
"state": "opened",
"action": "update"
},
"labels": [
{
"id": 340,
"title": "aaaa",
"color": "#0033CC",
"project_id": 910,
"created_at": "2020-12-08 04:05:34 UTC",
"updated_at": "2020-12-08 04:05:34 UTC",
"template": false,
"description": null,
"type": "ProjectLabel",
"group_id": null
}
],
"changes": {
"assignees": {
"previous": [],
"current": [
{
"name": "Quan Hoang",
"username": "quan.hoang",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/255/avatar.png",
"email": "[email protected]"
}
]
}
},
"repository": {
"name": "Test Gitlab Webhook",
"url": "[email protected]:quan.hoang/atlantis-example.git",
"description": "",
"homepage": "https://gitlab.com/quan.hoang/atlantis-example"
},
"assignees": [
{
"name": "Quan Hoang",
"username": "quan.hoang",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/255/avatar.png",
"email": "[email protected]"
}
]
}
Loading