有没有办法使 GitHub 中的所有存储库/回购从命令行或 git 私有?
Is there a way to make all your repositories / repos in GitHub Private from commandline or git?
我想遍历我的 GitHub 帐户并将我的所有存储库设置为私有。
我搜索了一下,但不确定如何搜索?
列出用户 abc
的所有 public 个存储库:
curl --request GET https://api.github.com/users/abc/repos
将用户 abc
的名为 xyz
的特定存储库设置为私有:
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
替换为您的个人命令行访问令牌。要生成一个,请遵循 this
curl
实用程序可以从 here 下载
jq
可以从 here 安装
- 如果您使用 Windows 到 运行 命令,请使用 git-bash(为了
xargs
实用程序兼容性)
参考文献:
- GitHub Developer API
- Authentication Methods
- How to use PATCH verb with curl
- JSON processor online
- Using xargs with input from file
POWERSHELL 代码
对于 Windows 用户:它是 Powershell 代码,没有依赖项!
您需要 运行 Powershell 作为管理员!
您需要在 GITHUB 上制作 TOKEN 并粘贴到 Powershell 代码中!
您可以在 GitHub/Settings/Developer settings/Personal 中制作 TOKEN 访问 tokens/Generate 新令牌。
您可以从 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
}
}
我想遍历我的 GitHub 帐户并将我的所有存储库设置为私有。
我搜索了一下,但不确定如何搜索?
列出用户 abc
的所有 public 个存储库:
curl --request GET https://api.github.com/users/abc/repos
将用户 abc
的名为 xyz
的特定存储库设置为私有:
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
替换为您的个人命令行访问令牌。要生成一个,请遵循 this curl
实用程序可以从 here 下载
jq
可以从 here 安装
- 如果您使用 Windows 到 运行 命令,请使用 git-bash(为了
xargs
实用程序兼容性)
参考文献:
- GitHub Developer API
- Authentication Methods
- How to use PATCH verb with curl
- JSON processor online
- Using xargs with input from file
POWERSHELL 代码
对于 Windows 用户:它是 Powershell 代码,没有依赖项!
您需要 运行 Powershell 作为管理员!
您需要在 GITHUB 上制作 TOKEN 并粘贴到 Powershell 代码中!
您可以在 GitHub/Settings/Developer settings/Personal 中制作 TOKEN 访问 tokens/Generate 新令牌。
您可以从 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
}
}