有没有办法从命令行或 git 使 GitHub 中的所有存储库/存储库私有?



我想遍历我的 GitHub 帐户并将我所有的存储库设置为私有。

我搜索了一下,不确定该怎么做?

列出用户的所有公共仓库abc

curl --request GET https://api.github.com/users/abc/repos

要将名为用户abcxyz的特定存储库设置为私有:

curl -u abc:TOKEN --data "{"private": "true"}" --request PATCH https://api.github.com/repos/abc/xyz

要将用户abc拥有的所有存储库设置为私有存储库:

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/%

注意:

  • abc替换为您在 GitHub 上的用户名
  • TOKEN替换为命令行的个人访问令牌。要生成一个,请遵循此
  • 可以从此处下载curl实用程序
  • jq可以从这里安装
  • 如果您使用 Windows 运行该命令,请使用 git-bash(以实现xargs实用程序兼容性(

引用:

  • GitHub 开发者接口
  • 身份验证方法
  • 如何将 PATCH 动词与卷曲一起使用
  • JSON 处理器在线
  • 将 xarg 与来自文件的输入一起使用

POWERSHELL 代码

对于Windows用户:它是Powershell代码,它没有依赖关系!

您需要以管理员身份运行Powershell!

你需要在GITHUB上制作TOKEN并粘贴Powershell代码!

您可以在 GitHub/设置/开发人员设置/个人访问令牌/生成新令牌中制作令牌。

您可以从 GitHub 存储库获取 ps 文件: 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
}
}

最新更新