Last active
October 11, 2024 18:21
-
-
Save JustinGrote/fdda538bbfde2fa3040439f51e6a065f to your computer and use it in GitHub Desktop.
Rename and setup repos to be just Pull Request sources to an upstream repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-GhAction { | |
param( | |
$RepoName, | |
$Path, | |
$Method = 'GET', | |
$Body, | |
$BaseUri = 'https://api.github.com/repos' | |
) | |
$irmParams = @{ | |
Method = $Method | |
Uri = "$BaseUri/$RepoName/$Path" | |
Authentication = 'Bearer' | |
ContentType = 'application/json' | |
token = (& gh auth token | ConvertTo-SecureString -AsPlainText) | |
Headers = @{ | |
Accept = 'application/vnd.github+json' | |
'X-Github-Api-Version' = '2022-11-28' | |
} | |
Body = $Body | ConvertTo-Json -Depth 10 | |
} | |
Invoke-RestMethod @irmParams | |
} | |
filter Set-AsRepoRepository { | |
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
param ( | |
[ArgumentCompleter({ | |
param($c, $p, $w) | |
& gh repo list -L 1000 --fork --json name | |
| ConvertFrom-Json | |
| Where-Object name -NotLike '_PR-*' | |
| Where-Object name -Like "$w*" | |
| ForEach-Object name | |
})] | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[string]$RepoName, | |
[string]$PRSuffix = '_PR-' | |
) | |
$ErrorActionPreference = 'Stop' | |
if ($RepoName -notmatch '/') { | |
$user = & gh api user --jq '.login' | |
Write-Verbose "Appending user $user to repo name $RepoName" | |
$RepoName = "$user/$RepoName" | |
} | |
$newRepoName = "$PRSuffix$($RepoName.split('/')[-1])" | |
if (-not $PSCmdlet.ShouldProcess($RepoName, "Change to PR Repo named $newRepoName")) { | |
return | |
} | |
if ($RepoName.StartsWith($PRSuffix)) { | |
Write-Error "Repo appears to already have been processed (starts with $PRSuffix)" | |
return | |
} | |
$editArgs = @( | |
'--enable-issues=false' | |
'--enable-wiki=false' | |
'--enable-projects=false' | |
'--enable-discussions=false' | |
'--delete-branch-on-merge' | |
'-d' | |
'**THIS REPO IS ONLY USED FOR PULL REQUESTS TO THE PARENT FORK**. It is not maintained and I will not accept any PRs here.' | |
) | |
& gh repo edit $RepoName @editArgs | |
#Disable GitHub Actions | |
Invoke-GhAction -RepoName $RepoName -Path 'actions/permissions' -Method 'PUT' -Body @{ | |
enabled = $false | |
} | |
& gh repo rename -R $RepoName $newRepoName | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment