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

feat: add ability to take action on a MR depending on various setttings #8

Merged
merged 13 commits into from
May 8, 2024
Merged
Prev Previous commit
Next Next commit
fix: handle errors returned from gitlab
jippi committed May 7, 2024
commit 0066c033352668f1717512b3790e6481f41373e5
7 changes: 7 additions & 0 deletions .scm-engine.example.yml
Original file line number Diff line number Diff line change
@@ -14,6 +14,13 @@ actions:
- action: comment
message: "As promised, we're closing the MR due to inactivity, bye bye"

- name: Approve MR if the 'break-glass-approve' label is configured
if: not merge_request.approved && merge_request.has_label("break-glass-approve")
then:
- action: approve
- action: comment
message: "Approving the MR since it has the 'break-glass-approve' label. Talk to ITGC about this!"

label:
- name: lang/go
color: "$indigo"
12 changes: 9 additions & 3 deletions pkg/scm/gitlab/client_actioner.go
Original file line number Diff line number Diff line change
@@ -35,10 +35,14 @@ func (c *Client) ApplyStep(ctx context.Context, update *scm.UpdateMergeRequestOp
update.DiscussionLocked = gitlab.Ptr(false)

case "approve":
c.wrapped.MergeRequestApprovals.ApproveMergeRequest(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx), &gitlab.ApproveMergeRequestOptions{})
_, _, err := c.wrapped.MergeRequestApprovals.ApproveMergeRequest(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx), &gitlab.ApproveMergeRequestOptions{})

return err

case "unapprove":
c.wrapped.MergeRequestApprovals.UnapproveMergeRequest(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx))
_, err := c.wrapped.MergeRequestApprovals.UnapproveMergeRequest(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx))

return err

case "comment":
msg, ok := step["message"]
@@ -55,10 +59,12 @@ func (c *Client) ApplyStep(ctx context.Context, update *scm.UpdateMergeRequestOp
return errors.New("step field 'message' must not be an empty string")
}

c.wrapped.Notes.CreateMergeRequestNote(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx), &gitlab.CreateMergeRequestNoteOptions{
_, _, err := c.wrapped.Notes.CreateMergeRequestNote(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx), &gitlab.CreateMergeRequestNoteOptions{
Body: gitlab.Ptr(msgString),
})

return err

default:
return fmt.Errorf("GitLab client does not know how to apply action %q", step["action"])
}