How to get a commit SHA from a release or tag on Github API V3 - github

The release nor tag response don't seem to have information (SHA) about the commit they were made from. How can I get it if I only have a tag/release like v1.2.3?

There's no specific endpoint in GitHub API v3 to get the commit SHA from tag/release name.
For your use-case, you can use the List tags endpoint to get all the tags for a particular repo, iterate over the response and get the desired tag details with the commit SHA.
Endpoint: GET /repos/:owner/:repo/tags
Sample response below:
[
{
"name": "v0.1",
"commit": {
"sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc",
"url": "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"
},
"zipball_url": "https://github.com/octocat/Hello-World/zipball/v0.1",
"tarball_url": "https://github.com/octocat/Hello-World/tarball/v0.1"
}
]

Related

GitHub REST and GraphQL API are returning different data

I am scraping some data from GitHub. The RESTful URL to this particular PR shows that it has a merge_commit_sha value: https://api.github.com/repos/ansible/ansible/pulls/15088
However, when I try to get the same PR using GitHub GraphQL API, it shows it does not have any mergedCommit value.
resource(
url: "https://github.com/ansible/ansible/pull/15088"
) {
...on PullRequest {
id
number
title
merged
mergeCommit {
message
}
}
}
For context, the PR of interest is actually merged and should have a merged-commit value. I am looking for an explanation of the difference between these two APIs.
This link posted in the other answer contains the explanation:
As in, Git doesn’t have the originalCommit (which makes sense).
Presumably the original commit SHA is there, but the graphQL API actually checks to see if git has it, whereas the REST API doesn’t?
If you search for the commit SHA the API returns, you can't find it in the repo.
https://github.com/ansible/ansible/commit/d7b54c103050d9fc4965e57b7611a70cb964ab25
Since this is a very old pull request on an active repo, there's a good chance some old commits were cleaned up or other maintenance on the repo. It's hard to tell as that kind of maintenance obviously isn't version controlled.
Another option is the pull request was merged with fast-forward, which does not involve a merge commit. But that wouldn't explain the SHA on the REST API response.
So probably at some point they removed old merge commits to save some space, or something similar. Some objects still point to removed SHAs, but GraphQL API filters on existing objects.
Feel like it is a bug to me because if you query another PR such as 45454 , it can return the mergeCommit:
{
"data": {
"resource": {
"id": "MDExOlB1bGxSZXF1ZXN0MjE0NDYyOTY2",
"number": 45454,
"title": "win_say - fix up syntax and test issues (#45450)",
"merged": true,
"mergeCommit": {
"message": "win_say - fix up syntax and test issues (#45450)\n\n\n(cherry picked from commit c9c141fb6a51d6b77274958a2340fa54754db692)",
"oid": "f2d5954d11a1707cdb70b01dfb27c722b6416295"
}
}
}
}
Also find out other encountered the same problem at this and another similar issue at this. I suggest you can try to raise this issue to them at this.

Using GitHub's API to get lines of code added/deleted per commit (on a branch)?

The following gets a raw list of commits for a project's master branch:
https://api.github.com/repos/<organization_name>/<repo_name/commits?page=0&per_page=30
Question 1: How can one get a similar list but for a specific <branchname>?
Question 2: The list of commits above doesn't include any data about the lines of code added/deleted per commit (i.e., a very rough productivity metric). Is there a way to get this data in the query?
You can fetch the specific branch with sha={branchname} param in the /commits params;
sha string SHA or branch to start listing commits from. Default: the repository’s default branch (usually master).
https://api.github.com/repos/<org_name>/<repo_name>/commits?sha=<branchName>&page=0&per_page=30
To get per-file specific changes for each commit, you'd need to check url variable for each commit entity in the response of above URL. From that new endpoint call, you will get a more detailed information of that single commit. files variable in there will contain the changes contained in that commit. Both added & removed codes per file.
An example with my repo;
https://api.github.com/repos/buraequete/orikautomation/commits?sha=master&page=0&per_page=30
If we get the first commits url;
https://api.github.com/repos/buraequete/orikautomation/commits/89792e6256dfccc5e9151d81bf04145ba02fef8f
Which contains the changes you want in files variable as a list.
"files": [
{
"sha": "8aaaa7de53bed57fc2865d2fd84897211c3e70b6",
"filename": "lombok.config",
"status": "added",
"additions": 1,
"deletions": 0,
"changes": 1,
"blob_url": "https://github.com/buraequete/orikautomation/blob/89792e6256dfccc5e9151d81bf04145ba02fef8f/lombok.config",
"raw_url": "https://github.com/buraequete/orikautomation/raw/89792e6256dfccc5e9151d81bf04145ba02fef8f/lombok.config",
"contents_url": "https://api.github.com/repos/buraequete/orikautomation/contents/lombok.config?ref=89792e6256dfccc5e9151d81bf04145ba02fef8f",
"patch": "## -0,0 +1 ##\n+lombok.accessors.chain = true"
},
...
]
Sorry but I don't think there is a way to get those per file changes in the original /commits endpoint call, you have to do multiple calls...

Obtain TFS GIT Commit Details From TFS Work Item Artifact Link

Is it possible to leverage TFS or TS REST api to obtain details for a GIT commit by leveraging the work item commit "ArtifiactLink" url?
So you want to get detail commit information based on a work item artifacts link (while the artifact link type contains commit).
You can achieve that with two REST API, detail steps as below:
1. Get the work item with full expanded
GET https://{instance}/DefaultCollection/_apis/wit/workitems/{id}?api-version1.0&$expand=all
For TFS2015, the format looks like:
GET http://tfsServer:8080/tfs/DefaultCollection/_apis/wit/workitems?ids={id}&$expand=all&api-version=1.0
For VSTS, the format looks like:
GET https://account.visualstudio.com/DefaultCollection/_apis/wit/workitems?ids=7&$expand=all&api-version=1.0
2. Get commit(s) and related repo(s) linked in the above work item
Search in the response of the step1 REST API, get the part which rel is ArtifactLink and the url start with vstfs:///Git/Commit. The URL format is
vstfs:///Git/Commit/{project ID}%2F{repo ID}%2F{commit ID}
Such as part of the REST API response as:
{
"rel": "ArtifactLink",
"url": "vstfs:///Git/Commit/b959f22b-eeb7-40dc-b37e-986377eaa86f%2F4cfde261-fec3-451c-9d41-a400ba816110%2Fb3c3c5b8718f403402be770cb3b5912df7c64dd6",
"attributes": {
"authorizedDate": "2017-09-26T03:14:03.98Z",
"id": 92,
"resourceCreatedDate": "2017-09-26T03:14:03.98Z",
"resourceModifiedDate": "2017-09-26T03:14:03.98Z",
"revisedDate": "9999-01-01T00:00:00Z",
"name": "Fixed in Commit"
}
}
The project ID is b959f22b-eeb7-40dc-b37e-986377eaa86f, the repo ID is 2F4cfde261-fec3-451c-9d41-a400ba816110 and the commit ID is b3c3c5b8718f403402be770cb3b5912df7c64dd6.
3. Get commit(s) details
Use the project ID, repo ID and commit ID you get in step2 to get a single commit:
GET https://{instance}/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version={version}
For TFS 2015, the format looks like:
GET http://tfsServer:8080/tfs/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version=1.0
For VSTS, the format looks like:
GET https://account.visualstudio.com/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version=1.0

Jenkins pipeline read commit status from webhook

I'm trying to find a way to have a Jenkins job be triggered by a PR comment matching a particular pattern, have the job check to see if the PR's latest commit status is PASSED, which would be set by a separate CI job that runs the moment the PR opens, and merge the code. The PR's comment would symbolize that the code has been properly peer reviewed, the commit status represents that all front and back end unit tests have already passed.
The only part of this that I'm really unsure about is retrieving the commit status from the webhook payload. Any advice?
TLDR
Call the statuses API to list all statuses created against that SHA or use the combined status API.
Finding the latest commit
If you don't have the SHA of the latest commit, then there are a few ways to get this from the API endpoints.
This will assume there is an application that listens for webhook events, specifically the IssuesCommentEvent, to parse the comment's body for the 'particular pattern'.
Get the URL to the Pull Request
A webhook event will contain the Pull Request url in the JSON body, e.g.:
{
"action": "created",
"issue": {
...
"pull_request": {
"url": "https://api.github.com/repos/sample/mysample/pulls/13",
...
}
}
}
}
Get the SHA or statuses endpoint
Use the pull request API endpoing url returned in the previous step. The JSON body will contain a statuses_url value, which will return each status created against that SHA or, or get the SHA and call any of the previously mentioned statuses endpoints.
{
"statuses_url": "https://api.github.com/repos/sample/mysamples/statuses/1985617647f17fe4fc85efeeaffef24581a12488",
...
"head":{
"sha": "1985617647f17fe4fc85efeeaffef24581a12488",
}
}

How can I use github api to get all tags or releases for a project?

I would like to know how to use the github-api to get all the current releases or tags for a project. I have seen the documentation for tags in github-api but I don't see a way to list all tags or list all releases but only list a specific tag by :sha.
It's possible, but the documentation is perhaps not in the place you'd expect it to be.
http://developer.github.com/v3/git/refs/
You can also request a sub-namespace. For example, to get all the tag
references, you can call:
GET /repos/:owner/:repo/git/refs/tags
This morning I received an answer to this same question that I posted to the github support team. Not sure how I can attribute the answer correctly but here is their response.
Quote from Ivan Žužak of Github support team
You can get a list of all tags for a repository using this API call:
http://developer.github.com/v3/repos/#list-tags
So, for example, making the following cURL request will return the list of tags for the libgit2/libgit2 repository:
$ curl -v "https://api.github.com/repos/libgit2/libgit2/tags"
Note: for the releases specifically of a GitHub repo, you now have (since Sept. 25th, 2013), an api to list all the releases:
List releases for a repository
Users with push access to the repository will receive all releases (i.e., published releases and draft releases).
Users with pull access will receive published releases only.
GET /repos/:owner/:repo/releases
Response
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
[
{
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name}",
"id": 1,
"tag_name": "v1.0.0",
"target_commitish": "master",
"name": "v1.0.0",
"body": "Description of the release",
"draft": false,
"prerelease": false,
"created_at": "2013-02-27T19:35:32Z",
"published_at": "2013-02-27T19:35:32Z"
}
]
Get a single release
GET /repos/:owner/:repo/releases/:id
In v4, graphQL, you can use this query https://developer.github.com/v4/object/tag/
query {
repository(owner: "onmyway133", name: "Scale") {
refs(refPrefix: "refs/tags/", last: 2) {
edges {
node {
name
}
}
}
}
}
The API call to get all tags as JSON list is:
https://api.github.com/repos/{OWNER}/{REPO}/tags
Example: https://api.github.com/repos/alacritty/alacritty/tags
The response is sorted: more recent tags come first.
To get releases change /tags to /releases. And don't add a trailing slash / to the end.
This will list all your Releases (tags) on Github. Might need to tweek the grep part depending on your tag naming conventions:
curl -s 'https://github.com/username/reponame/tags/'|grep -o "$Version v[0-9].[0-9][0-9]"
If cutting-n-pasting the URL from the "clone repo" section, remember to chop the.git off at the end of the address when constructing above URL- HTH- Terrence Houlahan