Thomas Parslow
Thomas Parslow

Reputation: 6012

How to update a fork from it's original via the Github API

I've created a fork of a GitHub repository via the GitHub API. Now, later on, I want to pull any updates from the origin repository into the fork. This should always be a fast-forward in my use case. I have read access to the origin repository and read-write to the fork.

I thought of maybe creating a Pull Request then accepting (both of which you can do via the API) but this creates noise (Pull Requests being created and destroyed) and just doesn't seem right.

Is there any way to do this via the API?

Upvotes: 7

Views: 2716

Answers (4)

aviv
aviv

Reputation: 346

I don't have the inside scoop on this, so this might be a miss-feature that will be removed at some point. Until then:

GitHub makes available all commits across (I assume) the entire fork network; So APIs that accept commit hashes will be happy to work on hashes from the upstream, or across other forks (This is explicitly documented for repos/commits/compare and creating a pull requst).

So there are a couple of ways to update via APIs only:

  1. Using Git data api: This will usually be the best option, if you don't change your fork's master.

    1. Get upstream ref /repos/upstream/repo/git/refs/heads/master, and get the hash from it
    2. Update your fork PATCH /repos/my/repo/git/refs/heads/master with the same hash.
  2. Using a higher-level merge API: This will create a merge commit, which some people like.

    1. Get the upstream ref like before
    2. Create a merge to branch master in your repo.
  3. Pull-request to yourself and merge it via API: This will end up creating not only a merge commit, but a PR as well.

    1. Create PR: POST to /repos/your/repo/pulls with head = "upstream:master"
    2. Get the PR URL from the response,
    3. Merge it: PUT to /repos/your/repo/pulls/number/merge

It's possible that the "upstream:master" notation would also work for options 1 & 2, saving an API call.

Upvotes: 18

virchau13
virchau13

Reputation: 1448

This is now possible in the GitHub API; documentation here, and announcement here.

In summary, make a POST request to /repos/{owner}/{repo}/merge-upstream with the proper authentication and the payload of { "branch": "branch-name" }.

Upvotes: 5

Lorhan Sohaky
Lorhan Sohaky

Reputation: 19

This that work for me, because I needed update from upstream but without a merge request commit. My ref is master.

  1. Create a pull request POST /repos/:myUsername/:myRepo/pulls
    • INPUT: {title, head: 'ownerFromUpStream:master', base: 'master', ...}
  2. Get sha from pull request (ex. response.data.head.sha)
  3. PATCH /repos/:myUsername/:myRepo/git/refs/master
    • PARAMS: {sha: shaFromPullRequest}

DOC.

Upvotes: 0

Ivan Zuzak
Ivan Zuzak

Reputation: 18772

Not possible currently, but I've gone ahead and added that to our API wishlist. :)

Upvotes: 0

Related Questions