Github merging API returns 404 Not Found - github

I'm attempting to use the github api to merge a commit into a feature branch for an enterprise repository and am getting 404 Not Found which api docs say is either because the base branch or head provided in the POST request do not exist.
github merge api docs
Things I've checked:
My api token has full repo access
The base branch has been pushed to origin
I can successfully merge the commit locally by running git merge {hash}
I'm able to successfully use the api for some other actions such as creating pull requests or comparing commits
Other things to note that may or may not be relevant. It's hard to tell since the 404 Not Found message is not that helpful.
This base branch is set up to do fast-forwards of master branch commits so there will effectively never be merge commits to this branch, only fast-forwards.
I was successfully able to create a pull request via the api on the same base branch as my attempts at merging, which indicates the base branch isn't the problem.
Instead of using a commit hash for the head parameter I used 'master' since the docs say you can alternatively use a branch name instead of a hash. But that didn't work-- same 404
There are other methods of doing fast forwards with the github api such as updating refs but I haven't gotten these to work either.

Related

Can we create pull request from master branch to master branch in Azure devops

Can we create pull request from master branch to master branch in Azure devops because our team will work on directly in master branch and we need to code review and approval method also.
No this isn't how git works. You would have to branch off of master and then PR these branches back into master. Committing directly to master is not a recommended practice
Contributions to a source code repository that uses a distributed version control system are commonly made by means of a pull request, also known as a merge request.
The contributor requests that the project maintainer pulls the source code change, hence the name "pull request". The maintainer has to merge the pull request if the contribution should become part of the source base.
A pull request can be accepted or rejected by maintainers. Once the pull request is reviewed and approved, it is merged into the repository.
The above is how to pull request works.
We can push empty commits via git commit --allow-empty -m 1, But for your situation, even no empty commits(Because the source branch and target branch always the same), so of course the pull request will not be able to created.
Another thing will clearly tell you the reason:
If you use this API to create Pull Request from 'master' to 'master', you will find it is not accepted.
The detection of whether the 'sourcebranch' and 'targetbranch' are the same is high priority, even before the detection of the existence of the branch, so what you want to achieve is not possible from the basic of the design.

Pull request not appearing in upstream repository

A colleague of mine forked my GitHub repository, and he adds his modifications. However, when he makes a pull request, it is not reported to my repository: the list of open pull requests is still empty.
In order to apply his modifications on my work, I must go to his repository and apply the pull request myself
Unless I misunderstood the pull request concept, do you a have a solution on this? Should I recreate my repository?
I must inform you that repository was a private one before I made it public.
Your colleague should create a pull request that targets your repository. The easiest way to do this is to navigate to your repository and create the pull request from there:
Confirm that the base fork is the repository you'd like to merge changes into. Use the base branch drop-down menu to select the branch of the upstream repository you'd like to merge changes into.
If the pull request is created properly, targeting your repository, you should see it in your list of pull requests.

Get the user who created a branch with GitHub API

Background
I am using GitHub Enterprise.
I want to check unused branches in my repository, and ask owners of these unused branches to clean-up.
You can see "Your branches" (branches created by current user) on “Branches” page in GitHub. So I think GitHub might have information for who created a branch.
On the other hand, the result of GitHub REST API (https://developer.github.com/v3/git/refs/#get-a-reference) does not contain the creator of the specified branch.
Question
Is there’s any way to get the user who created a branch with GitHub API?
There is no real "ownership" associated to a branch with Git/GitHub.
As mentioned in the documentation, "Your branches" reference in a repository the branches you have push access to, not necessarily the ones you have "created".
The best you can do is, if you have access to a local clone, a simple git fetch, followed by:
git for-each-ref --format="%(committerdate) %09 %(refname:short) %09 %(authorname)" --sort=-committerdate refs/remotes/origin
That will list the remote branches from the most recent updated one to the oldest, with the author of the last commit on each branch.
But if you have to use GitHub API, then you would need to:
list the remote branches,
then for each one get the commit mentioned with the branch
You can then contact the committer of that most recent commit.

Using GitHub API to determine if the last commit to master was a result of a merged pull request?

We use GitHub flow as branching strategy which means that anything in master is the result of a merged pull request. It's trivial to get the last commit by doing the following.
GET https://api.github.com/repos/:org/:repo/git/refs/heads/master
However it seems non-trivial to work out the pull request that caused this commit.
Is it possible to find out the pull request that created this commit via the API?
The PR should be one of the parents—the second one—of the commit in master, since that commit is the result of a merge of the PR.
You can find that parent with the GitHub commit API
GET /repos/:owner/:repo/git/commits/:sha
GitHub doesn't offer a way to get the branch from a commit, but you could list all the PR and cross-reference the SHA1 found above with said PRs.

Get git commits by branch name\id with git api

I am working on automated service to work with Git hub repositories. And I am having problem on my side - I can't find a way to get all commit in particular branch by its hash\name.
My code is an automated tool to make code reviewes. So I've added a feature to ignore particular branch in my review process (ex. a testing branch or something like that). So in my service I am marking branch as ignored. Once I get commits from git hub api - there is no information there about which branch is current commit belongs to.
I started thinking that my overall github idea is wrong - since commit-branch link is pretty obvious thing so there should be something that made the API developers to ignore that in the GetCommits method
So my question is - Is there a way to find out which branch commit (using v3 api json result) belongs to in github api (v3 - GET /repos/:owner/:repo/commits/:sha).
Thanks
There is currently no way in GitHub's API to ask if a commit is in the history of a specific branch. The only thing you can do is fetch all the commits for a specific branch, and then iterate through the commits to see if a specific commit is in the list. To do that, make a request to /repos/:owner/:repo/commits?sha=branchname, where branchname is the name of the branch you want to fetch commits for e.g. https://api.github.com/repos/izuzak/pmrpc/commits?sha=master.
If you just want to check if your commit is for example on branch test123, do:
https://api.github.com/repos/golang/go/compare/test123...001a75a74c4a27901b0b536efe1be581612c52a9
and check the status.
If it's 'identical' or 'behind', then the commit is part of the branch. If it is different, it's not.
In Python with PyGitHub Library (https://pygithub.readthedocs.io/en/latest/index.html)
You can do
g = Github(accesskey) # PyGitHub object
repo = g.get_repo(repository) # Repository
commits = repo.get_commits(sha='stable-2.9') # Commits by a branch