6

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
GIS Danny
  • 173
  • 1
  • 7

2 Answers2

29

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:

  • Replace abc with your username on GitHub
  • Replace TOKEN with your personal access token for command line. To generate one follow this
  • curl utility can be downloaded from here
  • jq can be installed from here
  • If you are using Windows to run the command, use git-bash (for xargs utility compatibility)

References:

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
0

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
    }

}
akicsike
  • 371
  • 2
  • 8