代码之家  ›  专栏  ›  技术社区  ›  Rogelio Blanco

如何从GitHub API获取最后一次提交

  •  25
  • Rogelio Blanco  · 技术社区  · 8 年前

    使用GitHub API(RESTAPI v3)从git存储库获取最新提交信息的最佳方法是什么。

    GET /repos/:owner/:repo/commits/master
    我可以假设响应的对象“commit”是来自分支主机的最新提交吗?

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

    4 回复  |  直到 6 年前
        1
  •  50
  •   VonC    8 年前

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

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

    • 但你也可以考虑 the last push event :这将表示用户推送到该repo的最后一次和最近一次提交(在任何分支上)。

        2
  •  7
  •   Bertrand Martel    5 年前

    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
  •  5
  •   pythonNovice    4 年前

    public 将不会显示推送到私有存储库的事件。 https://api.github.com/users/<username>/events/public

        4
  •  3
  •   Anton    5 年前

    如果您只需要某个分支的最新提交的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}"