How can I get last commit from GitHub API

纵饮孤独 提交于 2020-06-24 08:29:05

问题


Which is the best way to get the latest commit information from a git repository using GitHub API (Rest API v3).

Option 1: GET /repos/:owner/:repo/commits/master
Can I assume that the object 'commit' of the response is the latest commit from branch master?

Option 2: GET /repos/:owner/:repo/git/commits/5a2ff
Or make two calls, one to get the sha by getting the HEAD ref from master and then get the commit information using the sha returned.


回答1:


It depends on your definition of "last".

  • for a given branch (like master), GET /repos/:owner/:repo/commits/master is indeed the last (most recent) commit.

  • But you can also consider the last push event: that would represent the last and most recent commit done (on any branch), pushed by a user to this repo.




回答2:


You can also use Github GraphQL v4 to get the last commit of the default branch :

{
  repository(name: "linux", owner: "torvalds") {
    defaultBranchRef {
      target {
        ... on Commit {
          history(first: 1) {
            nodes {
              message
              committedDate
              authoredDate
              oid
              author {
                email
                name
              }
            }
          }
        }
      }
    }
  }
}

Or for all branches :

{
  repository(name: "material-ui", owner: "mui-org") {
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              history(first: 1) {
                nodes {
                  message
                  committedDate
                  authoredDate
                  oid
                  author {
                    email
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Try it in the explorer



来源:https://stackoverflow.com/questions/45726013/how-can-i-get-last-commit-from-github-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!