Skip to content

Commit

Permalink
Add filtering options to Get-VSTeamPullRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelZ committed Feb 21, 2020
1 parent 0731040 commit 70193a7
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 24 deletions.
95 changes: 95 additions & 0 deletions .docs/Get-VSTeamPullRequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ This command returns all the open pull requests for the Demo team project.

### -------------------------- EXAMPLE 3 --------------------------

```PowerShell
PS C:\> Get-VSTeamPullRequest -ProjectName Demo -All
```

This command returns all pull requests for the Demo team project.

### -------------------------- EXAMPLE 4 --------------------------

```PowerShell
PS C:\> Get-VSTeamPullRequest -ProjectName Demo -TargetBranchRef "refs/heads/mybranch"
```

This command returns all open pull requests for a specific branch

### -------------------------- EXAMPLE 5 --------------------------

```PowerShell
PS C:\> Get-VSTeamPullRequest -Id 123
```
Expand Down Expand Up @@ -64,6 +80,83 @@ Specifies the pull request by ID.
Type: String
Aliases: PullRequestId
Accept pipeline input: true (ByPropertyName)
Parameter Sets: ById
```
### -RepositoryId
The repository ID of the pull request's target branch.
```yaml
Type: Guid
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
### -SourceRepositoryId
If set, search for pull requests whose source branch is in this repository.
```yaml
Type: Guid
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
### -SourceBranchRef
If set, search for pull requests from this branch.
```yaml
Type: String
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
### -TargetBranchRef
If set, search for pull requests into this branch.
```yaml
Type: String
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
### -Status
If set, search for pull requests that are in this state. Defaults to Active if unset. Valid values for this parameter are:
- abandoned
- active
- all
- completed
- notSet
```yaml
Type: String
Parameter Sets: SearchCriteriaWithStatus
```
### -All
```yaml
Type: Switch
Parameter Sets: SearchCriteriaWithAll
```
### -Top
The number of pull requests to retrieve.
```yaml
Type: Int32
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
### -Skip
The number of pull requests to ignore. For example, to retrieve results 101-150, set top to 50 and skip to 100.
```yaml
Type: Int32
Parameter Sets: SearchCriteriaWithStatus, SearchCriteriaWithAll
```
## INPUTS
Expand All @@ -75,3 +168,5 @@ Accept pipeline input: true (ByPropertyName)
## RELATED LINKS
[Show-VSTeamPullRequest](Show-VSTeamPullRequest.md)
[Add-VSTeamPullRequest](Add-VSTeamPullRequest.md)
[Update-VSTeamPullRequest](Update-VSTeamPullRequest.md)
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.4.5

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/243) from [Michel Zehnder](https://github.com/MichelZ) which included the following:

Add additional filtering capabilities to Get-VSTeamPullRequest

## 6.4.4

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/231) from [Dave Neeley](https://github.com/daveneeley) which included the following:
Expand Down
97 changes: 73 additions & 24 deletions Source/Public/Get-VSTeamPullRequest.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,90 @@
function Get-VSTeamPullRequest {
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = "SearchCriteriaWithStatus")]
param (
[Alias('PullRequestId')]
[string] $Id
[Parameter(ParameterSetName = "ById")]
[string] $Id,
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Guid] $RepositoryId,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[Guid] $SourceRepositoryId,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[ValidatePattern('^refs/.*')]
[string] $SourceBranchRef,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[ValidatePattern('^refs/.*')]
[string] $TargetBranchRef,
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[ValidateSet("abandoned", "active", "all", "completed", "notSet")]
[string] $Status,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[switch] $All,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[int] $Top,
[Parameter(ParameterSetName = "SearchCriteriaWithAll")]
[Parameter(ParameterSetName = "SearchCriteriaWithStatus")]
[int] $Skip
)

DynamicParam {
_buildProjectNameDynamicParam -mandatory $false
_buildProjectNameDynamicParam -Mandatory $false
}

Process {
# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]
# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]

try {
if ($ProjectName) {
try {
if ($Id) {
if ($ProjectName) {
$resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -Id $Id
}
else {
} else {
$resp = _callAPI -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -Id $Id
}
}
} else {
$queryString = @{
'searchCriteria.sourceRefName' = $SourceBranchRef
'searchCriteria.sourceRepositoryId' = $SourceRepositoryId
'searchCriteria.targetRefName' = $TargetBranchRef
'searchCriteria.status' = if ($All.IsPresent) { 'all' } else { $Status }
'$top' = $Top
'$skip' = $Skip
}

if ($resp.PSobject.Properties.Name -contains "value") {
$pullRequests = $resp.value
}
else {
$pullRequests = $resp
}
if ($RepositoryId) {
if ($ProjectName) {
$resp = _callAPI -ProjectName $ProjectName -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString
} else {
$resp = _callAPI -Id "$RepositoryId/pullRequests" -Area git -Resource repositories -Version $([VSTeamVersions]::Git) -QueryString $queryString
}
} else {
if ($ProjectName) {
$resp = _callAPI -ProjectName $ProjectName -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -QueryString $queryString
} else {
$resp = _callAPI -Area git -Resource pullRequests -Version $([VSTeamVersions]::Git) -QueryString $queryString
}
}
}

foreach ($respItem in $pullRequests) {
_applyTypesToPullRequests -item $respItem
}
if ($resp.PSobject.Properties.Name -contains "value") {
$pullRequests = $resp.value
}
else {
$pullRequests = $resp
}

Write-Output $pullRequests
}
catch {
_handleException $_
}
foreach ($respItem in $pullRequests) {
_applyTypesToPullRequests -item $respItem
}

Write-Output $pullRequests
} catch {
_handleException $_
}
}
}
99 changes: 99 additions & 0 deletions unit/test/pullrequest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,105 @@ InModuleScope VSTeam {
}
}

It 'Get-VSTeamPullRequest with All' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -All

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*status=all*"
}
}

It 'Get-VSTeamPullRequest with Status abandoned' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -Status abandoned

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*status=abandoned*"
}
}

It 'Get-VSTeamPullRequest with source branch' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "refs/heads/mybranch"

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*searchCriteria.sourceRefName=refs/heads/mybranch*"
}
}

It 'Get-VSTeamPullRequest with target branch' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "refs/heads/mybranch"

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*searchCriteria.targetRefName=refs/heads/mybranch*"
}
}

It 'Get-VSTeamPullRequest with repository id' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -RepositoryId "93BBA613-2729-4158-9217-751E952AB4AF"

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*repositories/93BBA613-2729-4158-9217-751E952AB4AF*"
}
}


It 'Get-VSTeamPullRequest with source repository id' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF"

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*"
}
}

It 'Get-VSTeamPullRequest with top and skip' {
Mock Invoke-RestMethod { return $singleResult }

Get-VSTeamPullRequest -ProjectName Test -SourceRepositoryId "93BBA613-2729-4158-9217-751E952AB4AF" -Top 100 -Skip 200

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {
$Uri -like "*api-version=$([VSTeamVersions]::Git)*" -and
$Uri -like "*Test/_apis/git*" -and
$Uri -like "*searchCriteria.sourceRepositoryId=93BBA613-2729-4158-9217-751E952AB4AF*" -and
$Uri -like "*`$top=100*" -and
$Uri -like "*`$skip=200*"
}
}

It 'Get-VSTeamPullRequest with source branch in wrong format throws' {
Mock Invoke-RestMethod { return $singleResult }

{ Get-VSTeamPullRequest -ProjectName Test -SourceBranchRef "garbage" } | should throw
}

It 'Get-VSTeamPullRequest with target branch in wrong format throws' {
Mock Invoke-RestMethod { return $singleResult }

{ Get-VSTeamPullRequest -ProjectName Test -TargetBranchRef "garbage" } | should throw
}

It 'Get-VSTeamPullRequest No Votes should be Pending Status' {
Mock Invoke-RestMethod { return $singleResult }

Expand Down

0 comments on commit 70193a7

Please sign in to comment.