🗼

GitHubリポジトリを大量にアーカイブする

に公開

コマンド

使わないリポジトリはアーカイブしておきたい。GitHub の Web UI からでは大量にアーカイブすることが難しいが gh コマンドを使うと簡単にできる。gh には yes オプションがあるので "Please type reponame/username to comfirm" のような問いかけを無視してアーカイブを実行することができる。

gh repo list --source --no-archived --visibility=public --limit 100 --json name,updatedAt --jq '.[] | [.updatedAt, .name] | @tsv' | sort | fzf --multi | awk '{print $2}' | GITHUB_TOKEN=$github_token_repo_archiver xargs -I{} gh repo archive --yes {}

補足

対象リポジトリを絞る条件は次の通り。

  • フォークではない (--source)
  • アーカイブされていない (--no-archived)
  • Public である (--visibility=public)

また、Fine-grained personal access tokensを使う場合、トークンに以下の権限を付与する。

  • Repository access
    • All repositories
  • Permissions
    • Contents: Read and write
    • Admission: Read and write
編集しやすいように改行したコマンド
gh repo list --source --no-archived --visibility=public \
--limit 100 \
--json name,updatedAt \
--jq '.[] | [.updatedAt, .name] | @tsv' \
| sort \
| fzf --multi \
| awk '{print $2}' \
| GITHUB_TOKEN=$github_token_repo_archiver \
xargs -I{} gh repo archive --yes {}
private や fork も対象に含めるコマンド
GITHUB_TOKEN=$github_token_repo_archiver gh repo list --no-archived --limit 200 --json name,updatedAt,visibility --jq '.[] | [.updatedAt, .visibility, .name] | @tsv' | sort | fzf --multi | awk '{print $3}' | GITHUB_TOKEN=$github_token_repo_archiver xargs -I{} gh repo archive --yes {}

Discussion