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

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

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.

How to get Pull Requests associated with a Work Item via the Azure DevOps API

I am trying to list all Pull Requests associated with a Work Item but according to the Work Items API there doesn't seem to be a way to get it:
GET https://dev.azure.com/{org}/{project}/_apis/build/builds/123456/workitems?api-version=6.0
The above returns list of work items, such as:
{
"count": 40,
"value": [
{
"id": "156267",
"url": "https://dev.azure.com/xxx/_apis/wit/workItems/12345"
},
...
]
}
Now, if I still decide to query each returned work item I still don't see a Pull Request.
For example:
GET https://dev.azure.com/xxx/_apis/wit/workItems/12345
The above returns a JSON object about updates done to the workitem, but this can be a commit, state update or comments.
Is there a way to get a list of PRs per work item?
Thanks
You need to add &$expand=relations:
GET https://dev.azure.com/xxx/_apis/wit/workItems/12345?$expand=relations
Now in the response you will get the linked PR under the relations.

Azure DevOps REST Api Tags call (Repos > Tags)

Is there a Repos Tags REST Api call to get the data from this page? (Repos > Tags)
It would be awesome if it also includes CREATE, PATCH and DELETE.
The tab it self use https://dev.azure.com/{organization}/_git/{repo}/tags?__rt=fps&__ver=2
which I interpret as a bad sign.
EDIT 1: Create a Tag
Create: the Create Tag button use: Annotated Tags
So what is missing in this REST Api call is a LIST to get the {objectId} of the elements.
EDIT 2: List & Delete Tags
List: To list all Tags objectId, I found out that you can use Refs - List
Delete: I think this call is complete undocumented. But you can comprehend that the TFS use the following payload to do this job:
var json = {
name: `refs/tags/${xName}`,
newObjectId: '0000000000000000000000000000000000000000',
oldObjectId: xObjectId
};
var payload = [json];
Post this payload to https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.1
EDIT 3: behavior on git client side
The only way I found to update the git tags on client side is here:
git tag -l | xargs git tag -d
git fetch --tags
1.To create Tags: Create/Get tag apis
Create tag:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/annotatedtags?api-version=6.0-preview.1
Get tag:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/annotatedtags/{objectId}?api-version=6.0-preview.1
2.To list Tags: We don't have "Annotated Tags-List", but we have Refs-tags which does similar job. (Hint from Mar Tin, thanks to him!)
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=tags/&api-version=6.0-preview.1
3.To delete the tag(Fetch it from F12):
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{RepoName}/refs?api-version=5.1
Request Body(Don't forget the [ and ],it's necessary when testing in PostMan):
[{
"name": "refs/tags/{TagName}",
"newObjectId": "0000000000000000000000000000000000000000",
"oldObjectId": "{OldObjectID}"
}]
Note: All the info about ObjectID can be fetched via Refs-tags api in tip2.
For your question about whether the tag is really deleted or is it
only moved to a sneaky archive.
You're see this when trying to delete the Tag via the Delete tag button in web portal:
And the corresponding request I fetched from Edge(F12) is:
So the tag will be permanently deleted, it's not just moved to a sneaky archive.

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

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"
}
]

Get information about Clones, Unique Clones, Views and Unique Visitors using github API

Is there any way to get information (in JSON or Java objects) about Clones, Unique Clones, Views and Unique Visitors using github API on the basis of date or month or year?
Not that I know of: both the repos API and the statistic API do not expose those data.
All you have are "followers" (as I mentioned in 2012) but https://github.com/<username>/<reponame>/graphs/traffic remains the only source for traffic-related data.
Perhaps it is a bit late (almost 6 years), but, is this what you're looking for? Following the link, we see two examples to download clones information from a github repo:
In order to download the clones in shell, use:
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/traffic/clones
In order to download the clones in javascript, use:
await octokit.request('GET /repos/{owner}/{repo}/traffic/clones', {
owner: 'octocat',
repo: 'hello-world'
})
The result is a JSON object:
{
"count": 173,
"uniques": 128,
"clones": [
{
"timestamp": "2016-10-10T00:00:00Z",
"count": 2,
"uniques": 1
},
...
]
}
The ... indicate more values (follow the link for the complete example).