Amin Shah Gilani
Amin Shah Gilani

Reputation: 9866

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

How do I batch archive my repositories? I'd preferably want to be able to sort through them and figure out a way to not archive my active repositories.

I have hundreds of old GitHub repositories in my account since before the GitHub notifications feature, and now I get vulnerability notifications for all of them. Here's what my notifications look, for projects that were last used maybe 6 years ago:

enter image description here

Upvotes: 11

Views: 5108

Answers (3)

112Legion
112Legion

Reputation: 1237

Using only gh.

gh repo list --no-archived --limit 144 --visibility public --source --json nameWithOwner --jq ".[].nameWithOwner" > repos_names.txt

Set --limit to the number of repositories you have. Use vim to delete line which you don't want to archive by pressing dd on the line:

vim repos_names.txt

Run the the following command to arrive them

cat repos_names.txt | while read in; do gh repo archive -y "$in"; done

Clear after:

rm repos_names.txt

Upvotes: 5

VonC
VonC

Reputation: 1327324

Note: since 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 can limit the list to your not-yet-archived repositories

  • gh repo archive can then, for each element of that list, archive the GitHub repository.


wolfram77 also proposes in the comments:

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

Upvotes: 5

Amin Shah Gilani
Amin Shah Gilani

Reputation: 9866

You can use the GitHub API along with two tools to achieve this. I'll be using:

  • Hub, but you can make direct API calls
  • jq, but you can use any JSON parser

Here's how:

  1. Fetch a list of all the GitHub repositories in our account, and saving them in a file:

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

  2. Go through that file manually, remove any repositories you don't want to archive

  3. Archive all the repositories in the file:

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

Upvotes: 9

Related Questions