How to use the GitHub Commits API to list the commits (shas) of a deleted branch? - rest

I'm trying to get a list of all the commit shas between 2 dates that belonged to a branch of a particular repo that was deleted. https://docs.github.com/en/rest/reference/repos#list-commits does not talk about deleted branches. A different API that I can use that wouldn't restore the branch in the process would also be useful to me

I found out that you can also supply the sha instead of the deleted branch name as the "sha" query parameter to the Commits API.

Related

Github merging API returns 404 Not Found

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.

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.

Branch name when doing a pull request

I've done a few pull requests on GH already, but I committed to the master branch. Now I read on various places that it's a good idea to create a branch.
Are there any guidelines for branch naming? I usually work with Mercurial and give my branches the same name as their relevant bug ticket ID, but that doesn't work for this.
I've looked at a few repositories: some commit to master, some commit to fix-somebug, some commit to patch-1. I understand that this doesn't create conflicts, because pull requests are merged to master (or a different, long living branch) and the branch is then deleted, is that correct?
The idea behind a branch for a pull request is to allow for said branch to be automatically deleted once the pull request is accepted.
And since April 2013, that branch will be deleted for you:
You are then suppose to update/rebase your master from the master of the upstream repo in order to get what you developed in isolation a branch from the official repo that you have forked (since that repo has accepted your pull request)
The name of the branch should represent the development effort you are engaged in.
It is always a good practise to make commit on the git branches rather than master. You can use any name for your git branch(it doesn't allow spaces in branch names, also some special characters).

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