如何批量归档我帐户中的所有 GitHub 个存储库?

How can I batch archive all GitHub repositories in my account?

如何批量归档我的存储库?我希望能够对它们进行分类并找出一种不存档我的活动存储库的方法。

自从 GitHub 通知功能出现之前,我的帐户中就有数百个旧的 GitHub 存储库,现在我收到了所有这些存储库的漏洞通知。这是我的通知的样子,对于上次使用的项目可能是 6 年前:

您可以使用 GitHub API 以及两个工具来实现此目的。我将使用:

  • Hub,但您可以直接 API 调用
  • jq,但您可以使用任何 JSON 解析器

方法如下:

  1. 获取我们帐户中所有 GitHub 存储库的列表,并将它们保存在文件中:

    hub api --paginate users/amingilani/repos | jq -r '.[]."full_name"' > repos_names.txt

  2. 手动浏览该文件,删除您不想存档的任何存储库

  3. 存档文件中的所有存储库:

    cat repos_names.txt | xargs -I {} -n 1 hub api -X PATCH -F archived=true /repos/{}

注:自 2020 年起:

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 可以将列表限制为您尚未归档的存储库

  • 然后可以为该列表的每个元素归档 GitHub 存储库。


wolfram77 also proposes in :

gh repo list <org> | awk '{NF=1}1' | \
  while read in; do gh repo archive -y "$in"; done