Azure / acr-builder

Azure Container Registry Build Runner
MIT License
38 stars 35 forks source link

acb download fails to fetch PR from ADO repos #672

Closed yuehaoliang closed 8 months ago

yuehaoliang commented 8 months ago

Describe the bug

If the context is GitHub (https://github.com/user/myapp-repo.git#pull/<PR_ID>/head), acb download works fine to fetch and check out PR #. However, if the context is AzureDevOps (https://dev.azure.com/user/myproject/_git/myapp-repo#pull/<PR_ID>/head), acb download checks out the wrong codebase.

Root Cause

Briefly explaining how acb download works: it initially establishes a connection to the remote repository

git init
git remote add https://dev.azure.com/user/myproject/_git/myapp-repo

Then, it tries

git fetch --depth 1 origin pull/<PR_ID>/head

If the preceding command fails, it subsequently executes

git fetch origin

Finally, it checks out the fetched latest commit

git checkout FETCH_HEAD

If the remote repository is an AzureDevOps repository, executing git fetch origin pull/<PR_ID>/head will result in the error message fatal: couldn't find remote ref pull/<PR_ID>/head. Therefore, acb download mistakenly checks out the most recent commit in the repository instead of the desired pull request.

Fix

For GitHub, the pull request reference is pull/<PR_ID>/head, but for AzureDevOps, the pull request reference is actually pull/<PR_ID>/merge. (I learned about it from ChatGPT, and later I found this StackOverflow post discussing it; however, there is no official documentation available.) The pull request reference differs from providers. FYI, for GitLab, it should be merge-requests/<PR_ID>/head.