From 85cb1f09896dd4a497240c9c654289582dad6f02 Mon Sep 17 00:00:00 2001 From: Lukas Hass Date: Mon, 29 Jan 2024 22:47:24 +0100 Subject: [PATCH 1/4] feat: disallow self approval --- plugins/auto_merge/checks/has_assignee.go | 25 +++++++++++++++++++ .../auto_merge/checks/has_enough_approvals.go | 6 ++--- 2 files changed, 28 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..c43686e 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) && approver.User.Username != assignee.Username { approvedBy = append(approvedBy, approver.User.Username) } } From ffd8c05bac16b9df8823649540b365e0f5585627 Mon Sep 17 00:00:00 2001 From: Lukas Hass Date: Mon, 29 Jan 2024 22:56:54 +0100 Subject: [PATCH 2/4] register check --- plugins/auto_merge/merge_checks.go | 1 + 1 file changed, 1 insertion(+) 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, }, From bf25570a423c22777d1b82b4fc0b622651bf79be Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 31 Jan 2024 11:37:49 +0100 Subject: [PATCH 3/4] Update plugins/auto_merge/checks/has_enough_approvals.go Co-authored-by: Anbraten --- plugins/auto_merge/checks/has_enough_approvals.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/auto_merge/checks/has_enough_approvals.go b/plugins/auto_merge/checks/has_enough_approvals.go index c43686e..7267c95 100644 --- a/plugins/auto_merge/checks/has_enough_approvals.go +++ b/plugins/auto_merge/checks/has_enough_approvals.go @@ -68,7 +68,7 @@ func (check HasEnoughApprovalsCheck) getApprovals(approvedByAll []*gitlab.MergeR var approvedBy []string for _, approver := range approvedByAll { - if utils.StringInSlice(approver.User.Username, possibleApprovers) && approver.User.Username != assignee.Username { + if utils.StringInSlice(approver.User.Username, possibleApprovers) && assignee != nil && approver.User.Username != assignee.Username { approvedBy = append(approvedBy, approver.User.Username) } } From 1956ad36cf23d61b213cca1ad125f1fd6e208be8 Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 31 Jan 2024 11:40:26 +0100 Subject: [PATCH 4/4] Update plugins/auto_merge/checks/has_enough_approvals.go Co-authored-by: Anbraten --- plugins/auto_merge/checks/has_enough_approvals.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/auto_merge/checks/has_enough_approvals.go b/plugins/auto_merge/checks/has_enough_approvals.go index 7267c95..f7d2088 100644 --- a/plugins/auto_merge/checks/has_enough_approvals.go +++ b/plugins/auto_merge/checks/has_enough_approvals.go @@ -68,7 +68,7 @@ func (check HasEnoughApprovalsCheck) getApprovals(approvedByAll []*gitlab.MergeR var approvedBy []string for _, approver := range approvedByAll { - if utils.StringInSlice(approver.User.Username, possibleApprovers) && assignee != nil && approver.User.Username != assignee.Username { + if utils.StringInSlice(approver.User.Username, possibleApprovers) && (assignee == nil || approver.User.Username != assignee.Username) { approvedBy = append(approvedBy, approver.User.Username) } }