|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + |
| 9 | + go_github "github.com/google/go-github/v62/github" |
| 10 | + "github.com/jippi/scm-engine/pkg/scm" |
| 11 | + "github.com/jippi/scm-engine/pkg/state" |
| 12 | + slogctx "github.com/veqryn/slog-context" |
| 13 | +) |
| 14 | + |
| 15 | +func (c *Client) ApplyStep(ctx context.Context, update *scm.UpdateMergeRequestOptions, step scm.EvaluationActionStep) error { |
| 16 | + owner, repo := ownerAndRepo(ctx) |
| 17 | + |
| 18 | + action, ok := step["action"] |
| 19 | + if !ok { |
| 20 | + return errors.New("step is missing an 'action' key") |
| 21 | + } |
| 22 | + |
| 23 | + actionString, ok := action.(string) |
| 24 | + if !ok { |
| 25 | + return fmt.Errorf("step field 'action' must be of type string, got %T", action) |
| 26 | + } |
| 27 | + |
| 28 | + switch actionString { |
| 29 | + case "add_label": |
| 30 | + name, ok := step["name"] |
| 31 | + if !ok { |
| 32 | + return errors.New("step field 'name' is required, but missing") |
| 33 | + } |
| 34 | + |
| 35 | + nameVal, ok := name.(string) |
| 36 | + if !ok { |
| 37 | + return errors.New("step field 'name' must be a string") |
| 38 | + } |
| 39 | + |
| 40 | + labels := update.AddLabels |
| 41 | + if labels == nil { |
| 42 | + labels = &scm.LabelOptions{} |
| 43 | + } |
| 44 | + |
| 45 | + tmp := append(*labels, nameVal) |
| 46 | + |
| 47 | + update.AddLabels = &tmp |
| 48 | + |
| 49 | + case "remove_label": |
| 50 | + name, ok := step["name"] |
| 51 | + if !ok { |
| 52 | + return errors.New("step field 'name' is required, but missing") |
| 53 | + } |
| 54 | + |
| 55 | + nameVal, ok := name.(string) |
| 56 | + if !ok { |
| 57 | + return errors.New("step field 'name' must be a string") |
| 58 | + } |
| 59 | + |
| 60 | + labels := update.RemoveLabels |
| 61 | + if labels == nil { |
| 62 | + labels = &scm.LabelOptions{} |
| 63 | + } |
| 64 | + |
| 65 | + tmp := append(*labels, nameVal) |
| 66 | + |
| 67 | + update.AddLabels = &tmp |
| 68 | + |
| 69 | + case "close": |
| 70 | + update.StateEvent = scm.Ptr("close") |
| 71 | + |
| 72 | + case "reopen": |
| 73 | + update.StateEvent = scm.Ptr("reopen") |
| 74 | + |
| 75 | + case "lock_discussion": |
| 76 | + update.DiscussionLocked = scm.Ptr(true) |
| 77 | + |
| 78 | + case "unlock_discussion": |
| 79 | + update.DiscussionLocked = scm.Ptr(false) |
| 80 | + |
| 81 | + case "approve": |
| 82 | + if state.IsDryRun(ctx) { |
| 83 | + slogctx.Info(ctx, "Approving MR") |
| 84 | + |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + _, _, err := c.wrapped.PullRequests.CreateReview(ctx, owner, repo, state.MergeRequestIDInt(ctx), &go_github.PullRequestReviewRequest{ |
| 89 | + Event: scm.Ptr("APPROVE"), |
| 90 | + }) |
| 91 | + |
| 92 | + return err |
| 93 | + |
| 94 | + case "unapprove": |
| 95 | + if state.IsDryRun(ctx) { |
| 96 | + slogctx.Info(ctx, "Unapproving MR") |
| 97 | + |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + _, _, err := c.wrapped.PullRequests.CreateReview(ctx, owner, repo, state.MergeRequestIDInt(ctx), &go_github.PullRequestReviewRequest{}) |
| 102 | + |
| 103 | + return err |
| 104 | + |
| 105 | + case "comment": |
| 106 | + msg, ok := step["message"] |
| 107 | + if !ok { |
| 108 | + return errors.New("step field 'message' is required, but missing") |
| 109 | + } |
| 110 | + |
| 111 | + msgString, ok := msg.(string) |
| 112 | + if !ok { |
| 113 | + return fmt.Errorf("step field 'message' must be a string, got %T", msg) |
| 114 | + } |
| 115 | + |
| 116 | + if len(msgString) == 0 { |
| 117 | + return errors.New("step field 'message' must not be an empty string") |
| 118 | + } |
| 119 | + |
| 120 | + if state.IsDryRun(ctx) { |
| 121 | + slogctx.Info(ctx, "Commenting on MR", slog.String("message", msgString)) |
| 122 | + |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + _, _, err := c.wrapped.PullRequests.CreateComment(ctx, owner, repo, state.MergeRequestIDInt(ctx), &go_github.PullRequestComment{ |
| 127 | + Body: scm.Ptr(msgString), |
| 128 | + }) |
| 129 | + |
| 130 | + return err |
| 131 | + |
| 132 | + default: |
| 133 | + return fmt.Errorf("GitLab client does not know how to apply action %q", step["action"]) |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
0 commit comments