Skip to content

Commit 2d9b230

Browse files
authored
feat: skip comment when no change flag (#24)
1 parent 0cb63c9 commit 2d9b230

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

pkg/cli/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func New(flags *LDFlags) *cli.App {
3939
Name: "patch",
4040
Usage: "update an existing comment instead of creating a new comment. If there is no existing comment, a new comment is created.",
4141
},
42+
&cli.BoolFlag{
43+
Name: "skip-no-changes",
44+
Usage: "If there is no change tfcmt updates a label but doesn't post a comment",
45+
},
4246
},
4347
},
4448
{

pkg/cli/var.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func parseOpts(ctx *cli.Context, cfg *config.Config) error {
4444
cfg.CI.Link = buildURL
4545
}
4646

47+
if ctx.IsSet("skip-no-changes") {
48+
cfg.Terraform.Plan.WhenNoChanges.DisableComment = ctx.Bool("skip-no-changes")
49+
}
50+
4751
vars := ctx.StringSlice("var")
4852
vm := make(map[string]string, len(vars))
4953
if err := parseVarOpts(vars, vm); err != nil {

pkg/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ type WhenDestroy struct {
7070

7171
// WhenNoChanges is a configuration to add a label when the plan result contains no change
7272
type WhenNoChanges struct {
73-
Label string
74-
Color string `yaml:"label_color"`
73+
Label string
74+
Color string `yaml:"label_color"`
75+
DisableComment bool `yaml:"disable_comment"`
7576
}
7677

7778
// WhenPlanError is a configuration to notify the plan result returns an error

pkg/controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (ctrl *Controller) getNotifier(ctx context.Context) (notifier.Notifier, err
188188
EmbeddedVarNames: ctrl.Config.EmbeddedVarNames,
189189
Templates: ctrl.Config.Templates,
190190
Patch: ctrl.Config.PlanPatch,
191+
SkipNoChanges: ctrl.Config.Terraform.Plan.WhenNoChanges.DisableComment,
191192
})
192193
if err != nil {
193194
return nil, err

pkg/notifier/gitlab/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Config struct {
4949
Templates map[string]string
5050
UseRawOutput bool
5151
Patch bool
52+
SkipNoChanges bool
5253
}
5354

5455
// MergeRequest represents GitLab Merge Request metadata

pkg/notifier/gitlab/notify.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func (g *NotifyService) Notify(param notifier.ParamExec) (int, error) { //nolint
8989
logE.WithField("size", len(comments)).Debug("list comments")
9090
}
9191

92+
if result.HasNoChanges && result.Warning == "" && len(errMsgs) == 0 && cfg.SkipNoChanges {
93+
logE.Debug("skip posting a comment because there is no change")
94+
return result.ExitCode, nil
95+
}
96+
9297
logE.Debug("create a comment")
9398

9499
if err := g.client.Comment.Post(body, PostOptions{

pkg/notifier/gitlab/notify_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,33 @@ func TestNotifyNotify(t *testing.T) { //nolint:maintidx
207207
ok: true,
208208
exitCode: 0,
209209
},
210+
{
211+
name: "valid with no change, then skip comment",
212+
createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI {
213+
api := gitlabmock.NewMockAPI(ctrl)
214+
api.EXPECT().CreateMergeRequestNote(gomock.Any(), gomock.Any()).Times(0)
215+
return api
216+
},
217+
config: Config{
218+
Token: "token",
219+
NameSpace: "namespace",
220+
Project: "project",
221+
MR: MergeRequest{
222+
Revision: "",
223+
Number: 1,
224+
},
225+
Parser: terraform.NewPlanParser(),
226+
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
227+
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
228+
SkipNoChanges: true,
229+
},
230+
paramExec: notifier.ParamExec{
231+
CombinedOutput: "No changes. Infrastructure is up-to-date.",
232+
ExitCode: 0,
233+
},
234+
ok: true,
235+
exitCode: 0,
236+
},
210237
{
211238
name: "valid, contains destroy, but not to notify",
212239
createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI {

0 commit comments

Comments
 (0)