Skip to content

Commit c4564b7

Browse files
committed
feat(automerge): implement --merge-method flag for apply command
Signed-off-by: a1k0u <[email protected]>
1 parent 95d55b6 commit c4564b7

15 files changed

+278
-107
lines changed

runatlantis.io/docs/automerging.md

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ Automerging can be enabled either by:
2929
If automerge is enabled, you can disable it for a single `atlantis apply`
3030
command with the `--auto-merge-disabled` option.
3131

32+
## How to set merge method for automerge
33+
34+
If automerge is enabled, you can use `--merge-method` option
35+
for `atlantis apply` command to specify which merge method use.
36+
37+
```shell
38+
atlantis apply --merge-method squash
39+
```
40+
41+
Implemented only for GitHub. You can choose one of them:
42+
- merge
43+
- rebase
44+
- squash
45+
3246
## Requirements
3347

3448
### All Plans Must Succeed

runatlantis.io/docs/using-atlantis.md

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ atlantis apply -w staging
149149
* `-p project` Apply the plan for this project. Refers to the name of the project configured in the repo's [`atlantis.yaml` file](repo-level-atlantis-yaml.md). Cannot be used at same time as `-d` or `-w`.
150150
* `-w workspace` Apply the plan for this [Terraform workspace](https://developer.hashicorp.com/terraform/language/state/workspaces). Ignore this if Terraform workspaces are unused.
151151
* `--auto-merge-disabled` Disable [automerge](automerging.md) for this apply command.
152+
* `--merge-method method` Specify which [merge method](automerging.md#how-to-set-merge-method-for-automerge) use for apply command if [automerge](automerging.md) is enabled. Implemented only for GitHub.
152153
* `--verbose` Append Atlantis log to comment.
153154

154155
### Additional Terraform flags

server/core/config/valid/global_cfg.go

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type MergedProjectCfg struct {
104104
Name string
105105
AutoplanEnabled bool
106106
AutoMergeDisabled bool
107+
MergeMethod string
107108
TerraformVersion *version.Version
108109
RepoCfgVersion int
109110
PolicySets PolicySets

server/events/apply_command_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) {
181181
a.updateCommitStatus(ctx, pullStatus)
182182

183183
if a.autoMerger.automergeEnabled(projectCmds) && !cmd.AutoMergeDisabled {
184-
a.autoMerger.automerge(ctx, pullStatus, a.autoMerger.deleteSourceBranchOnMergeEnabled(projectCmds))
184+
a.autoMerger.automerge(ctx, pullStatus, a.autoMerger.deleteSourceBranchOnMergeEnabled(projectCmds), cmd.MergeMethod)
185185
}
186186
}
187187

server/events/automerger.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type AutoMerger struct {
1313
GlobalAutomerge bool
1414
}
1515

16-
func (c *AutoMerger) automerge(ctx *command.Context, pullStatus models.PullStatus, deleteSourceBranchOnMerge bool) {
16+
func (c *AutoMerger) automerge(ctx *command.Context, pullStatus models.PullStatus, deleteSourceBranchOnMerge bool, mergeMethod string) {
1717
// We only automerge if all projects have been successfully applied.
1818
for _, p := range pullStatus.Projects {
1919
if p.Status != models.AppliedPlanStatus {
@@ -32,6 +32,7 @@ func (c *AutoMerger) automerge(ctx *command.Context, pullStatus models.PullStatu
3232
ctx.Log.Info("automerging pull request")
3333
var pullOptions models.PullRequestOptions
3434
pullOptions.DeleteSourceBranchOnMerge = deleteSourceBranchOnMerge
35+
pullOptions.MergeMethod = mergeMethod
3536
err := c.VCSClient.MergePull(ctx.Log, ctx.Pull, pullOptions)
3637

3738
if err != nil {

server/events/comment_parser.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141
policySetFlagShort = ""
4242
autoMergeDisabledFlagLong = "auto-merge-disabled"
4343
autoMergeDisabledFlagShort = ""
44+
mergeMethodFlagLong = "merge-method"
45+
mergeMethodFlagShort = ""
4446
verboseFlagLong = "verbose"
4547
verboseFlagShort = ""
4648
clearPolicyApprovalFlagLong = "clear-policy-approval"
@@ -70,7 +72,7 @@ type CommentBuilder interface {
7072
// BuildPlanComment builds a plan comment for the specified args.
7173
BuildPlanComment(repoRelDir string, workspace string, project string, commentArgs []string) string
7274
// BuildApplyComment builds an apply comment for the specified args.
73-
BuildApplyComment(repoRelDir string, workspace string, project string, autoMergeDisabled bool) string
75+
BuildApplyComment(repoRelDir string, workspace string, project string, autoMergeDisabled bool, mergeMethod string) string
7476
// BuildApprovePoliciesComment builds an approve_policies comment for the specified args.
7577
BuildApprovePoliciesComment(repoRelDir string, workspace string, project string) string
7678
}
@@ -226,7 +228,9 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
226228
var project string
227229
var policySet string
228230
var clearPolicyApproval bool
229-
var verbose, autoMergeDisabled bool
231+
var verbose bool
232+
var autoMergeDisabled bool
233+
var mergeMethod string
230234
var flagSet *pflag.FlagSet
231235
var name command.Name
232236

@@ -248,6 +252,7 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
248252
flagSet.StringVarP(&dir, dirFlagLong, dirFlagShort, "", "Apply the plan for this directory, relative to root of repo, ex. 'child/dir'.")
249253
flagSet.StringVarP(&project, projectFlagLong, projectFlagShort, "", "Apply the plan for this project. Refers to the name of the project configured in a repo config file. Cannot be used at same time as workspace or dir flags.")
250254
flagSet.BoolVarP(&autoMergeDisabled, autoMergeDisabledFlagLong, autoMergeDisabledFlagShort, false, "Disable automerge after apply.")
255+
flagSet.StringVarP(&mergeMethod, mergeMethodFlagLong, mergeMethodFlagShort, "", "Specifies merge method for the VCS if automerge is enabled.")
251256
flagSet.BoolVarP(&verbose, verboseFlagLong, verboseFlagShort, false, "Append Atlantis log to comment.")
252257
case command.ApprovePolicies.String():
253258
name = command.ApprovePolicies
@@ -317,8 +322,20 @@ func (e *CommentParser) Parse(rawComment string, vcsHost models.VCSHostType) Com
317322
return CommentParseResult{CommentResponse: e.errMarkdown(err, cmd, flagSet)}
318323
}
319324

325+
if mergeMethod != "" {
326+
if autoMergeDisabled {
327+
err := fmt.Sprintf("cannot use --%s at same time with --%s", mergeMethodFlagLong, autoMergeDisabledFlagLong)
328+
return CommentParseResult{CommentResponse: e.errMarkdown(err, cmd, flagSet)}
329+
}
330+
331+
if vcsHost != models.Github {
332+
err := fmt.Sprintf("--%s not implemeted for %s", mergeMethodFlagLong, vcsHost.String())
333+
return CommentParseResult{CommentResponse: e.errMarkdown(err, cmd, flagSet)}
334+
}
335+
}
336+
320337
return CommentParseResult{
321-
Command: NewCommentCommand(dir, extraArgs, name, subName, verbose, autoMergeDisabled, workspace, project, policySet, clearPolicyApproval),
338+
Command: NewCommentCommand(dir, extraArgs, name, subName, verbose, autoMergeDisabled, mergeMethod, workspace, project, policySet, clearPolicyApproval),
322339
}
323340
}
324341

@@ -387,7 +404,7 @@ func (e *CommentParser) parseArgs(name command.Name, args []string, flagSet *pfl
387404

388405
// BuildPlanComment builds a plan comment for the specified args.
389406
func (e *CommentParser) BuildPlanComment(repoRelDir string, workspace string, project string, commentArgs []string) string {
390-
flags := e.buildFlags(repoRelDir, workspace, project, false)
407+
flags := e.buildFlags(repoRelDir, workspace, project, false, "")
391408
commentFlags := ""
392409
if len(commentArgs) > 0 {
393410
var flagsWithoutQuotes []string
@@ -402,18 +419,18 @@ func (e *CommentParser) BuildPlanComment(repoRelDir string, workspace string, pr
402419
}
403420

404421
// BuildApplyComment builds an apply comment for the specified args.
405-
func (e *CommentParser) BuildApplyComment(repoRelDir string, workspace string, project string, autoMergeDisabled bool) string {
406-
flags := e.buildFlags(repoRelDir, workspace, project, autoMergeDisabled)
422+
func (e *CommentParser) BuildApplyComment(repoRelDir string, workspace string, project string, autoMergeDisabled bool, mergeMethod string) string {
423+
flags := e.buildFlags(repoRelDir, workspace, project, autoMergeDisabled, mergeMethod)
407424
return fmt.Sprintf("%s %s%s", e.ExecutableName, command.Apply.String(), flags)
408425
}
409426

410427
// BuildApprovePoliciesComment builds an apply comment for the specified args.
411428
func (e *CommentParser) BuildApprovePoliciesComment(repoRelDir string, workspace string, project string) string {
412-
flags := e.buildFlags(repoRelDir, workspace, project, false)
429+
flags := e.buildFlags(repoRelDir, workspace, project, false, "")
413430
return fmt.Sprintf("%s %s%s", e.ExecutableName, command.ApprovePolicies.String(), flags)
414431
}
415432

416-
func (e *CommentParser) buildFlags(repoRelDir string, workspace string, project string, autoMergeDisabled bool) string {
433+
func (e *CommentParser) buildFlags(repoRelDir string, workspace string, project string, autoMergeDisabled bool, mergeMethod string) string {
417434
// Add quotes if dir has spaces.
418435
if strings.Contains(repoRelDir, " ") {
419436
repoRelDir = fmt.Sprintf("%q", repoRelDir)
@@ -441,6 +458,9 @@ func (e *CommentParser) buildFlags(repoRelDir string, workspace string, project
441458
if autoMergeDisabled {
442459
flags = fmt.Sprintf("%s --%s", flags, autoMergeDisabledFlagLong)
443460
}
461+
if mergeMethod != "" {
462+
flags = fmt.Sprintf("%s --%s %s", flags, mergeMethodFlagLong, mergeMethod)
463+
}
444464
return flags
445465
}
446466

server/events/comment_parser_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ func TestBuildPlanApplyVersionComment(t *testing.T) {
729729
workspace string
730730
project string
731731
autoMergeDisabled bool
732+
mergeMethod string
732733
commentArgs []string
733734
expPlanFlags string
734735
expApplyFlags string
@@ -824,6 +825,16 @@ func TestBuildPlanApplyVersionComment(t *testing.T) {
824825
expApplyFlags: "-d dir -w workspace --auto-merge-disabled",
825826
expVersionFlags: "-d dir -w workspace",
826827
},
828+
{
829+
repoRelDir: "dir",
830+
workspace: "workspace",
831+
project: "",
832+
mergeMethod: "squash",
833+
commentArgs: []string{`"arg1"`, `"arg2"`, `arg3`},
834+
expPlanFlags: "-d dir -w workspace -- arg1 arg2 arg3",
835+
expApplyFlags: "-d dir -w workspace --merge-method squash",
836+
expVersionFlags: "-d dir -w workspace",
837+
},
827838
}
828839

829840
for _, c := range cases {
@@ -834,7 +845,7 @@ func TestBuildPlanApplyVersionComment(t *testing.T) {
834845
actComment := commentParser.BuildPlanComment(c.repoRelDir, c.workspace, c.project, c.commentArgs)
835846
Equals(t, fmt.Sprintf("atlantis plan %s", c.expPlanFlags), actComment)
836847
case command.Apply:
837-
actComment := commentParser.BuildApplyComment(c.repoRelDir, c.workspace, c.project, c.autoMergeDisabled)
848+
actComment := commentParser.BuildApplyComment(c.repoRelDir, c.workspace, c.project, c.autoMergeDisabled, c.mergeMethod)
838849
Equals(t, fmt.Sprintf("atlantis apply %s", c.expApplyFlags), actComment)
839850
}
840851
}
@@ -1023,6 +1034,7 @@ var ApplyUsage = `Usage of apply:
10231034
--auto-merge-disabled Disable automerge after apply.
10241035
-d, --dir string Apply the plan for this directory, relative to root of
10251036
repo, ex. 'child/dir'.
1037+
--merge-method string Specifies merge method for the VCS if automerge is enabled.
10261038
-p, --project string Apply the plan for this project. Refers to the name of
10271039
the project configured in a repo config file. Cannot
10281040
be used at same time as workspace or dir flags.

server/events/event_parser.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ type CommentCommand struct {
128128
SubName string
129129
// AutoMergeDisabled is true if the command should not automerge after apply.
130130
AutoMergeDisabled bool
131+
// MergeMethod specified the merge method for the VCS if automerge enabled.
132+
MergeMethod string
131133
// Verbose is true if the command should output verbosely.
132134
Verbose bool
133135
// Workspace is the name of the Terraform workspace to run the command in.
@@ -177,11 +179,11 @@ func (c CommentCommand) IsAutoplan() bool {
177179

178180
// String returns a string representation of the command.
179181
func (c CommentCommand) String() string {
180-
return fmt.Sprintf("command=%q verbose=%t dir=%q workspace=%q project=%q policyset=%q, clear-policy-approval=%t, flags=%q", c.Name.String(), c.Verbose, c.RepoRelDir, c.Workspace, c.ProjectName, c.PolicySet, c.ClearPolicyApproval, strings.Join(c.Flags, ","))
182+
return fmt.Sprintf("command=%q, verbose=%t, dir=%q, workspace=%q, project=%q, policyset=%q, auto-merge-disabled=%t, merge-method=%s, clear-policy-approval=%t, flags=%q", c.Name.String(), c.Verbose, c.RepoRelDir, c.Workspace, c.ProjectName, c.PolicySet, c.AutoMergeDisabled, c.MergeMethod, c.ClearPolicyApproval, strings.Join(c.Flags, ","))
181183
}
182184

183185
// NewCommentCommand constructs a CommentCommand, setting all missing fields to defaults.
184-
func NewCommentCommand(repoRelDir string, flags []string, name command.Name, subName string, verbose, autoMergeDisabled bool, workspace string, project string, policySet string, clearPolicyApproval bool) *CommentCommand {
186+
func NewCommentCommand(repoRelDir string, flags []string, name command.Name, subName string, verbose, autoMergeDisabled bool, mergeMethod string, workspace string, project string, policySet string, clearPolicyApproval bool) *CommentCommand {
185187
// If repoRelDir was empty we want to keep it that way to indicate that it
186188
// wasn't specified in the comment.
187189
if repoRelDir != "" {
@@ -198,6 +200,7 @@ func NewCommentCommand(repoRelDir string, flags []string, name command.Name, sub
198200
Verbose: verbose,
199201
Workspace: workspace,
200202
AutoMergeDisabled: autoMergeDisabled,
203+
MergeMethod: mergeMethod,
201204
ProjectName: project,
202205
PolicySet: policySet,
203206
ClearPolicyApproval: clearPolicyApproval,

server/events/event_parser_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -750,14 +750,14 @@ func TestNewCommand_CleansDir(t *testing.T) {
750750

751751
for _, c := range cases {
752752
t.Run(c.RepoRelDir, func(t *testing.T) {
753-
cmd := events.NewCommentCommand(c.RepoRelDir, nil, command.Plan, "", false, false, "workspace", "", "", false)
753+
cmd := events.NewCommentCommand(c.RepoRelDir, nil, command.Plan, "", false, false, "", "workspace", "", "", false)
754754
Equals(t, c.ExpDir, cmd.RepoRelDir)
755755
})
756756
}
757757
}
758758

759759
func TestNewCommand_EmptyDirWorkspaceProject(t *testing.T) {
760-
cmd := events.NewCommentCommand("", nil, command.Plan, "", false, false, "", "", "", false)
760+
cmd := events.NewCommentCommand("", nil, command.Plan, "", false, false, "", "", "", "", false)
761761
Equals(t, events.CommentCommand{
762762
RepoRelDir: "",
763763
Flags: nil,
@@ -769,7 +769,7 @@ func TestNewCommand_EmptyDirWorkspaceProject(t *testing.T) {
769769
}
770770

771771
func TestNewCommand_AllFieldsSet(t *testing.T) {
772-
cmd := events.NewCommentCommand("dir", []string{"a", "b"}, command.Plan, "", true, false, "workspace", "project", "policyset", false)
772+
cmd := events.NewCommentCommand("dir", []string{"a", "b"}, command.Plan, "", true, false, "", "workspace", "project", "policyset", false)
773773
Equals(t, events.CommentCommand{
774774
Workspace: "workspace",
775775
RepoRelDir: "dir",
@@ -816,7 +816,7 @@ func TestCommentCommand_IsAutoplan(t *testing.T) {
816816
}
817817

818818
func TestCommentCommand_String(t *testing.T) {
819-
exp := `command="plan" verbose=true dir="mydir" workspace="myworkspace" project="myproject" policyset="", clear-policy-approval=false, flags="flag1,flag2"`
819+
exp := `command="plan", verbose=true, dir="mydir", workspace="myworkspace", project="myproject", policyset="", auto-merge-disabled=false, merge-method=, clear-policy-approval=false, flags="flag1,flag2"`
820820
Equals(t, exp, (events.CommentCommand{
821821
RepoRelDir: "mydir",
822822
Flags: []string{"flag1", "flag2"},

0 commit comments

Comments
 (0)