Get when the file was last updated from a Github repository
Using Python
pip install 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)
Output:
population
5
2020-04-14 15:09:26
https://github.com/PyGithub/PyGithub
If you know the exact file path, you can use list commits on repository API specifying a path
which only includes commits with this specific file path and then extract the most recent commit (the most recent is the first one) :
Using Rest API v3
https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1
Using curl & jq :
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'
Using 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
}
}
}
}
}
}
}
}
Try it in the explorer
Using curl & jq :
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'