Get the user who created a branch with GitHub API - github

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.

Related

List hidden commits still in Github but not Git (security leak)

AWS informed me of leaked account info in a Github repo. It gave me link to the corresponding commit. That commit seems to only be visible if you have the direct sha. I am trying to identify all the other commits that could also contain leaks.
AWS gave the link: https://github.com/myorg/somerepo/commit/1abcdef and that gives me a very old commit with a banner
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If I try git show 1abcdef on that repo, it's not found. It really cannot be found in git itself. In fact, there is nothing that old.
If I access the commit through the Github API, it is there:
gh api 'repos/myorg/somerepo/commits/1abcdef
I can see all information about it, including the date in 2017.
So I tried:
gh api 'repos/myorg/somerepo/commits?until=2017-12-31'
But that finds nothing. The Github API doesn't seem to mention anything to look at orphaned commits.
I have obviously rotated all credentials for the compromised (dev) account but how can I find other potential problematic commits? If I cannot find them, how could a hacker do it?

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

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.

How can I find branches in all the repositories that my github account has access to, given a branch name?

In github, my account can access multiple related repositories. A user story can be implemented as same-name branches in multiple repositories.
Given a branch's name, can I find out which repositories contain a branch with the given name?
Can I further jump to such a branch in a given repository?
Probably the simplest way of searching repositories for branch names is to use the command git branch within each repo. You can specify git branch -a for local and remote branches, or git branch -r for just remote.
This isn't ideal for a number of reasons, one being that you'd need to have command-line access to all the repositories you want to search (such as all the repos being cloned locally), another being that you'll have to manually scan through the branch list that git branch outputs.
One possible solution to this is to use the GitHub API to list all repos and / or all branches. The answer here gives a decent example in Python of how to authenticate (to allow access to private repos), which you would need to follow with the API's GET request:
GET /repos/:owner/:repo/branches

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