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

Branch matcher does not work in atlantis v0.17.0 #1539

Closed
ericsonrumuy7 opened this issue Apr 28, 2021 · 6 comments · Fixed by #1768
Closed

Branch matcher does not work in atlantis v0.17.0 #1539

ericsonrumuy7 opened this issue Apr 28, 2021 · 6 comments · Fixed by #1768
Labels
bug Something isn't working

Comments

@ericsonrumuy7
Copy link

I saw that in v0.17.0 atlantis has added branch matcher feature #1383, but when I tried it, its seem does not work.

  1. update repo.yaml
repos:
  - id: /.*/
    branch: /(main|master)/
  1. restart atlantis
  2. create PR from branch test1 to branch test2

but atlantis still can be execute from that PR

@dghubble
Copy link
Contributor

I think even if you specify a single repo object, atlantis actually has your repo (which is limited to master/main) and another global one that will match regardless. So yeah, it doesn't seem to be functional. I'm unfortunately no longer using this.

https://github.com/runatlantis/atlantis/blob/master/server/events/yaml/valid/global_cfg.go#L173

@ericsonrumuy7
Copy link
Author

I think even if you specify a single repo object, atlantis actually has your repo (which is limited to master/main) and another global one that will match regardless. So yeah, it doesn't seem to be functional. I'm unfortunately no longer using this.

https://github.com/runatlantis/atlantis/blob/master/server/events/yaml/valid/global_cfg.go#L173

is there any workaround for this ? i'm currently using bitbucket server (stash) and need this feature so atlantis cannot be abused using other branch

@minamijoyo
Copy link
Contributor

Hi, @ericsonrumuy7 and all.
I also hit this problem and wrote a custom shell script in pre_workflow_hooks as a workaround. It checks a base branch of pull request and generates an invalid atlantis.yaml to suppress plan if the base branch doesn't match intended.

The following example restricts the base branch to default branch:

# Tested in atlantis v0.17.0

repos:
- id: /.*/
  apply_requirements: [approved, mergeable]
  pre_workflow_hooks:
    - run: pre_workflow_hooks.sh
#!/bin/bash

set -euo pipefail

# pre_workflow_hooks.sh

# Get the default branch
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | sed 's/.*: //')
echo "BASE_BRANCH_NAME: $BASE_BRANCH_NAME"
echo "DEFAULT_BRANCH: $DEFAULT_BRANCH"

if [ "$BASE_BRANCH_NAME" != "$DEFAULT_BRANCH" ] ; then
  echo "generate an invalid atlantis.yaml to restrict executions of plan / apply on the pull request that are merged into the non-default branch for security reasons."
  cat << EOF > atlantis.yaml
version: invalid
EOF
  # Note that exit with non-zero actually doesn't stop the following plan phase. It only writes stdout to log with ERROR level.
  # Generating the invalid atlantis.yaml will cause an error in plan.
  exit 1
fi

It works for me, but it's not the best solution by all means.

@jasonrberk
Copy link

jasonrberk commented Aug 17, 2021

@minamijoyo - so does the plan still run and put a comment on the PR?

I've got three atlantis instances running in three different AWS accounts, all tied to a single terragrunt repo. So the repo has three hooks, one for each atlantis instance behind an ALB.

I was toying with the idea of generating an atlantis config that was "valid", but sets the branch or id to a non-existent branch / repo name. The idea being my prod atlantis instance shouldn't comment on a PR in the non-prod folder in my terragrunt repo.

@minamijoyo
Copy link
Contributor

@jasonrberk (Aside from the original issue that the branch keyword in repo.yaml doesn't work,) yes, with pre_workflow_hooks, I think it's possible to generate a valid atlantis.yml based on the branch.

minamijoyo added a commit to minamijoyo/atlantis that referenced this issue Aug 23, 2021
Fixes runatlantis#1539

The branch matcher feature has been introduced in runatlantis#1383, but the current
implementation was broken and doesn't work at all (runatlantis#1539).

If my understanding is correct, there are two problems:

(1) The `GlobalCfg` has a default `Repo` instance which always matches
any repositries and branches. Therefore the branch matcher never be
functional.
(2) Validating base branches in
`DefaultPreWorkflowHooksCommandRunner.RunPreHooks()` implicitly assumed
that users customize `pre_workflow_hooks`, but the assumption isn't
always true because it defaults to empty.

For (1), I added a new method `MatchingRepo()` to `GlobalCfg` to check
`BranchMatches()` for a single `Repo` instance.

For (2), I moved validating branch to
`DefaultCommandRunner.validateCtxAndComment()`. Since the method has
already validated meta data of pull request, I think it's suitable place
to check base branches, but please let me know if there is anywhere more
suitable.
minamijoyo added a commit to minamijoyo/atlantis that referenced this issue Aug 23, 2021
Fixes runatlantis#1539

The branch matcher feature has been introduced in runatlantis#1383, but the current
implementation was broken and doesn't work at all (runatlantis#1539).

If my understanding is correct, there are two problems:

(1) The `GlobalCfg` has a default `Repo` instance which always matches
any repositries and branches. Therefore the branch matcher never be
functional.
(2) Validating base branches in
`DefaultPreWorkflowHooksCommandRunner.RunPreHooks()` implicitly assumed
that users customize `pre_workflow_hooks`, but the assumption isn't
always true because it defaults to empty.

For (1), I added a new method `MatchingRepo()` to `GlobalCfg` to check
`BranchMatches()` for a single `Repo` instance.

For (2), I moved validating branch to
`DefaultCommandRunner.validateCtxAndComment()`. Since the method has
already validated meta data of pull request, I think it's suitable place
to check base branches, but please let me know if there is anywhere more
suitable.
nishkrishnan pushed a commit that referenced this issue Aug 30, 2021
Fixes #1539

The branch matcher feature has been introduced in #1383, but the current
implementation was broken and doesn't work at all (#1539).

If my understanding is correct, there are two problems:

(1) The `GlobalCfg` has a default `Repo` instance which always matches
any repositries and branches. Therefore the branch matcher never be
functional.
(2) Validating base branches in
`DefaultPreWorkflowHooksCommandRunner.RunPreHooks()` implicitly assumed
that users customize `pre_workflow_hooks`, but the assumption isn't
always true because it defaults to empty.

For (1), I added a new method `MatchingRepo()` to `GlobalCfg` to check
`BranchMatches()` for a single `Repo` instance.

For (2), I moved validating branch to
`DefaultCommandRunner.validateCtxAndComment()`. Since the method has
already validated meta data of pull request, I think it's suitable place
to check base branches, but please let me know if there is anywhere more
suitable.
msarvar referenced this issue in lyft/atlantis Sep 27, 2021
Fixes #1539

The branch matcher feature has been introduced in #1383, but the current
implementation was broken and doesn't work at all (#1539).

If my understanding is correct, there are two problems:

(1) The `GlobalCfg` has a default `Repo` instance which always matches
any repositries and branches. Therefore the branch matcher never be
functional.
(2) Validating base branches in
`DefaultPreWorkflowHooksCommandRunner.RunPreHooks()` implicitly assumed
that users customize `pre_workflow_hooks`, but the assumption isn't
always true because it defaults to empty.

For (1), I added a new method `MatchingRepo()` to `GlobalCfg` to check
`BranchMatches()` for a single `Repo` instance.

For (2), I moved validating branch to
`DefaultCommandRunner.validateCtxAndComment()`. Since the method has
already validated meta data of pull request, I think it's suitable place
to check base branches, but please let me know if there is anywhere more
suitable.
krrrr38 pushed a commit to krrrr38/atlantis that referenced this issue Dec 16, 2022
Fixes runatlantis#1539

The branch matcher feature has been introduced in runatlantis#1383, but the current
implementation was broken and doesn't work at all (runatlantis#1539).

If my understanding is correct, there are two problems:

(1) The `GlobalCfg` has a default `Repo` instance which always matches
any repositries and branches. Therefore the branch matcher never be
functional.
(2) Validating base branches in
`DefaultPreWorkflowHooksCommandRunner.RunPreHooks()` implicitly assumed
that users customize `pre_workflow_hooks`, but the assumption isn't
always true because it defaults to empty.

For (1), I added a new method `MatchingRepo()` to `GlobalCfg` to check
`BranchMatches()` for a single `Repo` instance.

For (2), I moved validating branch to
`DefaultCommandRunner.validateCtxAndComment()`. Since the method has
already validated meta data of pull request, I think it's suitable place
to check base branches, but please let me know if there is anywhere more
suitable.
@MartinsPaulo
Copy link

Has this been resolved? I am facing the same issue in 2024.

  • id: /.*/
    branch: /^main$/
    workflow: main_workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants