Skip to content

Commit

Permalink
feat: add --dry flag
Browse files Browse the repository at this point in the history
Fixes #14

Signed-off-by: Christian Winther <[email protected]>
  • Loading branch information
jippi committed May 9, 2024
1 parent bd50d4e commit 387c127
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 35 deletions.
1 change: 1 addition & 0 deletions cmd/cmd_evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

func Evaluate(cCtx *cli.Context) error {
ctx := state.ContextWithProjectID(cCtx.Context, cCtx.String(FlagSCMProject))
ctx = state.ContextWithDryRun(ctx, cCtx.Bool(FlagDryRun))

cfg, err := config.LoadFile(cCtx.String(FlagConfigFile))
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/cmd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ func Server(cCtx *cli.Context) error { //nolint:unparam
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
BaseContext: func(l net.Listener) context.Context {
return cCtx.Context
ctx := state.ContextWithDryRun(cCtx.Context, cCtx.Bool(FlagDryRun))

return ctx
},
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/conventions.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

const (
FlagConfigFile = "config"
FlagAPIToken = "api-token"
FlagSCMProject = "project"
FlagSCMBaseURL = "base-url"
FlagConfigFile = "config"
FlagDryRun = "dry"
FlagMergeRequestID = "id"
FlagWebhookSecret = "webhook-secret"
FlagSCMBaseURL = "base-url"
FlagSCMProject = "project"
FlagServerListen = "listen-port"
FlagWebhookSecret = "webhook-secret"
)
14 changes: 14 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, mr st
}

func updateMergeRequest(ctx context.Context, client scm.Client, update *scm.UpdateMergeRequestOptions) error {
if state.IsDryRun(ctx) {
slogctx.Info(ctx, "Payload", slog.Any("changes", update))

return nil
}

_, err := client.MergeRequests().Update(ctx, update)

return err
Expand Down Expand Up @@ -117,6 +123,10 @@ func syncLabels(ctx context.Context, client scm.Client, remote []*scm.Label, req

slogctx.Info(ctx, "Creating label", slog.String("label", label.Name))

if state.IsDryRun(ctx) {
continue
}

_, resp, err := client.Labels().Create(ctx, &scm.CreateLabelOptions{
Name: &label.Name, //nolint:gosec
Color: &label.Color, //nolint:gosec
Expand Down Expand Up @@ -148,6 +158,10 @@ func syncLabels(ctx context.Context, client scm.Client, remote []*scm.Label, req

slogctx.Info(ctx, "Updating label", slog.String("label", label.Name))

if state.IsDryRun(ctx) {
continue
}

_, _, err := client.Labels().Update(ctx, &scm.UpdateLabelOptions{
Name: &label.Name, //nolint:gosec
Color: &label.Color, //nolint:gosec
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func main() {
"SCM_ENGINE_TOKEN",
},
},

&cli.StringFlag{
Name: cmd.FlagSCMBaseURL,
Usage: "Base URL for the SCM instance",
Expand All @@ -70,6 +69,10 @@ func main() {
"CI_SERVER_URL",
},
},
&cli.BoolFlag{
Name: cmd.FlagDryRun,
Usage: "Dry run, don't actually _do_ actions, just print them",
},
},
Commands: []*cli.Command{
{
Expand Down
20 changes: 20 additions & 0 deletions pkg/scm/gitlab/client_actioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"log/slog"

"github.com/jippi/scm-engine/pkg/scm"
"github.com/jippi/scm-engine/pkg/state"
slogctx "github.com/veqryn/slog-context"
"github.com/xanzy/go-gitlab"
)

Expand Down Expand Up @@ -35,11 +37,23 @@ func (c *Client) ApplyStep(ctx context.Context, update *scm.UpdateMergeRequestOp
update.DiscussionLocked = gitlab.Ptr(false)

case "approve":
if state.IsDryRun(ctx) {
slogctx.Info(ctx, "Approving MR")

return nil
}

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

return err

case "unapprove":
if state.IsDryRun(ctx) {
slogctx.Info(ctx, "Unapproving MR")

return nil
}

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

return err
Expand All @@ -59,6 +73,12 @@ func (c *Client) ApplyStep(ctx context.Context, update *scm.UpdateMergeRequestOp
return errors.New("step field 'message' must not be an empty string")
}

if state.IsDryRun(ctx) {
slogctx.Info(ctx, "Commenting on MR", slog.String("message", msgString))

return nil
}

_, _, err := c.wrapped.Notes.CreateMergeRequestNote(state.ProjectIDFromContext(ctx), state.MergeRequestIDFromContextInt(ctx), &gitlab.CreateMergeRequestNoteOptions{
Body: gitlab.Ptr(msgString),
})
Expand Down
15 changes: 14 additions & 1 deletion pkg/state/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"context"
"log/slog"
"strconv"

slogctx "github.com/veqryn/slog-context"
Expand All @@ -11,6 +12,7 @@ type contextKey uint

const (
projectID contextKey = iota
dryRun
mergeRequestID
)

Expand All @@ -27,12 +29,23 @@ func ProjectIDFromContext(ctx context.Context) string {
}

func ContextWithProjectID(ctx context.Context, value string) context.Context {
ctx = slogctx.With(ctx, "project_id", value)
ctx = slogctx.With(ctx, slog.String("project_id", value))
ctx = context.WithValue(ctx, projectID, value)

return ctx
}

func ContextWithDryRun(ctx context.Context, dry bool) context.Context {
ctx = slogctx.With(ctx, slog.Bool("dry_run", dry))
ctx = context.WithValue(ctx, dryRun, dry)

return ctx
}

func IsDryRun(ctx context.Context) bool {
return ctx.Value(dryRun).(bool) //nolint:forcetypeassert
}

func ContextWithMergeRequestID(ctx context.Context, id string) context.Context {
ctx = slogctx.With(ctx, "merge_request_id", id)
ctx = context.WithValue(ctx, mergeRequestID, id)
Expand Down
6 changes: 0 additions & 6 deletions pkg/tui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/reugn/pkgslog"
slogmulti "github.com/samber/slog-multi"
slogctx "github.com/veqryn/slog-context"
slogdedup "github.com/veqryn/slog-dedup"
Expand All @@ -33,11 +32,6 @@ func NewContext(ctx context.Context, stdout, stderr io.Writer) context.Context {
ctx,
slog.New(
slogmulti.
Pipe(
func(next slog.Handler) slog.Handler {
return pkgslog.NewPackageHandler(next, packageLogLevels())
},
).
Pipe(
slogctx.NewMiddleware(&slogctx.HandlerOptions{}),
).
Expand Down
24 changes: 2 additions & 22 deletions pkg/tui/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,6 @@ func ParseLogLevel(name string, fallback slog.Level) slog.Level {
}
}

func pkgLogLevel(name string, fallback slog.Level) slog.Level {
return ParseLogLevel(os.Getenv(name+"_LOG_LEVEL"), fallback)
}

func packageLogLevels() map[string]slog.Level {
logLevel := ParseLogLevel(os.Getenv("LOG_LEVEL"), slog.LevelInfo)

lowestOf := func(in slog.Level) slog.Level {
if in < logLevel {
return in
}

return logLevel
}

return map[string]slog.Level{
pkgPrefix + "/pkg/parser": pkgLogLevel("PARSER", lowestOf(slog.LevelWarn)),
pkgPrefix + "/pkg/scanner": pkgLogLevel("SCANNER", lowestOf(slog.LevelWarn)),
}
}

func logHandler(out io.Writer) slog.Handler {
logLevel := ParseLogLevel(os.Getenv("LOG_LEVEL"), slog.LevelInfo)

Expand All @@ -69,7 +48,8 @@ func logHandler(out io.Writer) slog.Handler {
return devslog.NewHandler(
out,
&devslog.Options{
SortKeys: true,
SortKeys: true,
MaxSlicePrintSize: 999,
HandlerOptions: &slog.HandlerOptions{
Level: logLevel,
AddSource: logLevel == slog.LevelDebug,
Expand Down

0 comments on commit 387c127

Please sign in to comment.