Skip to content

Commit

Permalink
feat: disallow self approval (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Anbraten <[email protected]>
  • Loading branch information
lukashass and anbraten authored Jan 31, 2024
1 parent e7d0de8 commit f1bb576
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
25 changes: 25 additions & 0 deletions plugins/auto_merge/checks/has_assignee.go
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 3 additions & 3 deletions plugins/auto_merge/checks/has_enough_approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions plugins/auto_merge/merge_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (plugin AutoMergePlugin) setupMergeChecks() {
checks.HasNoConflictsCheck{},
checks.HasNoOpenDiscussionsCheck{Client: plugin.Client},
checks.IsNotWorkInProgressCheck{},
checks.HasAssignee{},
checks.PassesCICheck{
Client: plugin.Client,
},
Expand Down

0 comments on commit f1bb576

Please sign in to comment.