0

I’m trying to (fast-forward) merge a branch using PyGithub in a test-bed repository, however it always returns a 404:

#!/usr/bin/env python

import os

from github import Github
from github.GithubException import GithubException

github = Github(os.environ["GH_TOKEN"])
repo = github.get_repo(os.environ["GITHUB_REPOSITORY"])

# branches = [b.name for b in repo.get_branches()]
branches = list(repo.get_branches())
print(f"BRANCHES: {branches}")

# Merge merge-me into main
# repo.merge(base=branches[0], head=branches[1])
try:
    print("=================== BY NAME")
    repo.merge(base=branches[0].name, head=branches[1].name)
except GithubException as ge:
    print(ge)

try:
    print("=================== BY SHA")
    repo.merge(base=branches[0].name, head=branches[1].commit.sha)
except GithubException as ge:
    print(ge)

I added the SHA attempt based on this answer. However, the script produces the following output:

BRANCHES: [Branch(name="main"), Branch(name="merge-me")]
=================== BY NAME
404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#merge-a-branch"}
=================== BY SHA
404 {"message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#merge-a-branch"}

According to the API docs, a 404 occurs when either the base or head do not exist. But I’m pulling both from the list of (the only) branches in the repository.

I would expect a permission problem to return a 403, but I’m at a loss to explain what is going on.

Edit 1:

I was experiencing this error with a private repository in an organization with SSO. I created a public repository with a similar setup, and it ran without issue.

This token is authorized for SSO to the organization, otherwise it would not be able to access the repository at all. So it shouldn’t be causing this problem, but that is my current best guess as to what is happening.

Edit 2:

The production version of this code was accidentally released to integration and it’s working within a Github Action using the same API Token. So now my best guess is there is something in my own environment getting in my way.

Erik Ogan
  • 21
  • 2
  • I know nothing about pygithub, and not that much about the GitHub API, but when GitHub do a merge, they never do a fast-forward. If you want the effect of a fast-forward, you'll want to pretend to have done a `git push`, which will involve asking GitHub to set the hash ID stored in some branch name; that looks like it's [this request](https://docs.github.com/en/rest/git/refs#update-a-reference). – torek May 04 '22 at 17:49
  • Thanks, @torek. The Github API won’t do a fast-forward when merging a _pull-request_, but I was able to do exactly what I wanted (including the fast-forward) on a public repository in my personal account. So I’m now convinced it’s something to do with SSO authorization. – Erik Ogan May 04 '22 at 18:09
  • Oh, wait, @torek, you’re right. My pull of the results was a fast-forward, but the merge itself is a new commit. That’s probably OK for now, but since I’m having trouble doing this via the API, I may break down and do it on the command-line, since the repo is already checked out. – Erik Ogan May 04 '22 at 18:14

0 Answers0