Skip to content

Commit

Permalink
feat: skip comment when no change flag (#24)
Browse files Browse the repository at this point in the history
krrrr38 authored Aug 3, 2024

Verified

This commit was signed with the committer’s verified signature.
dr-bonez Aiden McClelland
1 parent 0cb63c9 commit 2d9b230
Showing 7 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/cli/app.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,10 @@ func New(flags *LDFlags) *cli.App {
Name: "patch",
Usage: "update an existing comment instead of creating a new comment. If there is no existing comment, a new comment is created.",
},
&cli.BoolFlag{
Name: "skip-no-changes",
Usage: "If there is no change tfcmt updates a label but doesn't post a comment",
},
},
},
{
4 changes: 4 additions & 0 deletions pkg/cli/var.go
Original file line number Diff line number Diff line change
@@ -44,6 +44,10 @@ func parseOpts(ctx *cli.Context, cfg *config.Config) error {
cfg.CI.Link = buildURL
}

if ctx.IsSet("skip-no-changes") {
cfg.Terraform.Plan.WhenNoChanges.DisableComment = ctx.Bool("skip-no-changes")
}

vars := ctx.StringSlice("var")
vm := make(map[string]string, len(vars))
if err := parseVarOpts(vars, vm); err != nil {
5 changes: 3 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -70,8 +70,9 @@ type WhenDestroy struct {

// WhenNoChanges is a configuration to add a label when the plan result contains no change
type WhenNoChanges struct {
Label string
Color string `yaml:"label_color"`
Label string
Color string `yaml:"label_color"`
DisableComment bool `yaml:"disable_comment"`
}

// WhenPlanError is a configuration to notify the plan result returns an error
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
@@ -188,6 +188,7 @@ func (ctrl *Controller) getNotifier(ctx context.Context) (notifier.Notifier, err
EmbeddedVarNames: ctrl.Config.EmbeddedVarNames,
Templates: ctrl.Config.Templates,
Patch: ctrl.Config.PlanPatch,
SkipNoChanges: ctrl.Config.Terraform.Plan.WhenNoChanges.DisableComment,
})
if err != nil {
return nil, err
1 change: 1 addition & 0 deletions pkg/notifier/gitlab/client.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ type Config struct {
Templates map[string]string
UseRawOutput bool
Patch bool
SkipNoChanges bool
}

// MergeRequest represents GitLab Merge Request metadata
5 changes: 5 additions & 0 deletions pkg/notifier/gitlab/notify.go
Original file line number Diff line number Diff line change
@@ -89,6 +89,11 @@ func (g *NotifyService) Notify(param notifier.ParamExec) (int, error) { //nolint
logE.WithField("size", len(comments)).Debug("list comments")
}

if result.HasNoChanges && result.Warning == "" && len(errMsgs) == 0 && cfg.SkipNoChanges {
logE.Debug("skip posting a comment because there is no change")
return result.ExitCode, nil
}

logE.Debug("create a comment")

if err := g.client.Comment.Post(body, PostOptions{
27 changes: 27 additions & 0 deletions pkg/notifier/gitlab/notify_test.go
Original file line number Diff line number Diff line change
@@ -207,6 +207,33 @@ func TestNotifyNotify(t *testing.T) { //nolint:maintidx
ok: true,
exitCode: 0,
},
{
name: "valid with no change, then skip comment",
createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI {
api := gitlabmock.NewMockAPI(ctrl)
api.EXPECT().CreateMergeRequestNote(gomock.Any(), gomock.Any()).Times(0)
return api
},
config: Config{
Token: "token",
NameSpace: "namespace",
Project: "project",
MR: MergeRequest{
Revision: "",
Number: 1,
},
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
SkipNoChanges: true,
},
paramExec: notifier.ParamExec{
CombinedOutput: "No changes. Infrastructure is up-to-date.",
ExitCode: 0,
},
ok: true,
exitCode: 0,
},
{
name: "valid, contains destroy, but not to notify",
createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI {

0 comments on commit 2d9b230

Please sign in to comment.