Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 685
Amin Shah Gilani Avatar asked Jun 03 '20 10:06

Amin Shah Gilani


People also ask

How do I archive a repository on GitHub?

Nowadays, GitHub supports archiving repositories. You can archive a repository to make it read-only for all users and indicate that it's no longer actively maintained. You can also unarchive repositories that have been archived. ... On GitHub, navigate to the main page of the repository. Under your repository name, click ⚙ Settings.

Can you delete a GitHub repository that is not actively developed?

Just because a repository isn’t actively developed anymore and you don’t want to accept additional contributions doesn’t mean you want to delete it. Now archive repositories on GitHub to make them read-only. Archiving a repository makes it read-only to everyone (including repository owners).

How do I make a GitHub repository read-only?

Now archive repositories on GitHub to make them read-only. Archiving a repository makes it read-only to everyone (including repository owners). This includes editing the repository, issues, pull requests, labels, milestones, projects, wiki, releases, commits, tags, branches, reactions and comments.

What can be edited in a git repository?

This includes editing the repository, issues, pull requests, labels, milestones, projects, wiki, releases, commits, tags, branches, reactions and comments.


Video Answer


2 Answers

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/{}

like image 113
Amin Shah Gilani Avatar answered Nov 15 '22 03:11

Amin Shah Gilani


Note: since 2020:

  • gh repo list has been released (with gh 1.7.0 in commit 00cb921, Q1 2021): it does take pagination in account, as it is similar to an alias like:
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
like image 39
VonC Avatar answered Nov 15 '22 01:11

VonC