powershell - 为 github 存储库中的环境创建审阅者

标签 powershell runtime-error github-api environment github-api-v3

下面的 powershell 可以工作并帮助在存储库 variables 下创建环境 test_mont

# 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 = @("knowyrtech")
#$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

enter image description here

请建议使用 API 调用向环境中添加多个审阅者。

更新:尝试了@050的解决方案,但出现了一系列错误:

1. Invoke-WebRequest : {"message":"Invalid request.\n\nInvalid property /reviewers/0/id: `\"knowyrtech\"` is not of type 
`integer`.","documentation_url":"https://docs.github.com/rest/deployments/environments#create-or-update-an-environment"}
At line:37 char:13
+ $response = Invoke-WebRequest -Method PUT -Header $header -Body $revi ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Failed to add required reviewers.

2. Invoke-WebRequest : {"message":"Invalid request.\n\nInvalid property /reviewers: `{\"id\"=>111655092, \"type\"=>\"User\"}` is not of type 
`array`.","documentation_url":"https://docs.github.com/rest/deployments/environments#create-or-update-an-environment"}
At line:36 char:13
+ $response = Invoke-WebRequest -Method PUT -Header $header -Body $revi ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

3. Invoke-WebRequest : {"message":"Not Found","documentation_url":"https://docs.github.com/rest"} At line:33 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

最佳答案

要获得正确的格式,您需要执行以下操作:

# adding multiple users
# $requiredReviewers = @{reviewers = @(@{type="User";id=111655092},@{type="User";id=123456})}

#single user
$requiredReviewers = @{reviewers = @(@{type="User";id=111655092})}

# to add a team: 'type=User' needs to be 'type=Team' and you need to 
# use the teamid
#{type="Team";id=123456}

# Convert the list of reviewers to JSON format
$reviewersJson =  ConvertTo-Json $requiredReviewers

id 是您的用户 ID,因此它将是一个数字。我输入了您在示例中列出的用户名的用户 ID。您可以通过将“usernamehere”替换为您的用户名来获取用户 ID。 https://api.github.com/users/usernamehere

您可以添加的用户/团队数量也有限制“您最多可以列出六个用户或团队作为审阅者”( reference )。

此外,网址需要用引号引起来,但这是错误的:

$uri = https://api.github.com/repos/$owner/$repo/environments/$envName/reviewers

删除网址末尾的“/reviewers”并添加引号:

$uri = "https://api.github.com/repos/$owner/$repo/environments/$envName"

进行这些更改后,我能够成功运行该脚本。

添加团队

本质上与添加用户相同,只不过将 type="User" 更改为 type="Team"。您还需要获取 teamid。您可以通过 api 调用来完成此操作。下面将输出你的 teamid。您的 token 必须具有正确的权限。 (Reference)

$headers = @{Authorization = "token yourtoken"}
# org name
$myorg = "mybank"
# team name
$team = "are-devops"

$content = Invoke-WebRequest -Headers $headers -URI https://api.github.com/orgs/$myorg/teams/$team
$items = $content.Content | ConvertFrom-Json 

Write-Output $items.id

或者,您可以转到团队页面,右键单击头像图像并复制地址。团队 ID 将在链接中。

https://avatars.githubusercontent.com/t/team-id-is-here?s=116&v=4

关于powershell - 为 github 存储库中的环境创建审阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77174472/

相关文章:

c++ - 制作大表时程序停止工作(2'000' 000+)

Powershell DSC xChrome 示例在 AzureVM 上失败

php - 如何在 Windows 10 中卸载 PHP

azure - 在需要 Azure 身份验证的应用程序上调用 RestMethod

github - 格里特和 GitHub。 com.googlesource.gerrit.plugins.github.oauth.OAuthFilter

javascript - 从 Backbone 对 GitHub API 的 Ajax 调用

api - 如何从 Github API 请求仅与 Github 上存储库问题相关的某些字段的值?

使用递归复制文件期间的 powershell 错误检查

Java 成绩计算器

error-handling - Visual Studio错误无法打开我的表单的源代码