Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring #246

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cli/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func cmdApply(ctx *cli.Context) error {
setLogLevel(logLevel)
}

if err := parseOpts(ctx, &cfg); err != nil {
if err := parseOpts(ctx, cfg); err != nil {
return err
}

Expand All @@ -33,7 +33,7 @@ func cmdApply(ctx *cli.Context) error {

args := ctx.Args()

return t.Run(ctx.Context, controller.Command{
return t.Run(ctx.Context, &controller.Command{
Cmd: args.First(),
Args: args.Tail(),
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/urfave/cli/v2"
)

func newConfig(ctx *cli.Context) (config.Config, error) {
cfg := config.Config{}
func newConfig(ctx *cli.Context) (*config.Config, error) {
cfg := &config.Config{}
confPath, err := cfg.Find(ctx.String("config"))
if err != nil {
return cfg, err
return nil, err
}
if confPath != "" {
if err := cfg.LoadFile(confPath); err != nil {
return cfg, err
return nil, err
}
}
return cfg, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func cmdPlan(ctx *cli.Context) error {
setLogLevel(logLevel)
}

if err := parseOpts(ctx, &cfg); err != nil {
if err := parseOpts(ctx, cfg); err != nil {
return err
}

Expand All @@ -31,7 +31,7 @@ func cmdPlan(ctx *cli.Context) error {
}
args := ctx.Args()

return t.Run(ctx.Context, controller.Command{
return t.Run(ctx.Context, &controller.Command{
Cmd: args.First(),
Args: args.Tail(),
})
Expand Down
32 changes: 16 additions & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (

// Config is for tfcmt config structure
type Config struct {
CI CI `yaml:"-"`
Terraform Terraform
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"`
PlanPatch bool `yaml:"plan_patch"`
Log *Log
GHEBaseURL string `yaml:"ghe_base_url"`
GitHubToken string `yaml:"-"`
Complement *Complement `yaml:"ci"`
PlanPatch bool `yaml:"plan_patch"`
}

type CI struct {
Expand All @@ -40,20 +40,20 @@ type Log struct {

// Terraform represents terraform configurations
type Terraform struct {
Plan Plan
Apply Apply
Plan *Plan
Apply *Apply
UseRawOutput bool `yaml:"use_raw_output"`
}

// Plan is a terraform plan config
type Plan struct {
Template string
WhenAddOrUpdateOnly WhenAddOrUpdateOnly `yaml:"when_add_or_update_only"`
WhenDestroy WhenDestroy `yaml:"when_destroy"`
WhenNoChanges WhenNoChanges `yaml:"when_no_changes"`
WhenPlanError WhenPlanError `yaml:"when_plan_error"`
WhenParseError WhenParseError `yaml:"when_parse_error"`
DisableLabel bool `yaml:"disable_label"`
WhenAddOrUpdateOnly *WhenAddOrUpdateOnly `yaml:"when_add_or_update_only"`
WhenDestroy *WhenDestroy `yaml:"when_destroy"`
WhenNoChanges *WhenNoChanges `yaml:"when_no_changes"`
WhenPlanError *WhenPlanError `yaml:"when_plan_error"`
WhenParseError *WhenParseError `yaml:"when_parse_error"`
DisableLabel bool `yaml:"disable_label"`
}

// WhenAddOrUpdateOnly is a configuration to notify the plan result contains new or updated in place resources
Expand Down Expand Up @@ -88,7 +88,7 @@ type WhenParseError struct {
// Apply is a terraform apply config
type Apply struct {
Template string
WhenParseError WhenParseError `yaml:"when_parse_error"`
WhenParseError *WhenParseError `yaml:"when_parse_error"`
}

// LoadFile binds the config file to Config structure
Expand Down
45 changes: 19 additions & 26 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ func TestLoadFile(t *testing.T) {
t.Parallel()
testCases := []struct {
file string
cfg Config
cfg *Config
ok bool
}{
{
file: "../../example.tfcmt.yaml",
cfg: Config{
Terraform: Terraform{
Plan: Plan{
Template: "## Plan Result\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .CombinedOutput }}\n</pre></code></details>\n",
WhenDestroy: WhenDestroy{},
},
Apply: Apply{
Template: "",
cfg: &Config{
Terraform: &Terraform{
Plan: &Plan{
Template: "## Plan Result\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .CombinedOutput }}\n</pre></code></details>\n",
},
UseRawOutput: false,
},
Complement: Complement{
Complement: &Complement{
PR: []domain.ComplementEntry{},
Link: []domain.ComplementEntry{},
SHA: []domain.ComplementEntry{},
Expand All @@ -50,9 +46,9 @@ func TestLoadFile(t *testing.T) {
},
{
file: "../../example-with-destroy-and-result-labels.tfcmt.yaml",
cfg: Config{
Terraform: Terraform{
Plan: Plan{
cfg: &Config{
Terraform: &Terraform{
Plan: &Plan{
Template: `{{if .HasDestroy}}
## :warning: WARNING: Resource Deletion will happen :warning:

Expand All @@ -69,36 +65,33 @@ This plan contains **resource deletion**. Please check the plan result very care
</pre></code></details>
{{end}}
`,
WhenAddOrUpdateOnly: WhenAddOrUpdateOnly{
WhenAddOrUpdateOnly: &WhenAddOrUpdateOnly{
Label: "add-or-update",
},
WhenDestroy: WhenDestroy{
WhenDestroy: &WhenDestroy{
Label: "destroy",
},
WhenPlanError: WhenPlanError{
WhenPlanError: &WhenPlanError{
Label: "error",
},
WhenNoChanges: WhenNoChanges{
WhenNoChanges: &WhenNoChanges{
Label: "no-changes",
},
},
Apply: Apply{
Template: "",
},
UseRawOutput: false,
},
},
ok: true,
},
{
file: "no-such-config.yaml",
cfg: Config{
Terraform: Terraform{
Plan: Plan{
cfg: &Config{
Terraform: &Terraform{
Plan: &Plan{
Template: "## Plan Result\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .CombinedOutput }}\n</pre></code></details>\n",
WhenDestroy: WhenDestroy{},
WhenDestroy: &WhenDestroy{},
},
Apply: Apply{
Apply: &Apply{
Template: "",
},
},
Expand All @@ -111,7 +104,7 @@ This plan contains **resource deletion**. Please check the plan result very care
testCase := testCase
t.Run(testCase.file, func(t *testing.T) {
t.Parallel()
var cfg Config
cfg := &Config{}

if err := cfg.LoadFile(testCase.file); err == nil {
if !testCase.ok {
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type Controller struct {
Config config.Config
Config *config.Config
Parser terraform.Parser
Template *terraform.Template
ParseErrorTemplate *terraform.Template
Expand All @@ -33,8 +33,8 @@ type Command struct {
}

// Run sends the notification with notifier
func (ctrl *Controller) Run(ctx context.Context, command Command) error {
if err := platform.Complement(&ctrl.Config); err != nil {
func (ctrl *Controller) Run(ctx context.Context, command *Command) error {
if err := platform.Complement(ctrl.Config); err != nil {
return err
}

Expand Down Expand Up @@ -62,7 +62,7 @@ func (ctrl *Controller) Run(ctx context.Context, command Command) error {
cmd.Stderr = io.MultiWriter(os.Stderr, uncolorizedStderr, uncolorizedCombinedOutput)
_ = cmd.Run()

return apperr.NewExitError(ntf.Notify(ctx, notifier.ParamExec{
return apperr.NewExitError(ntf.Notify(ctx, &notifier.ParamExec{
Stdout: stdout.String(),
Stderr: stderr.String(),
CombinedOutput: combinedOutput.String(),
Expand All @@ -86,8 +86,8 @@ func (ctrl *Controller) renderTemplate(tpl string) (string, error) {
return buf.String(), nil
}

func (ctrl *Controller) renderGitHubLabels() (github.ResultLabels, error) { //nolint:cyclop
labels := github.ResultLabels{
func (ctrl *Controller) renderGitHubLabels() (*github.ResultLabels, error) { //nolint:cyclop
labels := &github.ResultLabels{
AddOrUpdateLabelColor: ctrl.Config.Terraform.Plan.WhenAddOrUpdateOnly.Color,
DestroyLabelColor: ctrl.Config.Terraform.Plan.WhenDestroy.Color,
NoChangesLabelColor: ctrl.Config.Terraform.Plan.WhenNoChanges.Color,
Expand Down Expand Up @@ -161,20 +161,20 @@ func (ctrl *Controller) renderGitHubLabels() (github.ResultLabels, error) { //no
}

func (ctrl *Controller) getNotifier(ctx context.Context) (notifier.Notifier, error) {
labels := github.ResultLabels{}
labels := &github.ResultLabels{}
if !ctrl.Config.Terraform.Plan.DisableLabel {
a, err := ctrl.renderGitHubLabels()
if err != nil {
return nil, err
}
labels = a
}
client, err := github.NewClient(ctx, github.Config{
client, err := github.NewClient(ctx, &github.Config{
Token: ctrl.Config.GitHubToken,
BaseURL: ctrl.Config.GHEBaseURL,
Owner: ctrl.Config.CI.Owner,
Repo: ctrl.Config.CI.Repo,
PR: github.PullRequest{
PR: &github.PullRequest{
Revision: ctrl.Config.CI.SHA,
Number: ctrl.Config.CI.PRNumber,
},
Expand Down
22 changes: 13 additions & 9 deletions pkg/notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type Client struct {
*github.Client
Debug bool

Config Config
Config *Config

common service
common *service

Comment *CommentService
Commits *CommitsService
Expand All @@ -42,14 +42,14 @@ type Config struct {
BaseURL string
Owner string
Repo string
PR PullRequest
PR *PullRequest
CI string
Parser terraform.Parser
// Template is used for all Terraform command output
Template *terraform.Template
ParseErrorTemplate *terraform.Template
// ResultLabels is a set of labels to apply depending on the plan result
ResultLabels ResultLabels
ResultLabels *ResultLabels
Vars map[string]string
EmbeddedVarNames []string
Templates map[string]string
Expand All @@ -68,7 +68,7 @@ type service struct {
}

// NewClient returns Client initialized with Config
func NewClient(ctx context.Context, cfg Config) (*Client, error) {
func NewClient(ctx context.Context, cfg *Config) (*Client, error) {
token := cfg.Token
token = strings.TrimPrefix(token, "$")
if token == EnvToken {
Expand Down Expand Up @@ -103,12 +103,13 @@ func NewClient(ctx context.Context, cfg Config) (*Client, error) {
Config: cfg,
Client: client,
v4Client: githubv4.NewClient(tc),
common: &service{},
}
c.common.client = c
c.Comment = (*CommentService)(&c.common)
c.Commits = (*CommitsService)(&c.common)
c.Notify = (*NotifyService)(&c.common)
c.User = (*UserService)(&c.common)
c.Comment = (*CommentService)(c.common)
c.Commits = (*CommitsService)(c.common)
c.Notify = (*NotifyService)(c.common)
c.User = (*UserService)(c.common)

c.API = &GitHub{
Client: client,
Expand Down Expand Up @@ -138,6 +139,9 @@ type ResultLabels struct {

// HasAnyLabelDefined returns true if any of the internal labels are set
func (r *ResultLabels) HasAnyLabelDefined() bool {
if r == nil {
return false
}
return r.AddOrUpdateLabel != "" || r.DestroyLabel != "" || r.NoChangesLabel != "" || r.PlanErrorLabel != ""
}

Expand Down
Loading