下面的powershell可以工作并帮助在repo下创建环境
test_mont
variables
# Define the owner, repository, environment, token, and reviewer variables
$owner = "knowyrtech" # The name of the owner of the repository
$repo = "variables" # The name of the repository
$envName = "test_mont" # The name of the environment
$token = "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" # The authentication token for accessing the GitHub API
$uri = "https://api.github.com/repos/$owner/$repo/environments/$envName"
$header = @{"Authorization" = "token $token"}
Invoke-WebRequest -Method PUT -Header $header -ContentType $contentType -Uri $uri
接下来,下面的 powershell 将审阅者添加到创建的环境中,但失败了。
# Define the owner, repository, environment, token, and reviewer variables
$owner = "knowyrtech" # The name of the owner of the repository
$repo = "variables" # The name of the repository
$envName = "test_mont" # The name of the environment
$token = "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" # The authentication token for accessing the GitHub API
# Define the required reviewers (GitHub usernames) you want to add
$requiredReviewers = @("mybank/are-devops")
# Convert the list of reviewers to JSON format
$reviewersJson = $requiredReviewers | ForEach-Object {
@{
reviewer = $_
}
} | ConvertTo-Json
# GitHub API URL for updating environment protection rules
$uri = https://api.github.com/repos/$owner/$repo/environments/$envName/reviewers
# Set headers with the authentication token
$headers = @{
"Authorization" = "token $token"
"Accept" = "application/vnd.github.v3+json"
}
# Send a POST request to add the required reviewers to the environment
$response = Invoke-WebRequest -Uri $uri -Method PUT -Headers $headers -Body $reviewersJson -ContentType "application/json"
# Check the response
if ($response.StatusCode -eq 200) {
Write-Host "Required reviewers added to the environment."
} else {
Write-Host "Failed to add required reviewers."
}
输出:
Invoke-WebRequest : {"message":"Not Found","documentation_url":https://docs.github.com/rest}
At line:26 char:13
+ $response = Invoke-WebRequest -Uri $uri -Method PUT -Headers $headers ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Failed to add required reviewers.
我也尝试过:
# Convert the list of reviewers to JSON format
$reviewersJson = $requiredReviewers | ForEach-Object {
@{
"reviewers" = [
@{
"type" = "User"
"id" = $_
}
]
}
} | ConvertTo-Json
但是出现错误:
At line:12 char:24 + "reviewers" = [ + ~ Missing type name after '['. At line:16 char:14 + } + ~ Missing '=' operator after key in hash literal. At line:10 char:54 + $reviewersJson = $requiredReviewers | ForEach-Object { + ~ Missing closing '}' in statement block or type definition. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingTypename
最后,我尝试用
"reviewers" = [
替换 "reviewers" : [
,但这也不起作用。
我还尝试将 uri 更改为
$uri = "https://api.github.com/repos/$owner/$repo/environments/$envName/protection-rules"
环境可见且可访问,如下:
https://api.github.com/repos/knowyrtech/variables/environments/test_mont
请建议使用
API
调用将多个审阅者添加到环境中。
我认为 JSON 的语法可能有一点错误。 JSON 的每个对象都必须位于分号之间。试试这个:
$reviewersJson = $requiredReviewers | ForEach-Object {
@{
"reviewers" =
@{
"type" = "User";
"id" = $_
}
}
} | ConvertTo-Json