I'd like to loop through my GitHub account and set all my repo's as Private.
I searched a bit and am unsure how to do so?
I'd like to loop through my GitHub account and set all my repo's as Private.
I searched a bit and am unsure how to do so?
To list all the public repositories of the user abc
:
curl --request GET https://api.github.com/users/abc/repos
To set a particular repository named xyz
of the user abc
as private :
curl -u abc:TOKEN --data "{\"private\": \"true\"}" --request PATCH https://api.github.com/repos/abc/xyz
To set all repositories owned by user abc
as private :
curl --request GET https://api.github.com/users/abc/repos | jq --raw-output '.[] .name' | xargs -I % curl -u abc:TOKEN --data "{\"private\": \"true\"}" --request PATCH https://api.github.com/repos/abc/%
Note:
abc
with your username on GitHubTOKEN
with your personal access token for command line. To generate one follow thiscurl
utility can be downloaded from herejq
can be installed from herexargs
utility compatibility)References:
POWERSHELL CODE
For Windows users: it is Powershell code and it has no dependecies!
You need to run Powershell as Administrator!
You need to make TOKEN on GITHUB and paste in the Powershell code!
You can make TOKEN in GitHub/Settings/Developer settings/Personal access tokens/Generate new token.
You can get the ps file from GitHub repo: powershell_switch_repos_visibility_public_private
$Global:GITHUB_USER = ""
$Global:GITHUB_TOKEN = ""
$Global:REPOS_VISIBILITY_TYPE = "public" # which type of repos to convert: public or private
$Global:SET_TO_PRIVATE = "true" # if REPOS_VISIBILITY_TYPE = "public" then value must be "true", if REPOS_VISIBILITY_TYPE = "private" then value must be "false"
#start running
[GitHub]::reposToPrivate()
class GitHub {
static [void] reposToPrivate(){
$jsonStr = ""
$pageNo = 1
Do {
$jsonStr = [GitHub]::getRepos($pageNo) # get json list of repos as json string
$json = [GitHub]::convertJson($jsonStr) # convert json string
[GitHub]::handleJsonForPrivate($json) # loop through repos list and switch private value
$pageNo++
} Until ($jsonStr -eq "[]") # if has no more page with repos than quits the loop
}
static [string] getRepos($pageNo){
$endpoint = "https://api.github.com/user/repos?visibility=$($Global:REPOS_VISIBILITY_TYPE)&page=$($pageNo)"
$resp = [GitHub]::callApi($endpoint, "GET", $false, @{})
return $resp
}
static [void] handleJsonForPrivate($json){
foreach($obj in $json){
Write-Host "endpoint: $($obj.url)"
$endpoint = $obj.url
$resp = [GitHub]::setRepoToPrivate($endpoint)
$respJson = [GitHub]::convertJson($resp)
Write-Host "private = $($respJson.private)"
}
}
static [string] setRepoToPrivate($endpoint){
$postParams = @{"private"="$($Global:SET_TO_PRIVATE)"}
$resp = [GitHub]::callApi($endpoint, "PATCH", $true, $postParams)
return $resp
}
static [string] b64Authentication(){
$AuthBytes = [System.Text.Encoding]::Ascii.GetBytes("$($Global:GITHUB_USER):$Global:GITHUB_TOKEN")
return [Convert]::ToBase64String($AuthBytes)
}
static [string] callApi([string]$endpoint, [string]$methodType, [bool]$hasPostParams, [hashtable]$postParams){
$resp = ""
if($hasPostParams){
$resp = Invoke-WebRequest -Uri $endpoint -Headers @{"Authorization"="Basic $([GitHub]::b64Authentication())"; "Accept"="application/vnd.github.v3+json"} -Method $methodType -Body ($postParams|ConvertTo-Json)
} else {
$resp = Invoke-WebRequest -Uri $endpoint -Headers @{"Authorization"="Basic $([GitHub]::b64Authentication())"; "Accept"="application/vnd.github.v3+json"} -Method $methodType
}
return $resp
}
static [Object] convertJson($jsonStr){
#Write-Host "jsonStr: $($jsonStr)"
$json = $jsonStr | ConvertFrom-JSON
return $json
}
}