Skip to content

Commit

Permalink
feat: add embedded_var_names
Browse files Browse the repository at this point in the history
BREAKING CHANGE: only variables defined in embedded_var_names are embedded into comments
  • Loading branch information
suzuki-shunsuke committed Jul 17, 2021
1 parent 32a0c63 commit 20a3d32
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
17 changes: 9 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

// Config is for tfcmt config structure
type Config struct {
CI CI `yaml:"-"`
Terraform Terraform
Vars map[string]string `yaml:"-"`
Templates map[string]string
Log Log
GHEBaseURL string `yaml:"ghe_base_url"`
GitHubToken string `yaml:"-"`
Complement Complement `yaml:"ci"`
CI CI `yaml:"-"`
Terraform Terraform
Vars map[string]string `yaml:"-"`
EmbeddedVarNames []string `yaml:"embedded_var_names"`
Templates map[string]string
Log Log
GHEBaseURL string `yaml:"ghe_base_url"`
GitHubToken string `yaml:"-"`
Complement Complement `yaml:"ci"`
}

type CI struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (ctrl *Controller) getNotifier(ctx context.Context) (notifier.Notifier, err
ParseErrorTemplate: ctrl.ParseErrorTemplate,
ResultLabels: labels,
Vars: ctrl.Config.Vars,
EmbeddedVarNames: ctrl.Config.EmbeddedVarNames,
Templates: ctrl.Config.Templates,
})
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ type Config struct {
Template *terraform.Template
ParseErrorTemplate *terraform.Template
// ResultLabels is a set of labels to apply depending on the plan result
ResultLabels ResultLabels
Vars map[string]string
Templates map[string]string
UseRawOutput bool
ResultLabels ResultLabels
Vars map[string]string
EmbeddedVarNames []string
Templates map[string]string
UseRawOutput bool
}

// PullRequest represents GitHub Pull Request metadata
Expand Down
48 changes: 30 additions & 18 deletions pkg/notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,38 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
}
}

postOpt := PostOptions{
Number: cfg.PR.Number,
Revision: cfg.PR.Revision,
}
logE := logrus.WithFields(logrus.Fields{
"program": "tfcmt",
})

embeddedComment, err := getEmbeddedComment(&cfg, param.CIName, isPlan)
if err != nil {
return result.ExitCode, err
}
logE.WithFields(logrus.Fields{
"comment": embeddedComment,
}).Debug("embedded HTML comment")
// embed HTML tag to hide old comments
body += embeddedComment

if err := g.client.Comment.Post(ctx, body, PostOptions{
Number: cfg.PR.Number,
Revision: cfg.PR.Revision,
}); err != nil {
return result.ExitCode, err
}
return result.ExitCode, nil
}

func getEmbeddedComment(cfg *Config, ciName string, isPlan bool) (string, error) {
vars := make(map[string]interface{}, len(cfg.EmbeddedVarNames))
for _, name := range cfg.EmbeddedVarNames {
vars[name] = cfg.Vars[name]
}

data := map[string]interface{}{
"Program": "tfcmt",
"Vars": cfg.Vars,
"Vars": vars,
"SHA1": cfg.PR.Revision,
"PRNumber": cfg.PR.Number,
}
Expand All @@ -102,23 +123,14 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
} else {
data["Command"] = "apply"
}
if err := metadata.SetCIEnv(param.CIName, os.Getenv, data); err != nil {
return result.ExitCode, err
if err := metadata.SetCIEnv(ciName, os.Getenv, data); err != nil {
return "", err
}
embeddedComment, err := metadata.Convert(data)
if err != nil {
return result.ExitCode, err
}
logE.WithFields(logrus.Fields{
"comment": embeddedComment,
}).Debug("embedded HTML comment")
// embed HTML tag to hide old comments
body += embeddedComment

if err := g.client.Comment.Post(ctx, body, postOpt); err != nil {
return result.ExitCode, err
return "", err
}
return result.ExitCode, nil
return embeddedComment, nil
}

func (g *NotifyService) updateLabels(ctx context.Context, result terraform.ParseResult) []string { //nolint:cyclop
Expand Down

0 comments on commit 20a3d32

Please sign in to comment.