从Github存储库上次更新文件时获取

Nic*_*rin 7 github github-api

我可以使用GitHub v3 API获取文件内容(如果它是文件夹,我可以获取文件列表). 例:

https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
Run Code Online (Sandbox Code Playgroud)

但是如何知道文件上次更新的时间?那有API吗?

小智 11

使用Python

pip 安装 PyGithub

from github import Github
g = Github()
repo = g.get_repo("datasets/population")
print(repo.name)
commits = repo.get_commits(path='data/population.csv')
print(commits.totalCount)
if commits.totalCount:
    print(commits[0].commit.committer.date)
Run Code Online (Sandbox Code Playgroud)

输出:

population
5
2020-04-14 15:09:26
Run Code Online (Sandbox Code Playgroud)

https://github.com/PyGithub/PyGithub


Ber*_*tel 6

如果您知道确切的文件路径,则可以在存储库API上使用列表提交,指定一个path仅包含具有此特定文件路径的提交,然后提取最新的提交(最新的是第一个):

使用Rest API v3

https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1

使用

curl -s "https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1" | \
     jq -r '.[0].commit.committer.date'
Run Code Online (Sandbox Code Playgroud)

使用GraphqQL API v4

{
  repository(owner: "bertrandmartel", name: "speed-test-lib") {
    ref(qualifiedName: "refs/heads/master") {
      target {
        ... on Commit {
          history(first: 1, path: "jspeedtest/build.gradle") {
            edges {
              node {
                committedDate
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在资源管理器中尝试

使用

curl -s -H "Authorization: Bearer YOUR_TOKEN" \
     -H  "Content-Type:application/json" \
     -d '{ 
          "query": "{ repository(owner: \"bertrandmartel\", name: \"speed-test-lib\") { ref(qualifiedName: \"refs/heads/master\") { target { ... on Commit { history(first: 1, path: \"jspeedtest/build.gradle\") { edges { node { committedDate } } } } } } } }"
         }' https://api.github.com/graphql | \
     jq -r '.data.repository.ref.target.history.edges[0].node.committedDate'
Run Code Online (Sandbox Code Playgroud)

  • 也许只是我,但我发现 GraphQL 非常不直观 (4认同)
  • 如果使用 REST API v3,我们可以使用 `&page=1&per_page=1` 来避免加载所有提交。 (2认同)