From f1bb5765aee47255a1826f2ae0ad1c65ab3bb5dc Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 31 Jan 2024 11:42:07 +0100 Subject: [PATCH] feat: disallow self approval (#73) Co-authored-by: Anbraten --- plugins/auto_merge/checks/has_assignee.go | 25 +++++++++++++++++++ .../auto_merge/checks/has_enough_approvals.go | 6 ++--- plugins/auto_merge/merge_checks.go | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 plugins/auto_merge/checks/has_assignee.go diff --git a/plugins/auto_merge/checks/has_assignee.go b/plugins/auto_merge/checks/has_assignee.go new file mode 100644 index 0000000..58a45ba --- /dev/null +++ b/plugins/auto_merge/checks/has_assignee.go @@ -0,0 +1,25 @@ +package checks + +import ( + "github.com/GEPROG/lassie-bot-dog/plugins/auto_merge/config" + "github.com/xanzy/go-gitlab" +) + +type HasAssignee struct { +} + +func (check HasAssignee) Check(_ *config.AutoMergeConfig, _ *gitlab.Project, mergeRequest *gitlab.MergeRequest) bool { + return mergeRequest.Assignee != nil +} + +func (check HasAssignee) Name() string { + return "has-assignee" +} + +func (check HasAssignee) PassedText(_ int) string { + return "Someone is assigned to your Merge-Request" +} + +func (check HasAssignee) FailedText(_ int) string { + return "No one is assigned to your Merge-Request" +} diff --git a/plugins/auto_merge/checks/has_enough_approvals.go b/plugins/auto_merge/checks/has_enough_approvals.go index 665976c..f7d2088 100644 --- a/plugins/auto_merge/checks/has_enough_approvals.go +++ b/plugins/auto_merge/checks/has_enough_approvals.go @@ -36,7 +36,7 @@ func (check HasEnoughApprovalsCheck) Check(config *config.AutoMergeConfig, proje } // get amount of users that need to approve and already approved - approvedBy := len(check.getApprovals(approvals.ApprovedBy, neededApproval.Users)) + approvedBy := len(check.getApprovals(approvals.ApprovedBy, neededApproval.Users, mergeRequest.Assignee)) atLeast := utils.Max(neededApproval.AtLeast, 1) if approvedBy < atLeast { @@ -64,11 +64,11 @@ func (check HasEnoughApprovalsCheck) FailedText(mergeRequestID int) string { return fmt.Sprintf("You still need some review for your changes %s", missingLabels) } -func (check HasEnoughApprovalsCheck) getApprovals(approvedByAll []*gitlab.MergeRequestApproverUser, possibleApprovers []string) []string { +func (check HasEnoughApprovalsCheck) getApprovals(approvedByAll []*gitlab.MergeRequestApproverUser, possibleApprovers []string, assignee *gitlab.BasicUser) []string { var approvedBy []string for _, approver := range approvedByAll { - if utils.StringInSlice(approver.User.Username, possibleApprovers) { + if utils.StringInSlice(approver.User.Username, possibleApprovers) && (assignee == nil || approver.User.Username != assignee.Username) { approvedBy = append(approvedBy, approver.User.Username) } } diff --git a/plugins/auto_merge/merge_checks.go b/plugins/auto_merge/merge_checks.go index 94c84ad..b91bc5e 100644 --- a/plugins/auto_merge/merge_checks.go +++ b/plugins/auto_merge/merge_checks.go @@ -70,6 +70,7 @@ func (plugin AutoMergePlugin) setupMergeChecks() { checks.HasNoConflictsCheck{}, checks.HasNoOpenDiscussionsCheck{Client: plugin.Client}, checks.IsNotWorkInProgressCheck{}, + checks.HasAssignee{}, checks.PassesCICheck{ Client: plugin.Client, },