Skip to content

Commit

Permalink
feat: support skipping the post of a comment if there is no change
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed May 9, 2023
1 parent eba4d0e commit 638294e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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",
},
},
},
{
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func parseOpts(ctx *cli.Context, cfg *config.Config, envs []string) error {
cfg.Output = output
}

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 := parseVars(vars, envs, vm); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ 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"`
DisableLabel bool `yaml:"disable_label"`
Label string
Color string `yaml:"label_color"`
DisableLabel bool `yaml:"disable_label"`
DisableComment bool `yaml:"disable_comment"`
}

// WhenPlanError is a configuration to notify the plan result returns an error
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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
Expand Down
1 change: 1 addition & 0 deletions pkg/notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
Templates map[string]string
UseRawOutput bool
Patch bool
SkipNoChanges bool
}

// PullRequest represents GitHub Pull Request metadata
Expand Down
5 changes: 5 additions & 0 deletions pkg/notifier/github/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func (g *NotifyService) Plan(ctx context.Context, param *notifier.ParamExec) (in
}
}

if result.HasNoChanges && 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(ctx, body, &PostOptions{
Number: cfg.PR.Number,
Expand Down

0 comments on commit 638294e

Please sign in to comment.