runatlantis/atlantis

Multiple plans with a targeted apply with auto merge will incorrectly merge pr

Opened this issue ยท 2 comments

Community Note

  • Please vote on this issue by adding a ๐Ÿ‘ reaction to the original issue to help the community and maintainers prioritize this request. Searching for pre-existing feature requests helps us consolidate datapoints for identical requirements into a single place, thank you!
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Overview of the Issue

Reproduction Steps

If the following is run

atlantis plan

And it affects multiple resources.

aws_s3_bucket.one
aws_s3_bucket.two

If this is run, the targeted apply will merge the pr leaving the other resource to be applied on a subsequent pr

atlantis apply -- -target='aws_s3_bucket.one'

Logs

Environment details

  • Atlantis version: 0.21.0
  • --automerge

https://www.runatlantis.io/docs/server-configuration.html#automerge

Additional Context

mergeResult, _, err := g.client.PullRequests.Merge(

err := c.VCSClient.MergePull(ctx.Pull, pullOptions)

// automergeEnabled returns true if automerging is enabled in this context.
func (c *AutoMerger) automergeEnabled(projectCmds []command.ProjectContext) bool {
// If the global automerge is set, we always automerge.
return c.GlobalAutomerge ||
// Otherwise we check if this repo is configured for automerging.
(len(projectCmds) > 0 && projectCmds[0].AutomergeEnabled)
}

func (a *ApplyStepRunner) hasTargetFlag(ctx command.ProjectContext, extraArgs []string) bool {

if a.autoMerger.automergeEnabled(projectCmds) && !cmd.AutoMergeDisabled {

The hasTargetFlag function result could be passed down to the automergeEnabled. If the flag is true then we can set automerge to false.

Well this is interesting. Looks like targeted applies were previously prevented but perhaps this block is only caught with atlantis apply -target convention and not with the atlantis apply -- -target convention.

Related pr #403

if a.hasTargetFlag(ctx, extraArgs) {


workaround: use --auto-merge-disabled

Either use a naked plan and targeted apply

atlantis plan
atlantis apply --auto-merge-disabled -- -target='aws_s3_bucket.one'

or a targeted plan and a naked apply

atlantis plan -- -target='aws_s3_bucket.one'
atlantis apply --auto-merge-disabled

autoMergeDisabledFlagLong = "auto-merge-disabled"

if autoMergeDisabled {
flags = fmt.Sprintf("%s --%s", flags, autoMergeDisabledFlagLong)
}

Hello,
Workaround works well when having 1 project in the repository or if targeted plan/apply is run in last position, after all other projects has been applied with -p project.
Otherwise, if targeted plan is run first for example ; even with auto-merge-disabled flag, and another project is applied, then the PR is automerged

Is an approach where targeted plan generate failed status check considered? Or maybe an option to allow not considering a targeted plan/apply as a valid commit check?

We'd need to somehow check if -target is in the comment and if so, add an empty file like .terraform-targeted so when you atlantis apply, it checks if the file exists. If the file exists, do not auto merge. If it doesnt exist, automerge unless the automerge flag is passed.