diff --git a/AzureDevOpsPowerShell/Public/Api/Core/Projects/Get-AzDoProjectProperties.ps1 b/AzureDevOpsPowerShell/Public/Api/Core/Projects/Get-AzDoProjectProperties.ps1 new file mode 100644 index 00000000..7887fb77 --- /dev/null +++ b/AzureDevOpsPowerShell/Public/Api/Core/Projects/Get-AzDoProjectProperties.ps1 @@ -0,0 +1,73 @@ +function Get-AzDoProjectProperties { + <# +.SYNOPSIS +Retrieves properties of specified Azure DevOps projects. + +.DESCRIPTION +The Get-AzDoProjectProperties function retrieves properties of specified Azure DevOps projects within a given collection URI. It supports pipeline input for project names and collection URI. + +.EXAMPLE +$Params = @{ + CollectionUri = "https://dev.azure.com/organization" + ProjectName = "Project1" +} +Get-AzDoProjectProperties @Params + +This example retrieves properties of the project named "Project1" in the specified Azure DevOps organization. + +.EXAMPLE +$Params = @{ + CollectionUri = "https://dev.azure.com/organization" +} +"Project1", "Project2" | Get-AzDoProjectProperties @Params + +This example retrieves properties of multiple projects ("Project1" and "Project2") in the specified Azure DevOps organization. + +.NOTES +This function requires the Validate-CollectionUri and Invoke-AzDoRestMethod helper functions to be defined in the scope. + +.LINK +https://learn.microsoft.com/en-us/rest/api/azure/devops/core/projects/get-project-properties?view=azure-devops-rest-7.1&tabs=HTTP +#> + [CmdletBinding(SupportsShouldProcess)] + param ( + # Collection Uri of the organization + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [ValidateScript({ Validate-CollectionUri -CollectionUri $_ })] + [string] + $CollectionUri, + + # Project where the Repos are contained + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $ProjectName + ) + + begin { + Write-Verbose "Starting function: Get-AzDoProjectProperties" + } + + process { + # somehow it will not work on project name, but will work like this: + $ProjectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).ProjectID + $params = @{ + uri = "$CollectionUri/_apis/projects/$ProjectId/Properties" + version = "7.2-preview.1" + method = 'GET' + } + + if ($PSCmdlet.ShouldProcess($CollectionUri, "Get project $ProjectName properties")) { + $result = (Invoke-AzDoRestMethod @params).value + } else { + Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)" + } + + if ($result) { + [PSCustomObject]@{ + CollectionURI = $CollectionUri + ProjectName = $ProjectName + Properties = $result + } + } + } +} diff --git a/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Get-AzDoEnvironment.ps1 b/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Get-AzDoEnvironment.ps1 index b29b2002..4f646f9e 100644 --- a/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Get-AzDoEnvironment.ps1 +++ b/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Get-AzDoEnvironment.ps1 @@ -4,24 +4,35 @@ function Get-AzDoEnvironment { Creates a Build Validation policy on a branch .DESCRIPTION Creates a Build Validation policy on a branch + .EXAMPLE - $params = @{ + $Params = @{ CollectionUri = "https://dev.azure.com/contoso" - Name = "Policy 1" - RepoName = "Repo 1" - ProjectName = "Project 1" - Id = 1 + ProjectName = "Project 1" } - Set-AzDoBranchPolicyBuildValidation @params + Get-AzDoEnvironment @Params - This example creates a policy with splatting parameters + This example retrieves all environments in the specified project ("Project 1") in Azure DevOps. .EXAMPLE - $env:SYSTEM_ACCESSTOKEN = '***' - New-AzDoPipeline -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -Name "Pipeline 1" -RepoName "Repo 1" -Path "main.yml" - | Set-AzDoBranchPolicyBuildValidation + $Params = @{ + CollectionUri = "https://dev.azure.com/contoso" + ProjectName = "Project 1" + EnvironmentName = "Environment 1" + } + Get-AzDoEnvironment @Params + + This example retrieves details of the environment named "Environment 1" in the specified project ("Project 1") in Azure DevOps. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/contoso" + ProjectName = "Project 1" + EnvironmentName = @("Environment 1", "Environment 2") + } + Get-AzDoEnvironment @Params - This example creates a new Azure Pipeline and sets this pipeline as Build Validation policy on the main branch + This example retrieves details of multiple environments ("Environment 1" and "Environment 2") in the specified project ("Project 1") in Azure DevOps. .OUTPUTS [PSCustomObject]@{ diff --git a/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Remove-AzDoEnvironment.ps1 b/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Remove-AzDoEnvironment.ps1 new file mode 100644 index 00000000..9cd8ccc9 --- /dev/null +++ b/AzureDevOpsPowerShell/Public/Api/Environments/Environments/Remove-AzDoEnvironment.ps1 @@ -0,0 +1,96 @@ +function Remove-AzDoEnvironment { + <# + .SYNOPSIS + Remove Environment from Azure DevOps. + + .DESCRIPTION + This function removes an environment from Azure DevOps. + + .EXAMPLE + Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1" + + This example removes the environment named "Environment 1" from the specified project in Azure DevOps. + + .EXAMPLE + Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1", "Environment 2" + + This example removes multiple environments ("Environment 1" and "Environment 2") from the specified project in Azure DevOps. + + .EXAMPLE + Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName 1 + + This example removes the environment with the ID 1 from the specified project in Azure DevOps. + + .EXAMPLE + Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName 1, 2 + + This example removes multiple environments with IDs 1 and 2 from the specified project in Azure DevOps. + + .EXAMPLE + Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1", 2 + + This example removes a mix of environments by name ("Environment 1") and ID (2) from the specified project in Azure DevOps. + + .LINK + https://learn.microsoft.com/en-us/rest/api/azure/devops/environments/environments/delete?view=azure-devops-rest-7.2 + #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] + [OutputType([System.Collections.ArrayList])] + param ( + # Collection URI. e.g. https://dev.azure.com/contoso. + # Azure Pipelines has a predefined variable for this. + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [ValidateScript({ Validate-CollectionUri -CollectionUri $_ })] + [string] + $CollectionUri, + + # Name of the project. + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $ProjectName, + + # Id or name of the environment. + # this is a string because a name can be used as well and will do a Get-AzDoEnvironment to get the ID. + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string[]] + $EnvironmentName + ) + + begin { + $result = @() + Write-Verbose "Starting function: Remove-AzDoEnvironment" + } + + Process { + + foreach ($Environment in $EnvironmentName) { + if ($Environment -ne [int]) { + $EnvironmentId = (Get-AzDoEnvironment -CollectionUri $CollectionUri -ProjectName $ProjectName -EnvironmentName $Environment).EnvironmentId + + } else { + $EnvironmentId = $Environment + } + + $params = @{ + uri = "$CollectionUri/$ProjectName/_apis/pipelines/environments/$EnvironmentId" + version = "7.2-preview.1" + method = 'DELETE' + } + + if ($PSCmdlet.ShouldProcess($CollectionUri, "Remove environment id: $($PSStyle.Bold)$EnvironmentId$($PSStyle.Reset)")) { + $result += Invoke-AzDoRestMethod @params + } else { + Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)" + } + } + if ($result) { + $result | ForEach-Object { + [PSCustomObject]@{ + CollectionUri = $CollectionUri + ProjectName = $ProjectName + Response = $_ + } + } + } + } +} diff --git a/AzureDevOpsPowerShell/Public/Api/Git/PolicyConfigurations/Get-AzDoPipelineBranchControl.ps1 b/AzureDevOpsPowerShell/Public/Api/Git/PolicyConfigurations/Get-AzDoPipelineBranchControl.ps1 new file mode 100644 index 00000000..c4d36047 --- /dev/null +++ b/AzureDevOpsPowerShell/Public/Api/Git/PolicyConfigurations/Get-AzDoPipelineBranchControl.ps1 @@ -0,0 +1,125 @@ +function Get-AzDoPipelineBranchControl { + <# +.SYNOPSIS +Retrieves branch policy configurations of specified Azure DevOps projects. + +.DESCRIPTION +The Get-AzDoPipelineBranchControl function retrieves branch policy configurations of specified Azure DevOps projects within a given collection URI. +It supports pipeline input for project names and collection URI. + +.EXAMPLE +$Params = @{ + CollectionUri = "https://dev.azure.com/organization" + ProjectName = "Project1" +} +Get-AzDoPipelineBranchControl @Params + +This example retrieves branch policy configurations for the project named "Project1" in the specified Azure DevOps organization. + +.EXAMPLE +$Params = @{ + CollectionUri = "https://dev.azure.com/organization" +} +"Project1", "Project2" | Get-AzDoPipelineBranchControl @Params + +This example retrieves branch policy configurations for multiple projects ("Project1" and "Project2") in the specified Azure DevOps organization. + +.EXAMPLE +$Params = @{ + CollectionUri = "https://dev.azure.com/organization" + ProjectName = "Project1" + RepositoryId = "12345" + RefName = "refs/heads/main" + PolicyType = "Build" +} +Get-AzDoPipelineBranchControl @Params + +This example retrieves branch policy configurations for the "main" branch in the repository with ID "12345" in the project "Project1" in the specified Azure DevOps organization, filtered by the "Build" policy type. + +.NOTES +This function requires the Validate-CollectionUri and Invoke-AzDoRestMethod helper functions to be defined in the scope. + +.LINK +https://learn.microsoft.com/en-us/rest/api/azure/devops/git/policy-configurations/list?view=azure-devops-rest-5.0#policyconfiguration +#> + [CmdletBinding(SupportsShouldProcess)] + param ( + # Collection URI of the organization + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [ValidateScript({ Validate-CollectionUri -CollectionUri $_ })] + [string] + $CollectionUri, + + # Project where the policies are defined + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $ProjectName, + + # Repository ID (optional) + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $RepositoryId, + + # Ref name (branch) to filter policies (optional) + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $RefName, + + # Policy type to filter results (optional) + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $PolicyType + ) + + begin { + Write-Verbose "Starting function: Get-AzDoPipelineBranchControl" + } + + process { + # somehow it will not work on project name, but will work like this: + $ProjectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).ProjectID + + $queryParams = @() + if ($RepositoryId) { + $queryParams += "repositoryId=$RepositoryId" + } + if ($RefName) { + $queryParams += "refName=$RefName" + } + if ($PolicyType) { + $queryParams += "policyType=$PolicyType" + } + $queryString = $queryParams -join "&" + + $params = @{ + Uri = "$CollectionUri/$ProjectId/_apis/git/policy/configurations" + Version = "7.1" + Method = 'GET' + } + if (-not([string]::IsNullOrEmpty(($queryString)))) { + $params.QueryParameters = $queryString + } + if ($PSCmdlet.ShouldProcess($CollectionUri, "Get branch policies for project $ProjectName")) { + $result = (Invoke-AzDoRestMethod @params).value + } else { + Write-Verbose "Calling Invoke-AzDoRestMethod with $($params | ConvertTo-Json -Depth 10)" + } + if ($result) { + + $result | ForEach-Object { + [PSCustomObject]@{ + CollectionUri = $CollectionUri + ProjectName = $ProjectName + IsEnabled = $_.isEnabled + IsBlocking = $_.isBlocking + Settings = $_.settings + PolicyId = $_.id + PolicyUrl = $_.url + PolicyType = $_.PolicyTypeRef + CreatedBy = $_.createdBy + CreatedDate = $_.createdDate + } + } + } + } +} diff --git a/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Get-AzDoPullRequest.ps1 b/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Get-AzDoPullRequest.ps1 new file mode 100644 index 00000000..208da48e --- /dev/null +++ b/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Get-AzDoPullRequest.ps1 @@ -0,0 +1,183 @@ +function Get-AzDoPullRequest { + <# +.SYNOPSIS + Retrieves pull request information from Azure DevOps. + +.DESCRIPTION + This function fetches pull request details using Azure DevOps REST API. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + } + Get-AzDoPullRequest @Params + + This example retrieves all pull requests for the specified project. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "RepositoryName" + } + Get-AzDoPullRequest @Params + + This example retrieves all pull requests for the specified repository in the project. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "RepositoryName" + Query = "searchCriteria.status=completed" + } + Get-AzDoPullRequest @Params + + This example retrieves all completed pull requests for the specified repository in the project. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "RepositoryName" + PullRequestId = "6789" + } + Get-AzDoPullRequest @Params + + This example retrieves details of a specific pull request by its ID for the specified repository in the project. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + PullRequestId = "6789" + } + Get-AzDoPullRequest @Params + + This example retrieves details of a specific pull request by its ID for the specified project. + +.OUTPUTS + PSCustomObject with pull request details. + +.NOTES + Requires authentication with Azure DevOps REST API. + +.LINK + https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request?view=azure-devops-rest-7.2 + +.LINK + https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request-by-id?view=azure-devops-rest-7.2 + +.LINK + https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests?view=azure-devops-rest-7.2 + +.LINK + https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests-by-project?view=azure-devops-rest-7.2 +#> + [CmdletBinding(DefaultParameterSetName = "AllProjectPullRequests", SupportsShouldProcess)] + param ( + # The base URL of the Azure DevOps organization (e.g., https://dev.azure.com/my-org) + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "RepoSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "ProjectSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllProjectPullRequests")] + [ValidateScript({ Validate-CollectionUri -CollectionUri $_ })] + [string] + $CollectionUri, + + # The name of the Azure DevOps project. + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "RepoSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "ProjectSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")] + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllProjectPullRequests")] + [string] + $ProjectName, + + # The name of the repository (optional for project-wide pull request queries). + [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "RepoSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "AllRepoPullRequests")] + [string] + $RepoName, + + # The ID of a specific pull request (optional for listing all pull requests). + [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "RepoSpecificPullRequest")] + [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "ProjectSpecificPullRequest")] + [string] + $PullRequestId, + + # A query string to filter the results (optional). + [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")] + [string] + $Query + ) + + begin { + $result = @() + Write-Verbose "Starting function: Get-AzDoPullRequest" + } + + process { + $uriBase = "$CollectionUri/$ProjectName/_apis/git" + $apiVersion = "7.2-preview.2" + $uri = "" + + switch ($PSCmdlet.ParameterSetName) { + "RepoSpecificPullRequest" { + $uri = "$uriBase/repositories/$RepoName/pullrequests/$PullRequestId" + } + "ProjectSpecificPullRequest" { + $uri = "$uriBase/pullrequests/$PullRequestId" + } + "AllRepoPullRequests" { + $uri = "$uriBase/repositories/$RepoName/pullrequests" + } + "AllProjectPullRequests" { + $uri = "$uriBase/pullrequests" + } + } + + if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Pull Request from: $ProjectName")) { + Write-Verbose "Calling API: $uri" + + $InvokeAzDoRestMethodSplat = @{ + Uri = $uri + Method = "GET" + Version = $apiVersion + } + if (-not([string]::IsNullOrEmpty($Query))) { + $InvokeAzDoRestMethodSplat.QueryParameters = $Query + } + $response = Invoke-AzDoRestMethod @InvokeAzDoRestMethodSplat + if ($response.value) { + $result += $response.value + } else { + $result += $response + } + if ($result) { + $result | ForEach-Object { + [PSCustomObject]@{ + CollectionUri = $CollectionUri + ProjectName = $ProjectName + RepoName = $RepoName + PullRequestTitle = $_.Title + PullRequestDescription = $_.Description + CreatedDate = $_.CreatedDate + PullRequestStatus = $_.Status + SourceRefName = $_.sourceRefName + TargetRefName = $_.targetRefName + Reviewers = $_.reviewers | ForEach-Object { + [PSCustomObject]@{ + Id = $_.id + Name = $_.name + Vote = $_.vote + IsRequired = $_.isRequired + } + } + PullRequestUrl = $_.url + } + } + } + } + } +} diff --git a/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Set-AzDoPullRequest.ps1 b/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Set-AzDoPullRequest.ps1 new file mode 100644 index 00000000..7f80dd70 --- /dev/null +++ b/AzureDevOpsPowerShell/Public/Api/Git/PullRequests/Set-AzDoPullRequest.ps1 @@ -0,0 +1,218 @@ +function Set-AzDoPullRequest { + <# +.SYNOPSIS + Updates pull request details in Azure DevOps. + +.DESCRIPTION + This function updates pull request details using Azure DevOps REST API. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "Repo" + PullRequestId = "123" + Title = "Updated PR Title" + Description = "New description" + } + Set-AzDoPullRequest @Params + + This example updates only the title and description of a pull request. + +.EXAMPLE + $completionOptions = @{ + deleteSourceBranch = $true + mergeCommitMessage = "Auto-merging PR" + } + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "Repo" + PullRequestId = "123" + AutoCompleteSetBy = "user-id-123" + CompletionOptions = $completionOptions + } + Set-AzDoPullRequest @Params + + This example sets auto-complete for a pull request with completion options. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "Repo" + PullRequestId = "123" + MergeOptions = "squash" + Status = "completed" + } + Set-AzDoPullRequest @Params + + This example changes the merge strategy to squash and completes the pull request. + +.EXAMPLE + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "Repo" + PullRequestId = "123" + TargetRefName = "develop" + } + Set-AzDoPullRequest @Params + + This example retargets a pull request to a different branch. + +.EXAMPLE + $completionOptions = @{ + transitionWorkItems = $true + deleteSourceBranch = $true + } + $Params = @{ + CollectionUri = "https://dev.azure.com/my-org" + ProjectName = "MyProject" + RepositoryName = "Repo" + PullRequestId = "123" + AutoCompleteSetBy = "user-id-123" + CompletionOptions = $completionOptions + } + Set-AzDoPullRequest @Params + + This example sets auto-complete for a pull request with a transition work item option. + +.OUTPUTS + PSCustomObject with pull request details. + +.NOTES + Requires authentication with Azure DevOps REST API. +#> + [CmdletBinding(SupportsShouldProcess)] + param ( + # The base URL of the Azure DevOps organization (e.g., https://dev.azure.com/my-org). + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [ValidateScript({ Validate-CollectionUri -CollectionUri $_ })] + [string] + $CollectionUri, + + # The name of the Azure DevOps project. + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $ProjectName, + + # The name of the repository (optional for project-wide pull request queries). + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $RepoName, + + # The ID of a specific pull request (optional for listing all pull requests). + [Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $PullRequestId, + + # The new status of the pull request. Allowed values: active, abandoned, completed. + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [ValidateSet('active', 'abandoned', 'completed', 'all')] + [string] + $Status, + + # The new title for the pull request (max 256 characters). + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [ValidateLength(0, 256)] + [string] + $Title, + + # The new description for the pull request (max 4000 characters). + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [ValidateLength(0, 4000)] + [string] + $Description, + + # Specifies how the PR should be completed. Example: @{ deleteSourceBranch = $true; mergeCommitMessage = "Merged PR" } + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [ValidateSet('setAutoComplete', 'setAutoCompleteSetBy')] + [string] + $CompletionOptions, + + # Specifies how the PR should be merged. Allowed values: noMerge, squash, rebase, rebaseMerge. + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [ValidateSet('noMerge', 'squash', 'rebase', 'rebaseMerge')] + [string] + $MergeOptions, + + # The Azure DevOps user ID who sets the PR to auto-complete. + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $AutoCompleteSetBy, + + # The new target branch for the pull request. Example: "main" (automatically prefixed with "refs/heads/"). + # Retargeting a pull request means changing the destination branch where the pull request will be merged. + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] + [string] + $TargetRefName + ) + + begin { + $result = @() + Write-Verbose "Starting function: Set-AzDoPullRequest" + } + + process { + $uriBase = "$CollectionUri/$ProjectName/_apis/git" + $apiVersion = "7.2-preview.2" + + $body = @{} + + if ($Status) { $body["status"] = $Status } + if ($Title) { $body["title"] = $Title } + if ($Description) { $body["description"] = $Description } + if ($MergeOptions) { $body["mergeOptions"] = $MergeOptions } + if ($TargetRefName) { $body["targetRefName"] = "refs/heads/$TargetRefName" } + + if ($CompletionOptions -and $CompletionOptions.Count -gt 0) { + $body["completionOptions"] = $CompletionOptions + } + + if ($AutoCompleteSetBy) { + $body["autoCompleteSetBy"] = @{ id = $AutoCompleteSetBy } + } + + if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Pull Request from: $ProjectName")) { + Write-Verbose "Calling API: $uri" + + $InvokeAzDoRestMethodSplat = @{ + Uri = "$uriBase/repositories/$RepositoryName/pullrequests/$PullRequestId" + Method = "PATCH" + Version = $apiVersion + Body = $Body + } + $response = Invoke-AzDoRestMethod @InvokeAzDoRestMethodSplat + if ($response.value) { + $result += $response.value + } else { + $result += $response + } + if ($result) { + $result | ForEach-Object { + [PSCustomObject]@{ + CollectionUri = $CollectionUri + ProjectName = $ProjectName + RepoName = $RepoName + PullRequestTitle = $_.Title + PullRequestDescription = $_.Description + CreatedDate = $_.CreatedDate + PullRequestStatus = $_.Status + SourceRefName = $_.sourceRefName + TargetRefName = $_.targetRefName + Reviewers = $_.reviewers | ForEach-Object { + [PSCustomObject]@{ + Id = $_.id + Name = $_.name + Vote = $_.vote + IsRequired = $_.isRequired + } + } + PullRequestUrl = $_.url + } + } + } + } + } +}