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

ignore GitLab Draft MRs #2468

Merged
merged 3 commits into from
Aug 25, 2022
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
24 changes: 16 additions & 8 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
37 changes: 34 additions & 3 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -380,6 +388,29 @@ func TestParseGitlabMergeEvent(t *testing.T) {
Equals(t, models.ClosedPullState, pull.State)
}

func TestParseGitlabMergeEventFromDraft(t *testing.T) {
path := filepath.Join("testdata", "gitlab-merge-request-event.json")
bytes, err := os.ReadFile(path)
Ok(t, err)

var event gitlab.MergeEvent
err = json.Unmarshal(bytes, &event)
Ok(t, err)

testEvent := deepcopy.Copy(event).(gitlab.MergeEvent)
testEvent.ObjectAttributes.WorkInProgress = true

_, evType, _, _, _, err := parser.ParseGitlabMergeRequestEvent(testEvent)
Ok(t, err)
Equals(t, models.OtherPullEvent, evType)

becjon marked this conversation as resolved.
Show resolved Hide resolved
parser.AllowDraftPRs = true
defer func() { parser.AllowDraftPRs = false }()
_, evType, _, _, _, err = parser.ParseGitlabMergeRequestEvent(testEvent)
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) {
Expand Down