cli: gh repo sync not working
Describe the bug
$ gh repo sync alizter/dune
HTTP 404: Not Found (https://api.github.com/repos/alizter/dune/git/refs/heads/main)
when the repo doesn’t need syncing, the command succeeds. Clicking this URL also correctly fetches the json.
NixOS
gh version 2.29.0 (1980-01-01)
https://github.com/cli/cli/releases/tag/v2.29.0
Steps to reproduce the behavior
- Have a repo that needs syncing.
- Do
gh repo sync user/repo - Should 404
Expected vs actual behavior
It used to work and sync the repo.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 16 (6 by maintainers)
Hello everyone, thank you for reporting this bug and providing additional information to help out.
I believe this is a platform change, but I also believe you can resolve it through the CLI. If you aren’t interested in the details, jump to the Resolution section below.
Investigation
Firstly, the
404 Not Foundonhttps://api.github.com/repos/<org>/<repo>/git/refs/heads/<branch>is a bit of a red herring. This404is a failure toPATCH, while attempting to update the branch to point at the commit ref provided in the body. I believe this is failing because the commit does not exist on the targeted repository. The reason it doesn’t exist on the provided repository is due to an earlier failure on the/merge-upstreamendpoint (that is swallowed for historical reasons I don’t yet understand). For example, from @andrewpollock’s logs:Edit: My assumption around the
404was incorrect, see the following post for details about why it failed and why the422and404have the same cause.Why is this endpoint failing (intermittently)?
In digging into this, I found a recent change that began requiring
workflowscope on the token if any commits to be merged from upstream included workflow changes.I was able to somewhat prove this locally by forking
ocaml/dune, checking out @Alizter’s commit, and seeinggh repo sync williammartin-test-org/dunefail because the next commit included a workflow change. I then reset to and pushedHEAD^1frommainandsynced successfully, seeing that the next commit didn’t have a workflow change.I believe this theory is further evidenced by:
sync repobutton on the UI succeeds (which would already have the required privileges)sync repoon the UI, that the CLI works again (because themerge-upstreamno longer fails and the commit exists afterPATCHoccurs)workflowscope to my token allowed me to sync even with workflow changes upstream.Resolution
To resolve this you can add the
workflowscope to your token either on initial login like so:or via refreshing you existing token like so:
Caveat
It’s possible that this change introduced other failure cases that I don’t yet have the necessary knowledge to understand but I believe that this is almost certainly the common case. If after refreshing your token you still experience errors, please leave a comment.
CLI Changes
From our point of view, we should determine how to handle this error in a more graceful manner, prompting to refresh to include the token. Unfortunately, there are a number of reasons that the API returns
422and the reasons are opaque to clients, so we may need some changes before we can support this.In the meantime, hopefully this issue serves as a place for people to find help.
@spencerschrock Sounds like a platform bug that
permissions: write-alldoes not allow syncing. I can ask around internally about it.Update, I heard back from the internal API team and they explained that this behavior is a security feature and is not a bug. So unfortunately the only work around is to generate a new token, perhaps with this, that has the correct scopes.
Further Investigation
Following up on the previous comment I did some further digging into the relationship between the earlier
422and the later404. As it turns out, both occur for the same reason as far as I can tell (missingworkflowscope).I created a commit that changed a workflow that was not associated with a branch and tried to
PATCHupdate thehead/mainref and received a404when using a token withoutworkflowscope:It was later successful with
workflowscope:What’s happening here is that as described in the oauth scopes doc, the
workflowscope is needed for any workflow file change that isn’t in a branch on the repository already. When we attempt to update theref, GitHub is rejecting this as “you don’t have permission to point this branch at something that contains a workflow file change” and that’s manifesting as a404.I think that ties up the loose ends!
After I’ve synced by the web UI:
In case this helps:
Consider reopening - I have been encountering this off-and-on for at least a week.
Today it appears to work. I would guess then somebody in the GitHub API was tweaking some things causing my queries to fail. Nothing actionable from you guys I suppose.