Github GraphQL explorer - Get contributions per repository for a user - github

I'm creating a christmas summary for my team and I am trying to use githubs GraphQL Explorer over at https://docs.github.com/en/graphql/overview/explorer
I'm having some trouble to create the query I want to build. I'm trying to get contributions per repo for a user. Can anyone help out?
I tried to build upon a query I found online, but it only gave me per day.
I figured it out!
{
search(query: "org:test", type: REPOSITORY, last: 100) {
nodes {
... on Repository {
name
defaultBranchRef {
name
target {
... on Commit {
history(first: 100, since: "2013-07-11T00:00:00") {
totalCount
nodes {
... on Commit {
committedDate
additions
author {
name
email
}
}
}
}
}
}
}
}
}
}
}

Related

How to compare two branches in github with GraphQL?

Can we compare two branches with the Github GraphQL?
From their v3 rest API, you can do:
/repos/:owner/:repo/compare/:base...:head
(docs: https://developer.github.com/v3/repos/commits/#compare-two-commits)
and this works with SHA's, branches, tags, etc.
However, I'm unable to find it's equivalent GraphQL query in the docs.
This is my attempt so far :
I'm able to get the list of commits for each branch seperately, however, the entire history is loaded and I would only like the difference between canary branch and nightly branch.
query{
repository(owner:"samridh",name:"release-generator"){
name
branch0: ref(qualifiedName: "canary"){
target{
... on Commit {
history(first:100){
...CommitFragment
}
}
}
}
branch1: ref(qualifiedName: "nightly"){
target{
... on Commit {
history(first:100){
...CommitFragment
}
}
}
}
}
}
fragment CommitFragment on CommitHistoryConnection {
totalCount
nodes {
oid
message
committedDate
author {
name
email
}
}
pageInfo {
hasNextPage
endCursor
}
}
This would have been done as :
/repos/samridh/release-generator/compare/nightly...canary
in the v3 REST API
Unfortunately, after scrolling through hours and hours of the github community page, it seems that as of this date, the API is not migrated on the v4, and must be done via v3 itself.
However, the v3 API only supports 250 commits, any commits beyond that will be ignored and not shown. This can be worked around using graphQL though.
Fire this query to get the starting and ending points:
query getStartAndEndPoints {
repository(owner: "samridh", name: "release-generator") {
endPoint: ref(qualifiedName: "canary") {
...internalBranchContent
}
startPoint: ref(qualifiedName: "nightly") {
...internalBranchContent
}
}
}
fragment internalBranchContent on Ref {
target {
... on Commit {
history(first: 1) {
edges {
node {
committedDate
}
}
}
}
}
}
This will give you the start and end date of the query.
Plugin these values to :
query findDifference{
repository(owner:"samridh",name:"release-generator"){
ref(qualifiedName: "canary"){
target{
... on Commit {
history(
first : 100,
after: $(value of previous end cursor) #keep it empty first time
until : $(endDate),
since: $(startDate),
){
...CommitFragment
}
}
}
}
}
}
fragment CommitFragment on CommitHistoryConnection {
totalCount
nodes {
oid
}
pageInfo {
startCursor
hasNextPage
endCursor
}
}
and extract all the oid, 100 at a time ( Github GraphQL only supports 100 at a time )
Finally, you can call the v3 API, likewise :
/repos/samridh/release-generator/compare/<commit1>...<commit100>
/repos/samridh/release-generator/compare/<commit101>...<commit200>
/repos/samridh/release-generator/compare/<commit201>...<commit300>

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

GraphQL - How to get all the commits after a given commit?

Is there a way in Github v4 API to get all the commits after a given commit?
I have tried this, but it doesn't give me any result.
{
repository(owner: "karthikeayan", name: "puhar-petti") {
ref(qualifiedName: "master") {
target {
... on Commit {
history(last: 100, before: "d1b7ccc044be72490525a3fe1b819440f4927cba 0") {
pageInfo {
startCursor
endCursor
}
edges {
node {
oid
messageHeadline
messageBody
}
}
}
}
}
}
}
}
With git log I can accomplish this by doing
git log d1b7ccc044be72490525a3fe1b819440f4927cba..HEAD
In my experiments, the before cursor is ignored.
In v4 you can search forward by pages, as in this query.graphql gist.
history(first: 100, after: "d1b7ccc044be72490525a3fe1b819440f4927cba 0") {
...
}
In v3 you might be able to use the Search Commits API that is being previewed. However, it only searches the default branch.

Github GraphQL - Getting a repository's list of commits

I am using GraphQL to get some data from a list of repositories using Github's GraphQL (v4) API. I want to get a list of the latest commits from a repository, no matter what is the commit's branch/tag/ref.
For now I am doing the following to get the list of commits from a certain repository:
... on Repository{
refs(refPrefix:"refs/",orderBy:$refOrder,first:1){
edges{
node{
... on Ref{
target{
... on Commit{
history(first:10){
totalCount
edges{
node{
... on Commit{
committedDate
}
}
}
}
}
}
}
}
}
}
}
Where $refOrder is an object I am sending together with the request and it is defined below:
{
"refOrder": {
"direction": "DESC",
"field": "TAG_COMMIT_DATE"
}
}
This piece of code is working, but not retrieving the results I want. The response comes back with a list of commits, but not necessarily the last commits from the repository. When I go to the repository page and click on "Commits", I usually see a list of commits that are more recent than what I got as results from my API call.
What am I missing? Should I try a different refPrefix or orderBy argument? I have already tried "master" as the refPrefix, but faced the same problem.
Just realized that what I was looking for is a field which exists in the Repository object called defaultBranchRef. Using this field I was able to retrieve the data I was looking for.
My query now looks like this:
... on Repository{
defaultBranchRef{
target{
... on Commit{
history(first:10){
edges{
node{
... on Commit{
committedDate
}
}
}
}
}
}
}
}
If you are also interested in getting the latest commits for all branches (not just the default branch), you can request reference with prefix refs/heads/ :
{
repository(owner: "bertrandmartel", name: "callflow-workshop") {
refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, first: 100) {
edges {
node {
... on Ref {
name
target {
... on Commit {
history(first: 2) {
edges {
node {
... on Commit {
committedDate
}
}
}
}
}
}
}
}
}
}
}
}
In your case using refs/ also gave you tag ref.
Try it in the explorer

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.