Как получить список всех форков репозитория GitHub в Linux? - PullRequest
0 голосов
/ 07 ноября 2019

Мне бы хотелось иметь простой неинтерактивный способ получения списка вилок репозитория GitHub.

Лично для меня он должен работать как минимум на Linux.

1 Ответ

0 голосов
/ 07 ноября 2019

Используя GraphQL (GiHub API v4) из bash-скрипта, используя 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; }'

Вам необходимо создать токен доступа .

Пример вызоваприведенный выше сценарий:

git-hub-list-forks -o hoijui -r JavaOSC -t 1234567890abcdef1234567890abcdef

ПРИМЕЧАНИЕ. GitHub ограничивает максимальное количество вилок для выборки до 100 одновременно

...