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

Atlantis API BaseBranch, HeadBranch, HeadCommit are the same value request.Ref makes plan/apply fail through API calls #5093

Open
jiaflu opened this issue Nov 16, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jiaflu
Copy link

jiaflu commented Nov 16, 2024

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

Hi, Atlantis team, I've been testing Atlantis API endpoints following the doc https://www.runatlantis.io/docs/api-endpoints in recent days,
and I found a strange thing is that when Atlantis was trying to construct the PullRequest model, BaseBranch, HeadBranch, HeadCommit in the PullRequest model were the same value request.Ref, and the source code is here https://github.com/runatlantis/atlantis/blob/v0.29.0/server/controllers/api_controller.go#L248-L250
code capture:

return &request, &command.Context{
		HeadRepo: baseRepo,
		Pull: models.PullRequest{
			Num:        request.PR,
			BaseBranch: request.Ref,
			HeadBranch: request.Ref,
			HeadCommit: request.Ref,
			BaseRepo:   baseRepo,
		},
		Scope: a.Scope,
		Log:   a.Logger,
		API:   true,
	}, http.StatusOK, nil

this logic leads to an issue for me, after running a plan through API on a target project, the dir created by the plan will be overridden by the apply API call due to the reason {"level":"debug","ts":"2024-11-15T19:01:16.817+0800","caller":"events/working_dir.go:136","msg":"repo was already cloned but is not at correct commit, wanted 'atlantis-api-test' got '6f3b9277ec240d6b6f9521a76caf5374316a1eaf'","json":{}}, source code is here https://github.com/runatlantis/atlantis/blob/main/server/events/working_dir.go#L128-L137 , then Atlantis will do a force clone, this action will clean up the tf status files created by the previous plan, so the apply call always fails due to the error Error running apply operation: no plan found at path \"infra/glue/wap/wap-udp-dev\" and workspace \"default\"–did you run plan?

And I see if the plan is triggered by events(gitops way), it can correctly get the PR details through the below logic
https://github.com/runatlantis/atlantis/blob/v0.29.0/server/events/event_parser.go#L609-L618

Anyone can help me understand the above logic? thanks!

Reproduction Steps

plan API request:

curl --location 'http://127.0.0.1:4141/api/plan' \
--header 'X-Atlantis-Token: token' \
--header 'Content-Type: application/json' \
--data '{
    "Repository": "Platform/dev-infra",
    "Ref": "atlantis-api-test",
    "Type": "Github",
    "Paths": [{
      "Directory": "infra/glue/wap/wap-udp-dev",
      "Workspace": "default"
    }],
    "PR": 5588
}'

apply API request:

curl --location 'http://127.0.0.1:4141/api/apply' \
--header 'X-Atlantis-Token: token' \
--header 'Content-Type: application/json' \
--data '{
    "Repository": "Platform/dev-infra",
    "Ref": "atlantis-api-test",
    "Type": "Github",
    "Paths": [{
      "Directory": "infra/glue/wap/wap-udp-dev",
      "Workspace": "default"
    }],
    "PR": 5588
}'

Logs

Environment details

  • Atlantis version: v0.29.0
  • Deployment method: helm
  • If not running the latest Atlantis version have you tried to reproduce this issue on the latest version:
  • Atlantis flags: server --checkout-strategy=merge

Atlantis server-side config file:

workflows:
  standard:
    plan:
      steps:
        - run: rm -rf .terraform
        - init:
        - plan:
        - run: terraform$ATLANTIS_TERRAFORM_VERSION show -no-color -json $PLANFILE > $PLANFILE.json || true
    apply:
      steps:
        - apply:
    import:
      steps:
        - import:
    state_rm:
      steps:
        - state_rm:

Repo atlantis.yaml file:

version: 2
automerge: true
projects:
- name: glue/wap/wap-udp-dev
  dir: infra/glue/wap/wap-udp-dev
  workflow: standard
  terraform_version: v1.5.4

Additional Context

@jiaflu jiaflu added the bug Something isn't working label Nov 16, 2024
Copy link

dosubot bot commented Nov 16, 2024

Hello @jiaflu! I'm here to assist you with any bugs, questions, or contributions you have. Let's work together to resolve any issues you're facing.

The issue you're encountering is due to the BaseBranch, HeadBranch, and HeadCommit in the PullRequest model being set to the same value, request.Ref, when using the Atlantis API. This setup causes problems because the directory created by the plan operation is overridden by the apply operation, leading to the error "no plan found at path" during the apply call.

The root of the problem lies in the logic used in the api_controller.go file, where these fields are incorrectly set to the same value. This results in the repository not being at the correct commit, as indicated by the debug message you provided.

In contrast, when the plan is triggered by events, the correct PR details are obtained using different logic in the event_parser.go file. This discrepancy in handling PR details between API calls and event-triggered operations is causing the issue.

To resolve this, the logic in the api_controller.go file needs to be adjusted to correctly set the BaseBranch, HeadBranch, and HeadCommit values based on the actual PR details, similar to how it's done in the event_parser.go file. This would ensure that the plan and apply operations are executed on the correct commit and branch, preventing the directory from being overridden incorrectly.

To continue talking to Dosu, mention @dosu.

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

No branches or pull requests

1 participant