The below powershell works and helps create environment test_mont
under repo 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
Next, Below powershell adds reviewers into the created environment which fails.
# 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."
}
OUTPUT:
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.
I also tried:
# Convert the list of reviewers to JSON format
$reviewersJson = $requiredReviewers | ForEach-Object {
@{
"reviewers" = [
@{
"type" = "User"
"id" = $_
}
]
}
} | ConvertTo-Json
But getting an error:
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
Finally, I tried replacing "reviewers" = [
with "reviewers" : [
but that too did not work.
I also tried changing the uri to $uri = "https://api.github.com/repos/$owner/$repo/environments/$envName/protection-rules"
The environment is visible and accessible as below:
https://api.github.com/repos/knowyrtech/variables/environments/test_mont
Kindly suggest adding multiple reviewers to the environment using API
calls.
Update: tried solutions by @050 but getting a series of errors:
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
To get the proper format you need to do something like this:
# 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
The id
is your user id so it'll be a number. I put the user id for the username that you have listed in your example. You can get the user id by replacing "usernamehere" with your username.
https://api.github.com/users/usernamehere
There is also a restriction on the number of users/teams you can add "You can list up to six users or teams as reviewers"(reference).
Also the url needs to be in quotes and was wrong:
$uri = https://api.github.com/repos/$owner/$repo/environments/$envName/reviewers
Remove "/reviewers" at the end of the url and add quotes:
$uri = "https://api.github.com/repos/$owner/$repo/environments/$envName"
After making those changes I was able to successfully run the script.
Adding Teams
It's essentially the same as adding a user, except you change type="User"
to type="Team"
. You will also need to obtain the teamid. You can do this via api call. Bellow will output your teamid. Your token will have to have correct permissions. (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
Alternatively, you could just go to your team page right click the avatar image and copy the address. The team id will be in the link.
https://avatars.githubusercontent.com/t/team-id-is-here?s=116&v=4