Automatically archive GitHub repos
Automatically archive your repos when they are outdated? I will show you how you can do it π₯
Recently, I had the feeling that my GitHub repos were starting to get a bit too much. I haven't touched many of them for years, let alone actually used them properly. So I wanted to clean up all my repos and archive everything older than x months/years. But I didn't feel like going through 500 repos manually.
This could be done quite easily with the GitHub CLI. To do this, the GitHub CLI must be installed:
The script
As a Windows user, it made sense for me to just set up the script with PowerShell. In principle, the script only uses the GitHub CLI and retrieves all repos from the account (you have to be logged in, of course) and archives them or, if desired, switches them to private
.
$org = "Disane87"
$limit = 400
$retentionDays = 182
(gh repo list $org -L $limit --json name,pushedAt) | ConvertFrom-Json | ForEach-Object {
$repoData = $_;
Write-Host "--------------------------------------------------"
Write-Host "RepoName $($repoData.name)"
$publishedDate = [datetime]::ParseExact($repoData.pushedAt, "M/d/yyyy H:mm:ss", $null)
$timeDiff = New-TimeSpan -Start $publishedDate -End (Get-Date)
if ($timeDiff.TotalDays -gt $retentionDays) {
Write-Host "$($repoData.name) is older than $($retentionDays) days $($timeDiff.TotalDays)"
gh repo edit "$($org)/$($repoData.name)" --visibility private
gh repo archive $repoData.name -y
}
}
In principle, you only need to enter your GitHub user name in the script and change the retention if necessary. By default, this is 182 days.
If you do not want to set your repos to private
, then simply comment this line out with #
gh repo edit "$($org)/$($repoData.name)" --visibility private
Et voila, your GitHub is cleaned up: