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 fails to clone a PR that was created using gitea's Agit workflow #5140

Open
Infinoid opened this issue Dec 4, 2024 · 1 comment
Labels
bug Something isn't working provider/gitea

Comments

@Infinoid
Copy link

Infinoid commented Dec 4, 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

I've got atlantis set up to work with gitea. I am self-hosting both, and it's working pretty nicely. However, atlantis gets confused by PRs that were created using the Agit workflow. This is a weird syntax that gerrit uses a lot; gitea also supports it. Some of our users prefer it because they don't have to think about forks and branches.

For PRs created in this way, the webhook sent to atlantis looks a little different:

     "head": {
-      "label": "pr-test",
-      "ref": "pr-test",
+      "label": "",
+      "ref": "refs/pull/9/head",

Atlantis fails to clone the repo in this case. It looks like it's assuming ref is a branch name, and in this case it isn't.

Atlantis emits this error message:

running git clone --depth=1 --branch refs/pull/9/head --single-branch https://atlantis:<redacted>@gitea.server/tf/repo.git /atlantis-data/repos/tf/repo/9/default: Cloning into '/atlantis-data/repos/tf/repos/9/default'...
warning: Could not find remote branch refs/pull/9/head to clone.
fatal: Remote branch refs/pull/9/head not found in upstream origin
: exit status 128

I think it was following this code path.

Because it's a ref and not a branch, I think it needs to do something like this, instead:

git clone --depth=1 --single-branch https://gitea.server/tf/repo.git
cd repo
git fetch origin refs/pull/9/head:pr-branch
git checkout pr-branch

It looks like there's already code in there that can do the git fetch thing, but I don't know how atlantis chooses one method or another. It probably needed to choose differently in this case.

Reproduction Steps

  1. Have atlantis set up to monitor a gitea repo
  2. clone the gitea repo
  3. make a change, commit your change to your local main branch
  4. push your change using agit syntax (git push origin HEAD:refs/for/main -o topic="agit-test" -o title="Test agit workflow")
  5. open the PR link in gitea, atlantis will have commented with an error message similar to the above

Logs

Logs
{"level":"info","ts":"2024-12-04T16:57:16.228Z","caller":"events/working_dir.go:235","msg":"creating dir '/atlantis-data/repos/tf/repo/9/default'","json":{"repo":"tf/repo","pull":"9"}}
{"level":"error","ts":"2024-12-04T16:57:16.362Z","caller":"events/instrumented_project_command_builder.go:75","msg":"Error building auto plan commands: running git clone --depth=1 --branch refs/pull/9/head --single-branch https://atlantis:<redacted>@gitea.server/tf/repo.git /atlantis-data/repos/tf/repo/9/default: Cloning into '/atlantis-data/repos/tf/repo/9/default'...\nwarning: Could not find remote branch refs/pull/9/head to clone.\nfatal: Remote branch refs/pull/9/head not found in upstream origin\n: exit status 128","json":{},"stacktrace":"github.com/runatlantis/atlantis/server/events.(*InstrumentedProjectCommandBuilder).buildAndEmitStats\n\tgithub.com/runatlantis/atlantis/server/events/instrumented_project_command_builder.go:75\ngithub.com/runatlantis/atlantis/server/events.(*InstrumentedProjectCommandBuilder).BuildAutoplanCommands\n\tgithub.com/runatlantis/atlantis/server/events/instrumented_project_command_builder.go:26\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).runAutoplan\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:86\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).Run\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:305\ngithub.com/runatlantis/atlantis/server/events.(*DefaultCommandRunner).RunAutoplanCommand\n\tgithub.com/runatlantis/atlantis/server/events/command_runner.go:231"}
{"level":"error","ts":"2024-12-04T16:57:16.383Z","caller":"events/pull_updater.go:18","msg":"running git clone --depth=1 --branch refs/pull/9/head --single-branch https://atlantis:<redacted>@gitea.server/tf/repo.git /atlantis-data/repos/tf/repo/9/default: Cloning into '/atlantis-data/repos/tf/repo/9/default'...\nwarning: Could not find remote branch refs/pull/9/head to clone.\nfatal: Remote branch refs/pull/9/head not found in upstream origin\n: exit status 128","json":{"repo":"tf/chat","pull":"9"},"stacktrace":"github.com/runatlantis/atlantis/server/events.(*PullUpdater).updatePull\n\tgithub.com/runatlantis/atlantis/server/events/pull_updater.go:18\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).runAutoplan\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:91\ngithub.com/runatlantis/atlantis/server/events.(*PlanCommandRunner).Run\n\tgithub.com/runatlantis/atlantis/server/events/plan_command_runner.go:305\ngithub.com/runatlantis/atlantis/server/events.(*DefaultCommandRunner).RunAutoplanCommand\n\tgithub.com/runatlantis/atlantis/server/events/command_runner.go:231"}

Environment details

  • Atlantis version: ghcr.io/runatlantis/atlantis:v0.30.0 and v0.31.0 on amd64
  • Deployment method: helm via opentofu
  • If not running the latest Atlantis version have you tried to reproduce this issue on the latest version:
  • Atlantis flags:

Atlantis server-side config file:

# config file

Repo atlantis.yaml file is not present.

This instance was configured to use opentofu, based on https://www.runatlantis.io/blog/2024/integrating-atlantis-with-opentofu.

Additional Context

Here's a gist to show you what the webhook looks like in this case.

I had originally thought that this was a problem with gitea, and logged an issue here. They pointed me to the correct syntax for refs.

@Infinoid Infinoid added the bug Something isn't working label Dec 4, 2024
Copy link

dosubot bot commented Dec 4, 2024

Hey @Infinoid! 👋 I'm Dosu, a bot here to help you with bugs, questions, and becoming a contributor. If you need assistance or have any questions, feel free to ask!

I found a similar open issue regarding Atlantis failing to clone pull requests from Azure DevOps, which might be related to the problem you're experiencing with Gitea's Agit workflow. However, no solution was found in the comments of that issue [1].

Regarding your specific issue, it seems that Atlantis is incorrectly assuming that the ref is a branch name when it is actually a reference in the Agit workflow. This results in the failure to clone the repository. The suggested workaround involves using git fetch to handle the non-standard ref syntax, which is not currently being chosen by Atlantis.

Unfortunately, I couldn't find specific information on how Atlantis determines whether to use git clone or git fetch for a pull request. It might require a change in the logic to correctly handle the Agit workflow's ref syntax. You might consider exploring the code path mentioned in your message to see if adjustments can be made to accommodate this workflow.

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 provider/gitea
Projects
None yet
Development

No branches or pull requests

1 participant