Finding the largest repos on my self-hosted Forgejo instances turned out to be a bit more complicated than I expected.
TLDR command:
read -s TOKEN
curl -s \
-H "Authorization: token $TOKEN" \
"https://forgejo.example.com/api/v1/repos/search?limit=100" |
jq -r '
.data
| sort_by(.size) | reverse | .[] | [(.size * 1024 | tostring), .full_name, .html_url] | @tsv' \
| numfmt --field=1 --to=iec-i --suffix=B
First, create a token User settings - Applications with the Read
permission to all repositories.
Forgejo API is pretty extensive, but we need the Pagination section, which shows this command:
curl -v "https://forgejo.example.org/api/v1/repos/search?limit=1"
Combined with the authorization token, it will return the JSON object containing the information about the first repo. If we set it to 100, then 100 repos will be returned.
Now we can use the jq command. First, we select the second property .data.,
then we sort it by the key .size and reverse the results to have the largest
numbers on top of the results. With .[] we return each element of the
resulting array individually. After that, it’s a matter of selecting the keys
we are interested in, i.e., size converted to bytes from kibibyes (KiB,
returned by Forgejo), the name of the repo, and its URL. Finally, we convert it
to TSV format.
To output human-readable numbers, I use numfmt, which converts the first
field (.size) using the IEC standard, and add the suffix B.