github-api - 如何获取 Linux 上 GitHub 存储库的所有分支的列表?

标签 github-api git-fork

我想要一种简单的非交互式方式来获取 GitHub 存储库的分支列表。

就我个人而言,它至少必须在 Linux 上运行。

最佳答案

通过 bash 脚本使用 GraphQL (GiHub API v4),使用 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; }'

您需要 create an access token

上述脚本的调用示例:

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

关于github-api - 如何获取 Linux 上 GitHub 存储库的所有分支的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753590/

相关文章:

git - 使用 NPM 打包 fork Git 存储库的 BUILT 版本的正确方法是什么?

github - 使用 Github GraphQL 获取 secret 要点

Github GraphQL API v.4.0 游标错误

javascript - 如何使用 github API 从网络浏览器编辑 github 文件?

git - 如何在 GitHub 上获取其他人的分支上的分支?

git - 是否可以创建一个项目作为现有项目的前身?某种预 fork 项目?

github-api - 为什么 GitHub API 在拉取请求中返回未知的 mergeable_state?

git - 有没有办法使用 github API v3 获取给定 repo 的最新标签

git - 为 Git fork 更新 go Assets 路径

Github 切换 fork 父级