Using the GitHub API (v3) I'd like to figure out which branches a commit appears on. I didn't find a way to directly query this, either through repo commits or the commit data objects. An alternate solution would be to list all the branches, and compare with their HEAD; I guess the comparison would fail if the commit is not on the given branch.
Is this supported via the current API, and I just missed it? If not, do you have a (better) workaround?
That's not possible directly via the GitHub API.
Workaround 1:
get a list of all branches
for each branch, get a list of commits on that branch
check if the commit is in the list of commits for each branch
Workaround 2 (I think this will work, but not 100% sure if I missed a case):
get a list of all branches
for each branch, compare the branch with the SHA:
https://api.github.com/repos/:user/:repo/compare/:branch...:sha_of_commit
If the value of the status attribute in the response is diverged or ahead, then the commit is not in the branch. If the value of the status attribute is behind or identical, then the commit is in the branch.
I haven't checked if this is directly supported by the GitHub API, but this is trivial to do using plain Git:
git branch --all --contains <commit>
That will list all branches (local and remote) in a local repository that contain the given commit.
There is no direct endpoint to list the branches by a commit ID. But you can use a combination of other endpoints to solve this.
Condition 1
Use branches-where-head endpoint to get the name of the branches where the given commit ID is the head.
Condition 2
If condition 1 returns empty, get pull info object using the commit ID and extract the branch information using pulls info endpoint.
Tip: The return object contains properties - base.ref and head.ref which has the branch name. If base.ref is the name of the master branch, use head.ref which should give you the name of the branches that contains the commit ID.
In case anyone using the PyGitHub (https://pygithub.readthedocs.io/en/latest/introduction.html) library for API calls -
First get the list of all branches and then
g = Github(<accesskey>)
repo = g.get_repo(<repository>)
is_commit_in_branch = repo.compare('stable-2.11', <commit_id>).status
# If it returns behind or identical, then the commit is in the branch.
GitLab API
Following Ivan Zuzak's solution number 2, to know if a commit is on a branch:
Use GitLab's repository compare API, and compare from the branch, to the commit
GET /projects/:id/repository/compare?from=<branch>&to=<sha_of_commit>
If the commits list is empty, then yes, the commit is on that branch.
In Python, using python-gitlab:
def is_commit_on_branch(project, commit, branch):
c = project.repository_compare(branch, commit)
return not c['commits']
Related
I am trying to query the Github RestAPI v. X-GitHub-Api-Version to find line changes (addition, deletions), user, date, repository_name for any repositories in an organization that have files of a particular file extension. Not seeing a clear path forward with this, do I need to file all files using {{baseUrl}}/search/code?q=org:{{org}}+extension:tf, then take those outputs iterating over and querying commits for a file and then extract the details from the commit details?
I am trying to pull commit changes via api and all i get is the path to the file itself, as in the whole file.
What I want to achieve is to see the changes (diff only) for a single file per commit.
E.g:
If i query the same using Github i get the diff like so:
"## -1 +0,0 ##\n- console.log(\"Blasting!\")"
Is there a similar solution in Azure-devops?
Thanks!
What I want to achieve is to see the changes (diff only) for a single file per commit.
There is no existing Rest API to meet your needs. But you could refer to the following steps to get the content of the git diff.
Step1: You could use the Rest API to get the commit id.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=5.0
Step2: You could use the Rest API to get the commit by commit id.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.0
In the Rest API Result, you need to record the value of parentsid.
Step3: You could use the Rest API to Get the file path.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=6.0
Step4: You could use the following API to get the diff content.
Post https://dev.azure.com/Organization/Project /_api/_versioncontrol/fileDiff?__v=5&diffParameters={value}&repositoryId={repositoryid}
The {value} is Json type.
Here is an example:
{"originalPath":"filepath","originalVersion":"Parentsid","modifiedPath":"filepath","modifiedVersion":"commitid","partialDiff":true,"includeCharDiffs":true}
You could add the value to the API URL.
Then run the API and the result will contains the git diff content. (2 means remove, 1 means add)
Here is a result sample:
Here is the ticket, you could refer to it.
I was reading the documentation of GitHub API and I'm not sure what do do with the Merge Pull Request method.
https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
Specifically with the parameter SHA. I don't understand what exactly I should provide to API.
The INPUT section says I must provide
commit_title Title for the automatic commit message.
commit_message Extra detail to append to automatic commit message.
sha SHA that pull request head must match to allow merge.
merge_method Merge method to
use.
Where do I exactly get the sha value that I need to pass to API?
Thanks!
Consider the following diagram, which shows a feature branch derived from some base branch:
base: ... A -- B -- C
\
feature: D -- E
Let's suppose that we created a pull request from feature going back to base. GitHub would execute this pull request by merging feature into base. The pull request HEAD, at the time we created the pull request, would be commit E in feature. But, the HEAD of the feature branch could change before the pull request is completed.
The API call you mention includes the SHA-1 hash of the pull request HEAD, as a requirement for the pull request to complete. This would avoid the possibility of feature being merged back to base while containing additional commits beyond commit E.
Regarding how you would find the SHA-1 hash for E, the pull request HEAD, you may simply try using git log, e.g.
# from feature
git log
Then, check the output for what should be the latest entry from commit E, and find the hash.
This picture shows few releases from a project. To see more, one has to press "Next" button.
Is it possible to get data (commit hash tag, date) about all releases preferably in a excel, csv, json file format or at least in a single web page.
The list releases API endpoint will provide data about each release in a repository in JSON format:
GET /repos/:owner/:repo/releases
The target_commitish field returned in the response is the nearest thing to a commit hash that is returned by this API, but you can then use the the Git Data API to return the commit SHA:
GET /repos/:owner/:repo/git/tags/:sha
Where :sha can be the string returned in target_commitish. The result of this request will give you the matching commit SHA in the sha field.
Can I use the Github API to check if a certain repository contains a certain commit?
At first glance, it seems that the get a single commit API call should work, returning 404 if there is no such commit in the repository. But that is not true: It seems that this call will run successful on commits that are present in forked repositories (possibly due to a pull request). (This effect can also be observed in the regular web interface; this particular commit has not been pulled into that repository yet.)
Api GitHub search
For searching other repositories one can use the api, which finds commits via various criteria. (This method returns up to 100 results per page.):
https://developer.github.com/v3/search/#search-commits
Only the default branch is considered, mostly master
Api usage
GET /search/commits
q can be any kind of search term combination: https://help.github.com/articles/searching-commits/
Example parameters for q
hash:124a9a0ee1d8f1e15e833aff432fbb3b02632105
Matches commits with the hash 124a9a0ee1d8f1e15e833aff432fbb3b02632105.
parent:124a9a0ee1d8f1e15e833aff432fbb3b02632105
Matches children of 124a9a0ee1d8f1e15e833aff432fbb3b02632105.
further parameters, like sorting, ordering can be found in the documentation above.
Usage example per hash:
example call https://api.github.com/search/commits?q=<searchterm>+<searchterm2>
specific call: https://api.github.com/search/commits?q=repo:adejoux/kitchen-wpar+hash:0a3a228e5b250daf06f933b35b3f0eafc715be4f
You need to add an special header, because the api is available for developers to preview
header to add: application/vnd.github.cloak-preview