问题
I would like to have a simple, non-interactive way to get a list of forks of a GitHub repo.
For me personally, it has to run on at least Linux.
回答1:
Using GraphQL (GiHub API v4) from a bash script, using cURL:
#!/bin/bash
# Returns a list of all forks of a github repo.
# See the output of "$0 -h" for more details.
set -e
# initial default values
access_token=""
repo_owner=$USER
repo_name=$(basename $(pwd))
res_file=res.json
function print_help() {
echo "Returns a list of all forks of a github repo."
echo
echo "Usage:"
echo " `basename $0` [OPTIONS]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -o, --owner <string> Name of the GitHub user or organization of the original repo"
echo " -r, --repo <string> Name of the GitHub original repo"
echo " -t, --token <string> GitHub personal access token, required for authentication"
echo " To get one, see: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line"
}
# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
arg="$1"
shift
case "${arg}" in
-h|--help)
shift # past argument
print_help
exit 0
;;
-o|--owner)
repo_owner="$1"
shift # past argument
;;
-r|--repo)
repo_name="$1"
shift # past argument
;;
-t|--token)
access_token="$1"
shift # past argument
;;
*) # non-/unknown option
POSITIONAL+=("${arg}") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ -z "$access_token" ]
then
>&2 echo "WARNING: Access token not specified, though it is required!"
print_help
exit 1
fi
curl \
'https://api.github.com/graphql' \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Connection: keep-alive' \
-H 'Origin: altair://-' \
-H "Authorization: Bearer $access_token" \
--data-binary '{"query":"query { repository(owner: \"'$repo_owner'\", name: \"'$repo_name'\") { forks(first:100) { edges { node { nameWithOwner } } } } }","variables":{}}' \
--compressed \
> "$res_file"
cat "$res_file" \
| sed -e 's/nameWithOwner/\nXXX/g' \
| grep XXX \
| awk -e 'BEGIN { FS="\""; } { print $3; }'
You need to create an access token.
Sample invocation of the above script:
git-hub-list-forks -o hoijui -r JavaOSC -t 1234567890abcdef1234567890abcdef
NOTE: GitHub limits the maximum amount of forks to fetch to 100 at a time
来源:https://stackoverflow.com/questions/58753590/how-to-get-a-list-of-all-forks-of-a-github-repo-on-linux