Peter
Peter

Reputation: 14118

GitHub GraphQL query not returning last commit

Everywhere I read, I see I can get the latest commit for a GitHub repository using this GraphQL query:

{
repository(owner: "petermorlion", name: "RedStar.Amounts") {
    defaultBranchRef {
      name
      target {
        ... on Commit {
          history(first: 1) {
            edges {
              node {
                committedDate
              }
            }
          }
        }
      }
    }
  }
}

And this works. For this repository. As you can see (at the time I'm writing this), both the GraphQL explorer and the GitHub UI say 7th May is the latest commit:

GitHub UI results

GitHub GraphQL results

However, if I run this on another repository, I'm getting the first commit. Change the owner to ystk and the repository name to debian-libidn. GraphQL tells me the latest commit is 13th October 2009:

GitHub GraphQL wrong results

But the GitHub UI shows it is in fact 13th May 2011:

GitHub UI correct results

Is my query wrong? Should I be adding an orderby somewhere (I saw that it can't be added to history)?

Upvotes: 13

Views: 776

Answers (1)

Siavas
Siavas

Reputation: 5090

The GraphQL query is actually right.

What has gone wrong there is the dating of the apparently older commit, which although is authored in 2009 it is indeed more recent than the 2011 one. By newer here we refer to being pushed at a later point than the other one. This means the timestamp of the commit was amended, which was possibly caused by a misconfiguration of the author's clock at the time.

The main page will always show the most recently pushed commit, which for debian-libidn, for example, it shows the 2009-dated one.

To detail further, there are three timestamp-related properties of Commit that you could retrieve:

  • committedDate: the date at which point the commit was created locally. Updated when a commit was changed and affected by the author's clock

  • authoredDate: by default same as committedDate, can be changed with the --date optional parameter when committing changes, also affected by the author's clock

  • pushedDate: the date when the commit was pushed to the repository, according to the server's time (in this case GitHub's)

From the above, it seems you would prefer to use pushedDate instead, as that will give the real order of commits as they were pushed to the repo.

Upvotes: 11

Related Questions