Get GitHub Repository Insights via GitHub GraphQL API (v4) - github

I want to obtain information about the number of times my projects have been viewed, cloned and where the traffic came from (individually).
I can currently view this Traffic information by clicking on the Insights button of the repository (via the web interface).
Is there a schema in the GitHub v4 GraphQL API to retrieve this information?
The closest I got was the following; nodes didn't contain any sort of statistical data:
{
viewer {
repositories(first: 100) {
totalCount
nodes {
name
description
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
// response
{
"data": {
"viewer": {
"repositories": {
"totalCount": 55,
"nodes": [
{
"name": "Repo Name",
"description": "Repo Description"
},
{
...
}
}
}
}
}

Currently it is not possible to retrieve traffic information using GraphQL API (as explained in here).
Alternately you can use REST API v3 as described in GitHub documentation.
Please notice that reviewing traffic requires tokens with push permissions to the desired repositories.

Related

Retrieve ALL Github issues of a specific Project using the GraphQL API

I've been trying to retrieve all GitHub issues of a specific project using their GraphQL API.
The problem that i have is that i need to specify in the items a first or last param it doesn't work. Although by specifying one of these params i get only a partition of the issues.
I thought that i could get the first 100, then use pagination and get the other 100 etc until the response is an empty list. From what i read, i cannot find a parameter in the items that defines a page.
What are your thoughts on this? Is there a workaround?
Thanks a lot for your time.
This query seems to work fine and gives page info under items,
query{
organization(login: "microsoft") {
projectV2(number: 559) {
title
items(first: 100) {
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
Output,
{
"data": {
"organization": {
"projectV2": {
"title": "Azure TRE - Engineering",
"items": {
"pageInfo": {
"endCursor": "Njc",
"hasNextPage": false
}
}
}
}
}
}
Tested the query using,
GitHub Explorer

GitHub's GraphQL API: How can I get all of the commits I've contributed to a particular public repo?

I'm trying to query GitHub's GraphQL API (v4). What GraphQL query for this API can be used to get all of the commits I've contributed to a particular public repo?
I figured out the solution. (The solution shown below is for getting relevant commits from master, but can easily be adapted for other qualifiedName values.)
First, perform an initial query to obtain user ID:
query {
viewer {
id
}
}
Then the following query can be used to get all contributed commits to a particular repository:
query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
repository(owner: $repository_owner, name: $repository_name) {
ref(qualifiedName: "master") {
target {
... on Commit {
history(author: {id: $user_id}) {
totalCount
nodes {
id
authoredDate
message
}
}
}
}
}
}
}
The query variables take the following form:
{
"repository_owner": "REPOSITORY_OWNER_STRING",
"repository_name": "REPOSITORY_NAME_STRING",
"user_id": "USER_ID_FROM_STEP_1"
}

How to use github graphql v4 api to query all repositories in my organization?

I wanna query all repositories in my organization on github private, i try to use
query {
organization(login:"my-org-name") {
id
name
url
repositories(first:100) {
nodes {
id
name
}
}
}
}
However it returns
{
"data": {
"organization": {
"id": "MDEyOk*********************U4ODUw",
"name": "my-org-name",
"url": "https://github.com/my-org-name",
"repositories": {
"nodes": []
}
}
}
}
can't find any repositories.
I test it on Github Developer, https://developer.github.com/v4/explorer/
you can achieve using search end point (you need to be authenticated)
query myOrgRepos($queryString: String!) {
search(query: $queryString, type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
name
}
}
}
}
}
with query variables
{
"queryString": "org:my_org"
}
Note that as expected you do not get the list repositories of your organization, you get the list of your organization list that you have access
The GraphQL Explorer is an OAuth Application that needs to be given permission to access your organization data.
You can grant this access directly for the GraphQL API Explorer or navigate to it from your Settings -> Applications -> Authorized OAuth Apps
Note that a personal access token (PAT) does not have this restriction, so this isn't required for an application using your token.
Here my example.
You have to be Authenticated to do this request.
url: https://api.github.com/graphql
header: Authorization bearer token
method: POST
query {
viewer {
avatarUrl
login
resourcePath
url
bio
company
createdAt
location
followers(first: 100) {
nodes {
name
url
}
}
repositories(first: 100) {
nodes {
name
description
url
createdAt
collaborators(first: 5) {
nodes {
name
}
totalCount
}
}
totalCount
}
}
}

How to get total number of commits using GitHub API

I am trying to collect some statistics about our project repositories on GitHub. I am able to get total number of commits for each contributor , but it is for default branch.
curl https://api.github.com/repos/cms-sw/cmssw/stats/contributors
The problem is , how can i get the same info for non-default branches , where i can specify a branch name. Is any such operation possible using GitHub API ?
thanks.
You should be able to use GitHub's GraphQL API to get at this data, although it won't be aggregated for you.
Try the following query in their GraphQL Explorer:
query($owner:String!, $name:String!) {
repository(owner:$owner,name:$name) {
refs(first:30, refPrefix:"refs/heads/") {
edges {
cursor
node {
name
target {
... on Commit {
history(first:30) {
edges {
cursor
node {
author {
email
}
}
}
}
}
}
}
}
}
}
}
With these variables:
{
"owner": "rails",
"name": "rails"
}
That will list out each of the author emails for each of the commits of each of the branches in a given repository. It would be up to you to paginate over the data (adding something like cursor: "b7aa251234357f7ddddccabcbce332af39dd95f6" after the first:30 arguments). You'd also have to aggregate the counts on your end.
Hope this helps.

GitHub API: List a users teams within an organization

In GitHub when viewing my organization's user list I'm able to see how many teams a user is a member of.
Clicking on this count shows me which teams a user is in, putting me on the following page:
https://github.com/orgs/my-org/teams?query=%40username
However, I'm trying to achieve the same functionality via the GitHub API, but I've been unable to find an endpoint that lists what teams (within an organization) a user is currently a member of.
One workaround is to loop through all the teams in an organization and get their members list, but this can quickly go through my rate limit, so to be able to do this in one request would be useful.
You can do this with GraphQL API v4 filtering users in teams within an organization with userLogins :
{
organization(login: "my-org") {
teams(first: 100, userLogins: ["johndoe"]) {
totalCount
edges {
node {
name
description
}
}
}
}
}
which gives for instance :
{
"data": {
"organization": {
"teams": {
"totalCount": 2,
"edges": [
{
"node": {
"name": "Employees",
"description": "org employees"
}
},
{
"node": {
"name": "Developers",
"description": "active developers"
}
}
]
}
}
}
}
Source : platform.github.community forum
Unfortunately, GitHub does not yet provide a way to do this.
At the moment, like you suggested, you would have to loop through each team in the organization to get all members within each one, then you would need to loop through all users, and check if the users are members of those teams.
You can increase your rate limit if you use an API token, but the solution is still lousy if you have a large organization.
Heads up!
Providing an invalid/unknown "userLogin" sometimes results in all teams being returned.
Perhaps me doing something wrong, but I opened a ticket with GH about this, so lets see:
https://github.community/t/teams-userlogins-filter-is-not-working-as-expected/206251