action icon indicating copy to clipboard operation
action copied to clipboard

autofix-ci/action failed due to `fatal: couldn't find remote ref refs/pull/*/head`

Open suzuki-shunsuke opened this issue 8 months ago • 4 comments

autofix-ci/action failed due to fatal: couldn't find remote ref refs/pull/*/head in some pull requests created by Renovate.

https://github.com/lintnet/lintnet.github.io/actions/runs/14117851840/job/39552170367?pr=673

- uses: autofix-ci/action@2891949f3779a1cafafae1523058501de3d4e944 # v1.3.1
/usr/bin/git fetch --depth=1 origin +refs/pull/673/head
fatal: couldn't find remote ref refs/pull/673/head
Error: The process '/usr/bin/git' failed with exit code 128
  • https://github.com/aquaproj/aquaproj.github.io/actions/runs/14118562480/job/39554300329?pr=1457
  • https://github.com/aquaproj/aqua-renovate-config/actions/runs/14117861284/job/39552198255?pr=858

I'm looking into the cause. Before running autofix-ci/action, actions/checkout was run.

https://github.com/lintnet/lintnet.github.io/blob/134d2749e35f92bdb7159e782dad6cf24af2d688/.github/workflows/autofix.yaml#L11-L13

So I think refs/pull/*/head should have been fetched, but actually git fetch failed due to fatal: couldn't find remote ref refs/pull/*/head.

suzuki-shunsuke avatar Mar 28 '25 00:03 suzuki-shunsuke

  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +615181329e1c2e5a23a39a647290cdae5f85fe38:refs/remotes/pull/673/merge

Ah, I see. We should checkout not head but merged commit.

https://github.com/actions/checkout?tab=readme-ov-file#checkout-pull-request-head-commit-instead-of-merge-commit https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#github-context

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.head_ref }}

suzuki-shunsuke avatar Mar 28 '25 00:03 suzuki-shunsuke

Hmm. ref: ${{ github.head_ref }} doesn't resolve the issue.

https://github.com/lintnet/lintnet.github.io/actions/runs/14119460261/job/39556908446?pr=675

Fetching the repository
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +refs/heads/renovate/suzuki-shunsuke-ghalint-1.x*:refs/remotes/origin/renovate/suzuki-shunsuke-ghalint-1.x* +refs/tags/renovate/suzuki-shunsuke-ghalint-1.x*:refs/tags/renovate/suzuki-shunsuke-ghalint-1.x*
  From https://github.com/lintnet/lintnet.github.io
   * [new branch]      renovate/suzuki-shunsuke-ghalint-1.x -> origin/renovate/suzuki-shunsuke-ghalint-1.x
/usr/bin/git fetch --depth=1 origin +refs/pull/675/head
fatal: couldn't find remote ref refs/pull/675/head

https://github.com/autofix-ci/action/blob/551dded8c6cc8a1054039c8bc0b8b48c51dfc6ef/index.ts#L58

suzuki-shunsuke avatar Mar 28 '25 00:03 suzuki-shunsuke

https://docs.github.com/en/rest/guides/using-the-rest-api-to-interact-with-your-git-database?apiVersion=2022-11-28#checking-mergeability-of-pull-requests

A consuming API needs to explicitly request a pull request to create a test merge commit. A test merge commit is created when you view the pull request in the UI and the "Merge" button is displayed, or when you get, create, or edit a pull request using the REST API. Without this request, the merge Git refs will fall out of date until the next time someone views the pull request.

If you are currently using polling methods that produce outdated merge Git refs, then GitHub recommends using the following steps to get the latest changes from the default branch:

Receive the pull request webhook. Call GET /repos/{owner}/{repo}/pulls/{pull_number} to start a background job for creating the merge commit candidate. Poll your repository using GET /repos/{owner}/{repo}/pulls/{pull_number} to see if the mergeable attribute is true or false. You can use Git directly or GET /repos/{owner}/{repo}/git/refs/{ref} for updates to merge Git refs only after performing the previous steps.


Edit. Ah, sorry. merge commit is unrelated in this time.

suzuki-shunsuke avatar Mar 28 '25 01:03 suzuki-shunsuke

I guess we should use GITHUB_HEAD_REF, ${github.head_ref}, or ${github.event.pull_request.head.ref} rather than +refs/pull/${event.pull_request.number}/head.

In my understanding +refs/pull/${event.pull_request.number}/head isn't documented well, and maybe this has the same issue with merge commit. https://github.com/autofix-ci/action/issues/23#issuecomment-2759926615

I'm not sure why this issue happened in many repositories today although it didn't happen before.

suzuki-shunsuke avatar Mar 28 '25 04:03 suzuki-shunsuke

https://github.com/autofix-ci/action/pull/24

mhils avatar Jun 08 '25 11:06 mhils