首页 > 解决方案 > 获取文件上次从 Github 存储库更新的时间

问题描述

通过使用 GitHub v3 API,我可以获取文件内容(如果它是一个文件夹,我可以获取文件列表) 。 例子:

https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]

但是我怎么知道文件上次更新的时间呢?有那个API吗?

标签: githubgithub-api

解决方案


如果您知道确切的文件路径,则可以在存储库 API 上使用 list commits指定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'

使用 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
              }
            }
          }
        }
      }
    }
  }
}

在资源管理器中尝试

使用 &

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'

推荐阅读