Skip to content

Commit

Permalink
Merge pull request #38 from wernken-ng/main
Browse files Browse the repository at this point in the history
Add support for AllowEmptyRun
  • Loading branch information
davidwin93 authored Sep 6, 2023
2 parents af18e5c + 02df0f2 commit d6e8772
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
8 changes: 2 additions & 6 deletions pkg/comment_actions/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
ErrOtherTFTool = errors.New("use 'tfc' to interact with tfbuddy")
ErrNoNotePassed = errors.New("no notes passed in note block")
ErrInvalidAction = errors.New("invalid tfc action")
ErrPermanent = fmt.Errorf("could not parse comment as command. %w", utils.ErrPermanent)
)

type CommentOpts struct {
Expand All @@ -37,18 +38,13 @@ func ParseCommentCommand(noteBody string) (*CommentOpts, error) {
return nil, ErrNoNotePassed
}

if len(words)%2 != 0 {
log.Debug().Str("comment", comment[0:10]).Msg("not a tfc command")
return nil, ErrNotTFCCommand
}

opts := &CommentOpts{
TriggerOpts: &tfc_trigger.TFCTriggerOptions{},
}
_, err := flags.ParseArgs(opts, words)
if err != nil {
log.Error().Err(err).Msg("error parsing comment as command")
return nil, fmt.Errorf("could not parse comment as command. %w", utils.ErrPermanent)
return nil, ErrPermanent
}

if opts.Args.Agent == "terraform" || opts.Args.Agent == "atlantis" {
Expand Down
23 changes: 22 additions & 1 deletion pkg/comment_actions/parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,30 @@ func TestParseCommentCommand(t *testing.T) {
}, nil, "simple plan with version"},
{"tfc plan -w -v 1.1.8",
nil,
ErrNotTFCCommand,
ErrPermanent,
"not a valid command",
},
{"tfc apply -e", &CommentOpts{
TriggerOpts: &tfc_trigger.TFCTriggerOptions{
Action: tfc_trigger.ApplyAction,
AllowEmptyRun: true,
},
Args: CommentArgs{
Agent: "tfc",
Command: "apply",
},
}, nil, "short flag for allow empty run"},
{"tfc apply --allow_empty_run", &CommentOpts{
TriggerOpts: &tfc_trigger.TFCTriggerOptions{
Action: tfc_trigger.ApplyAction,
AllowEmptyRun: true,
},
Args: CommentArgs{
Agent: "tfc",
Command: "apply",
},
}, nil, "long flag for allow empty run"},
{"tfc apply -k", nil, ErrPermanent, "invalid command"},
}

for _, tc := range tcs {
Expand Down
9 changes: 9 additions & 0 deletions pkg/tfc_api/api_driven_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ApiRunOptions struct {
TFVersion string
// Terraform Target
Target string
// Terraform AllowEmptyApply
AllowEmptyRun bool
}

// CreateRunFromSource creates a new Terraform Cloud run from source files
Expand All @@ -49,12 +51,18 @@ func (c *TFCClient) CreateRunFromSource(ctx context.Context, opts *ApiRunOptions
// TODO: Clean this up maybe check for valid Versions from TFCloud
var tfVersion *string = nil
var tfPlanOnly *bool = nil
var tfAllowEmptyApply *bool = tfe.Bool(false)
tfTarget := []string{}

if opts.TFVersion != "" && !opts.IsApply {
log.Debug().Str("version", opts.TFVersion).Msg("setting tf version")
tfVersion = tfe.String(opts.TFVersion)
tfPlanOnly = tfe.Bool(true)
}

if opts.AllowEmptyRun {
tfAllowEmptyApply = tfe.Bool(true)
}

if opts.Target != "" {
tfTarget = append(tfTarget, strings.Split(opts.Target, ",")...)
Expand All @@ -69,6 +77,7 @@ func (c *TFCClient) CreateRunFromSource(ctx context.Context, opts *ApiRunOptions
TargetAddrs: tfTarget,
PlanOnly: tfPlanOnly,
TerraformVersion: tfVersion,
AllowEmptyApply: tfAllowEmptyApply,
})
if err != nil {
log.Error().Err(err).Msg("could create run")
Expand Down
16 changes: 9 additions & 7 deletions pkg/tfc_trigger/tfc_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type TFCTriggerOptions struct {
Workspace string `short:"w" long:"workspace" description:"A specific terraform Workspace to use" required:"false"`
TFVersion string `short:"v" long:"tf_version" description:"A specific terraform version to use" required:"false"`
Target string `short:"t" long:"target" description:"A specific terraform target to use" required:"false"`
AllowEmptyRun bool `short:"e" long:"allow_empty_run" description:"A specific terraform AllowEmptyRun" required:"false"`
}

func NewTFCTriggerConfig(opts *TFCTriggerOptions) (*TFCTriggerOptions, error) {
Expand Down Expand Up @@ -598,13 +599,14 @@ func (t *TFCTrigger) triggerRunForWorkspace(ctx context.Context, cfgWS *TFCWorks

// create new TFC run
run, err := t.tfc.CreateRunFromSource(ctx, &tfc_api.ApiRunOptions{
IsApply: isApply,
Path: pkgDir,
Message: fmt.Sprintf("MR [!%d]: %s", t.GetMergeRequestIID(), mr.GetTitle()),
Organization: org,
Workspace: wsName,
TFVersion: t.cfg.TFVersion,
Target: t.cfg.Target,
IsApply: isApply,
Path: pkgDir,
Message: fmt.Sprintf("MR [!%d]: %s", t.GetMergeRequestIID(), mr.GetTitle()),
Organization: org,
Workspace: wsName,
TFVersion: t.cfg.TFVersion,
Target: t.cfg.Target,
AllowEmptyRun: t.cfg.AllowEmptyRun,
})
if err != nil {
return fmt.Errorf("could not create TFC run. %w", err)
Expand Down

0 comments on commit d6e8772

Please sign in to comment.