Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 493
FTM Avatar asked Jan 16 '18 16:01

FTM


2 Answers

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
                            }
                        }
                    }
                }
            }
        }
    }
}
like image 75
FTM Avatar answered Oct 10 '22 05:10

FTM


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

like image 12
Bertrand Martel Avatar answered Oct 10 '22 05:10

Bertrand Martel