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

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).

Related

Github API get user code merged into master branch of specific repo

Just had a question regarding GitHub's REST api, I am looking through the documentation but cant seem to find what I am looking for. Is there a way to look up how much code a user has merged into the master branch over a period of time of a specific repo?
essentially I would like to get a return of
user x committed x lines of code in the range of May 2020 to May 2021. Something like that.
GITHUB API
Unfortunately, the Github API doesn't provide those informations (yet?), you'll only get the amount of contributions the user made to the repository with the List repository contributors service.
WORKAROUND
There seems to be a workaround:
Consulting this URL: https://github.com/<repo_owner>/<repo_name>/graphs/contributors, I observed the following requisition was made to fill contributors datas:
Request URL: https://github.com/<repo_owner>/<repo_name>/graphs/contributors-data
Request Method: GET
Request Headers: Not Sure...
This endpoint will return something similar to the following object:
[
{
"total":N,
"author":{
"id":<user_id>,
"login":"<user_name>",
"avatar":"<user_avatar>",
"path":"/<user_name>",
"hovercard_url":"/users/<user_name>/hovercard"
},
"weeks":[
{
"w":1586044800,
"a":0,
"d":0,
"c":0
},
{
"w":1586649600,
"a":0,
"d":0,
"c":0
},
...
]
}
]
On this response, you'll have a list of contributors with contributors' details, where:
total: represent the amount of contribution this user made to the repository.
author: the Github user datas.
weeks: a list of weekly contribution where for each week:
w: is the week id
a: the amount of lines added
d: the amount of lines deleted
c: the amount of commits made
Therefore, to get what you want, you'll have to sum all weeks contributions for each user.
Observation: I understand that to do so, you need to have access to the repository insights (they may not be available to any user).

GCS storage create folder API

Having trouble creating folder in a bucket in Google Cloud storage thru a API
I have already tried the curl call for the API with all varying possibilities for the request json format.
ABC is not really the organization but gave it to obfuscate real data. I have also setup the variable $access_token using gcloud call to get access-token.
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $access_token" -d '{"displayName":"[vicks]"}' https://cloudresourcemanager.googleapis.com/v2/folders?parent=ABC
{
"error": {
"code": 400,
"message": "field [Folder.display_name] has issue [invalid format]",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "Folder.display_name",
"description": "invalid format"
}
]
}
]
}
}
I am expecting the API call to create the directory but it fails showing error in display_name format though I have followed the document at https://cloud.google.com/resource-manager/docs/creating-managing-folders
Unfortunately, the link you provided is about the API that will help you create a folder within your organization and not inside a Cloud Storage bucket. This can be seen by looking at the first graph in the page you linked.
Thankfully, there is still a solution that might help you achieve what you’re looking for. While Google Cloud Storage objects are stored in a flat namespace, tools like gsutil or the even the Google Cloud Console can provide a hierarchical view of the objects by following simple naming rules (it is essentially an emulation of subdirectories).
1) To treat an object as a directory, you can create an empty object that ends with “/”. If you want a subdirectory called ‘abc’ then you can call the object ‘abc/’ and gsutil will treat it like an empty directory. To insert an object/file into the subdirectory, you may simply copy an object/file to the destination URL including ‘abc’ such as “gs://your-bucket/abc”.
2) Another less commonly used way is to create an empty object that ends with “$folder$”. If you want a subdirectory called ‘abc’ then you can call the object ‘abc$folder$’ and gsutil will treat it like an empty directory. Similarly to the previous point, to insert an object/file into the subdirectory, you may simply copy an object/file to the destination URL including ‘abc’ such as “gs://your-bucket/abc”.
I would highly suggest you read through this link to get a great understanding of how Google Cloud Storage subdirectories work. Additionally, I’ve found another previously answered stack question that is very relevant to your question and can be of great help to you as well.

Count open pull requests and issues on GitHub

I like to count all open pull requests and issues in a repository with help of the GitHub API. I found out that the API endpoint /repos/:owner/:repo result contains a open_issues property. However this is the sum of the amount of issues and pull requests.
Is there a way to get or calculate the amount of open issue and pull requersts in a repository?
osowskit is correct, the easiest way to do this is to iterate over the list of issues and the list of pull requests in a repository (I'm assuming you would like to get separate counts for each, reading between the lines of your question).
The issues API will return both issues and pull requests, so you will need to count both and subtract the number of pull requests from the number of issues to get the count of issues that aren't also pull requests. For example, using the wonderful github3.py Python library:
import github3
gh = github3.login(token='your_api_token')
issues_count = len(list(gh.repository('owner', 'repo').issues()))
pulls_count = len(list(gh.repository('owner', 'repo').pull_requests()))
print('{} issues, {} pull requests'.format(issues_count - pulls_count, pulls_count))
A more efficient way (than the accepted answer) is to use the search API.
To get the number of open issues you can call (replace with your org and repo):
https://api.github.com/search/issues?q=repo:realm/realm-java%20is:issue%20is:open&per_page=1
and to get the number of PR's:
https://api.github.com/search/issues?q=repo:realm/realm-java%20is:pr%20is:open&per_page=1
You can of course remove is:open if you want both open and closed issues/pr's.
Both will return "total_count" with the result. Note that I added per_page=1 to not actually retrieve all the issues.
With Github Graphql API, you can now do this in one single request:
{
repository(owner: "mui-org", name: "material-ui") {
issues(states: OPEN) {
totalCount
}
pullRequests(states: OPEN) {
totalCount
}
}
}
Output:
{
"data": {
"repository": {
"issues": {
"totalCount": 471
},
"pullRequests": {
"totalCount": 47
}
}
}
}
You don't need to iterate over all pull requests. The GitHub API returns pages and in the link header, you have access to the first, previous, next and last pages. You can use that to implement a more efficient algorithm:
1) Fetch the first page and specify a page size of 1
2) Get the value of the last page link (i.e. the number of pages)
3) You will thereby have the number of pages, hence of PRs and will not have to fetch all of these pages.
In case anyone is wondering, the accepted answer of constructing a search query with the Github Search API can take an is:merged argument instead of is:open, even though that option isn't well documented in the Github API:
For example:
https://api.github.com/search/issues?q=repo:realm/realm-java%20is:issue%20is:merged&per_page=1

Get Reactions using the Github api

Github issues may contain "reactions" for quite a while (as described here: https://github.com/blog/2119-add-reactions-to-pull-requests-issues-and-comments)
I would like to receive that information using the Github api, but there doesn't seem to be anything like that when getting an issue e.g.
api.github.com/repos/twbs/bootstrap/issues/19575
that information does not seem to be inside that response. Also, I did not find another API call that could retrieve that information. How to get those "reactions"?
This is now possible, being the preview state (meaning, you have to pass a custom Accept header in the request). Check out the GitHub API documentation page
Example
$ curl -H 'Accept: application/vnd.github.squirrel-girl-preview' https://api.github.com/repos/twbs/bootstrap/issues/19575/reactions
[
{
"id": 257024,
"user_id": 947110,
"content": "+1"
},
...
{
"id": 888868,
"user_id": 1889800,
"content": "+1"
}
]
The endpoint looks like this:
GET /repos/:owner/:repo/issues/:number/reactions
You can even pass a content parameter (querystring) indicating what kind of reaction you want to retrieve.

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