【发布时间】:2018-10-16 01:57:17
【问题描述】:
我可以通过using the GitHub v3 API. 获取文件内容(如果是文件夹,我可以获取文件列表) 示例:
https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
但是我怎么知道文件上次更新的时间?有相应的 API 吗?
【问题讨论】:
标签: github github-api
我可以通过using the GitHub v3 API. 获取文件内容(如果是文件夹,我可以获取文件列表) 示例:
https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
但是我怎么知道文件上次更新的时间?有相应的 API 吗?
【问题讨论】:
标签: github github-api
点安装 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)
输出:
population
5
2020-04-14 15:09:26
【讨论】:
您实际上可以使用您特别提到的请求来确定您想要什么。
请注意,以下所有日期/时间均在 GMT 下(当然)。
复制并粘贴以下命令以查找用户 YenForYang 在存储库 ForStackExchange 中的文件夹/文件的最后修改日期和时间:
\curl -sIA. --ignore-content-length \ -H"If-Modified-Since: Sun May 01 00:00:00 9999" \ "https://api.github.com/repos/YenForYang/ForStackExchange/contents/Folder/File?ref=branch" \ | \grep -m1 -oP "(?<=Last-Modified: )[ADFJMNOSTWa-eghilnoprtuvy0-9:, ]{25}" \(如果 Perl 正则表达式不可用,您可以
... | grep -F -m1 "Last-Modified:")
上述命令应返回 (GMT):
Thu, 27 Dec 2018 11:01:26(或者以后,如果我出于某种原因更新文件)
请注意,如果未指定ref 参数,则为ref=master。
如果您不能复制和粘贴,并且不关心 API 速率限制,您可能会选择较短的:
\curl -sIL "api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch" | \grep "^Las"
如果你在 Windows 上没有 grep,只需使用 find "Last-Modified: " 代替(双引号是必要的)。
如果您在 Windows 上没有 curl(下载它...或)使用 Powershell
(iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
【讨论】:
考虑到 git 没有 store file timestamps (and other metadata like permissions and ownership),这将是令人惊讶的,原因是 I detailed here。
因此,远程存储库端(此处为 GitHub)上也不存在该信息。
【讨论】:
如果您知道确切的文件路径,则可以使用list commits on repository API 指定仅包含具有此特定文件路径的提交的path,然后提取最近的提交(最近的是第一个):
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'
{
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'
【讨论】:
&page=1&per_page=1 来避免加载所有提交。