【问题标题】:How can I get last commit from GitHub API如何从 GitHub API 获取最后一次提交
【发布时间】:2021-11-25 02:35:22
【问题描述】:

这是使用 GitHub API(Rest API v3)从 git 存储库获取最新提交信息的最佳方式。

选项 1:GET /repos/:owner/:repo/commits/master
我可以假设响应的对象“提交”是分支 master 的最新提交吗?

选项 2:GET /repos/:owner/:repo/git/commits/5a2ff
或者进行两次调用,一次通过从 master 获取 HEAD ref 来获取 sha,然后使用返回的 sha 获取提交信息。

【问题讨论】:

  • 我正在使用 Rest API v3
  • 为什么/repos/:owner/:repo/commits/:branch 不是最新的提交?

标签: git github github-api


【解决方案1】:

这取决于你对“最后”的定义。

  • 对于给定的分支(如master),GET /repos/:owner/:repo/commits/master 确实是最后一次(最近的)提交。

  • 但您也可以考虑the last push event:它代表最后和最近完成的提交(在任何分支上),由用户推送到此存储库。

【讨论】:

  • 如何将 GET 思想与 api 结合使用。它是如何包含在 curl 中的?
  • 但是我需要传递一个所有者。我怎么知道那个开发者是最后一个向 repo 推送东西的开发者?
  • @Dani 为什么将存储库的所有者传递给您的问题?
  • @SridharSarnobat 是的,但不要使用您的 GHE 帐户密码,请使用令牌:github.blog/changelog/…(2020 年 11 月)
【解决方案2】:

您也可以使用Github GraphQL v4 获取默认分支的最后一次提交:

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

或者对于所有分支:

{
  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

【讨论】:

    【解决方案3】:

    从用户获取最新提交的另一种方法是使用以下端点。澄清一下,这只会显示public 事件,因此不会显示推送到私有存储库。 https://api.github.com/users/<username>/events/public

    【讨论】:

    • 哇,您甚至不必进行身份验证。感谢分享!
    【解决方案4】:

    如果您只需要某个分支的最新提交的 SHA1,这里有一个 curl 请求可以做到:

    curl -s -H "Authorization: token {your_github_access_token}" \
    -H "Accept: application/vnd.github.VERSION.sha" \ 
    "https://api.github.com/repos/{owner}/{repository_name}/commits/{branch_name}"
    

    【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2018-01-05
    • 2021-12-26
    • 2017-06-09
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多