I'm able to get the file contents (and if it's a folder, I'm able to get the list of files) by using the GitHub v3 API.
Example:
https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]
But how can I know when the file was last updated? Is there an API for that?
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'
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
That would be surprising, considering git does not store file timestamps (and other metadata like permissions and ownership), for reasons I detailed here.
So that information is not present on the remote repository side (here GitHub) either.
You can actually determine what you want using the request you specifically mentioned.
Note that all dates/times below are under GMT (of course).
Copy & paste the following command to find the last modified date and time of Folder/File in repository ForStackExchange by user YenForYang:
\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}" \
(If Perl regex isn't available, you can ... | grep -F -m1 "Last-Modified:")
The above command should return (GMT): Thu, 27 Dec 2018 11:01:26
(or later, if I update the file for some reason)
Note that if the ref parameter is unspecified, ref=master.
And if you can't copy and paste, and don't care about the API rate limits, you might opt for the shorter:
\curl -sIL "api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch" | \grep "^Las"
And if ya don't have grep on Windows just use find "Last-Modified: " instead (doubles quotes are necessary).
And if you don't have curl on Windows (download it...or) use Powershell
(iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
When you run a public repository with GitHub Pages and want to let your visitors know the history of a page, you can simply put the hyperlink to the https://github.com/<user>/<user>.github.io/commits/main/<path-to-file> for that page on that page.
Related
This curl command works as expected and shows when the repository was first created.
curl https://api.github.com/repos/RaRe-Technologies/gensim | grep created
"created_at": "2011-02-10T07:43:04Z",
But this does not show when a file in that repo was created.
curl
https://api.github.com/repos/RaRe-Technologies/gensim/blob/develop/gensim/scripts/make_wikicorpus.py
| grep created
Is there any way to find the date on which the file was introduced?
You can use https://api.github.com/repos/OWNER/REPO/commits?path=<path/to/file>, as described here.
The results of this request can then be parsed by jq, with the following options .[-1].commit.author.date.
This tells jq to get the last item of the array ([-1]), and then parse the value of commit, then author and then the date, which is the date of the commit.
So using the follwing command
curl "https://api.github.com/repos/RaRe-Technologies/gensim/commits?path=gensim/scripts/make_wikicor
pus.py" | jq -r ".[-1].commit.author.date"
will result in
2012-07-21T20:00:29Z
The alternative, using GitHub CLI gh, and its gh api command:
# Windows
gh api -X GET repos/RaRe-Technologies/gensim/commits \
-f path="gensim/scripts/make_wikicorpus.py" \
--jq ".[-1].commit.author.date"
# Linux
gh api -X GET repos/RaRe-Technologies/gensim/commits \
-f path='gensim/scripts/make_wikicorpus.py' \
--jq '.[-1].commit.author.date'
2012-07-21T20:00:29Z
You don't even need jq if you have gh installed.
After I switched to the new Github Token format, I am not able anymore to perform a call for this github API:
https://docs.github.com/en/rest/issues/comments#list-issue-comments
My script does the following:
[[ -z "$GITHUB_REPO_SLUG" ]] && export GITHUB_REPO_SLUG="$TRAVIS_REPO_SLUG"
GITHUB_API_BASE_URL="https://api.github.com/repos/${GITHUB_REPO_SLUG}"
GITHUB_AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
comment_ids=$(curl -s -H "$GITHUB_AUTH_HEADER" "$GITHUB_API_BASE_URL/issues/$pull_request_id/comments" \
| jq --arg USER "$GITHUB_USER" -r '. | map(select(.user.login==$USER)) | map(.id) | .[]')
for comment_id in $(echo $comment_ids); do
jq -n -r --arg message "$message" '{ body: $message }' \
| curl -s -H "$GITHUB_AUTH_HEADER" "$GITHUB_API_BASE_URL/issues/comments/$comment_id" -X PATCH --data #- > /dev/null
echo "Edited comment with id $comment_id of PR $pull_request_id"
done
The api call that list comments ids responds:
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/issues#list-issue-comments"
}
This still works using tokens in the old format, but doesn't with tokens in the new format.
Is there any way to use the GHP to call that api?
The github user associated with the github token must be provided writing access to the repository.
Since this was missing API responded 404 (default behaviour set for Github APIs).
i need some equivalent command to the following command:
git log --pretty="* %s%n%b" BRANCH2...TAGNAME1
i tried already the compare feature of the api but sadly without the result i expected:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=BRANCH2&to=TAGNAME1&straight=true" | jq .
what i get is:
"commit": null,
"commits": [],
but git log gives me the following:
* commit 1 description
* commit 2 description
so what i search and need is to compare BRANCH2 with TAGNAME1 and list the different commits.
this is later on needed in order to generate a changelog file which is pushed to a new tag.
This would be the compare API
Using the gitlab-org/gitlab repo as an example, to compare the tags/branches v14.8.0-ee to v14.9.0-ee:
curl https://gitlab.com/api/v4/projects/278964/repository/compare?from=v14.8.0-ee&to=v14.9.0-ee
If you're not getting the response you're expecting, try swapping the from and to arguments and/or omitting the straight argument.
thank you!
the solution in the end was change the from and to so in the end it works like that:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=TAGNAME1&to=BRANCH2&straight=true" | jq .
Given a short sha, I want to be able to download the file content of that commit using the github api.
currently i am using following to fetch the content, and it works fine, but how can i download the content for specific sha?
curl -u 'username:password' \
-H 'Accept: application/vnd.github.v3.raw' \
-o output.yaml
-L https://mycompany/api/v3/repos/myorg/myrepo/contents/services/serviceA/helm/manifest.yaml
apparently there is a query parameter ref can be passed to the url
The name of the commit/branch/tag. Default: the repository’s default branch (usually master)
How do I batch archive my repositories? I'd preferably want to be able to sort through them and figure out a way to not archive my active repositories.
I have hundreds of old GitHub repositories in my account since before the GitHub notifications feature, and now I get vulnerability notifications for all of them. Here's what my notifications look, for projects that were last used maybe 6 years ago:
You can use the GitHub API along with two tools to achieve this. I'll be using:
Hub, but you can make direct API calls
jq, but you can use any JSON parser
Here's how:
Fetch a list of all the GitHub repositories in our account, and saving them in a file:
hub api --paginate users/amingilani/repos | jq -r '.[]."full_name"' > repos_names.txt
Go through that file manually, remove any repositories you don't want to archive
Archive all the repositories in the file:
cat repos_names.txt | xargs -I {} -n 1 hub api -X PATCH -F archived=true /repos/{}
Note: since 2020:
gh repo list has been released (with gh 1.7.0 in commit 00cb921, Q1 2021): it does take pagination in account, as it is similar to an alias like:
set -e
repos() {
local owner="${1?}"
shift 1
gh api graphql --paginate -f owner="$owner" "$#" -f query='
query($owner: String!, $per_page: Int = 100, $endCursor: String) {
repositoryOwner(login: $owner) {
repositories(first: $per_page, after: $endCursor, ownerAffiliations: OWNER) {
nodes {
nameWithOwner
description
primaryLanguage { name }
isFork
pushedAt
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
' | jq -r '.data.repositoryOwner.repositories.nodes[] | [.nameWithOwner,.pushedAt,.description,.primaryLanguage.name,.isFork] | #tsv' | sort
}
repos "$#"
gh repo list --no-archived can limit the list to your not-yet-archived repositories
gh repo archive can then, for each element of that list, archive the GitHub repository.
wolfram77 also proposes in the comments:
gh repo list <org> | awk '{NF=1}1' | \
while read in; do gh repo archive -y "$in"; done
Using only gh.
gh repo list --no-archived --limit 144 --visibility public --source --json nameWithOwner --jq ".[].nameWithOwner" > repos_names.txt
Set --limit to the number of repositories you have.
Use vim to delete line which you don't want to archive by pressing dd on the line:
vim repos_names.txt
Run the the following command to arrive them
cat repos_names.txt | while read in; do gh repo archive -y "$in"; done
Clear after:
rm repos_names.txt