Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-JiraUser -Filter parameter added #306

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions JiraPS/Public/Get-JiraUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ function Get-JiraUser {
[String[]]
$UserName,

[Parameter( Position = 0, Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'ByFilter' )]
[ValidateNotNullOrEmpty()]
[Alias('Search')]
[String[]]
$Filter,

[Parameter( Position = 0, Mandatory, ParameterSetName = 'ByInputObject' )]
[Object[]] $InputObject,

[Parameter( ParameterSetName = 'ByFilter' )]
[Switch]
$IncludeInactive,

[Parameter( ParameterSetName = 'ByUserName' )]
[Parameter( ParameterSetName = 'ByFilter' )]
[ValidateRange(1, 1000)]
[UInt32]
$MaxResults = 50,

[Parameter( ParameterSetName = 'ByUserName' )]
[Parameter( ParameterSetName = 'ByFilter' )]
[ValidateNotNullOrEmpty()]
[UInt64]
$Skip = 0,
Expand All @@ -36,6 +43,7 @@ function Get-JiraUser {
$server = Get-JiraConfigServer -ErrorAction Stop

$selfResourceUri = "$server/rest/api/latest/myself"
$getResourceUri = "$server/rest/api/latest/user?username={0}&expand=groups"
$searchResourceUri = "$server/rest/api/latest/user/search?username={0}"

if ($IncludeInactive) {
Expand All @@ -57,6 +65,7 @@ function Get-JiraUser {
switch ($PsCmdlet.ParameterSetName) {
'ByInputObject' { $UserName = $InputObject.Name; $ParameterSetName = 'ByUserName' }
'ByUserName' { $ParameterSetName = 'ByUserName' }
'ByFilter' { $ParameterSetName = 'ByFilter' }
'Self' { $ParameterSetName = 'Self' }
}

Expand All @@ -80,12 +89,38 @@ function Get-JiraUser {
$PsCmdlet.ParameterSetName = "ByUserName"
}
"ByUserName" {
$resourceURi = $searchResourceUri
$resourceURi = $getResourceUri

foreach ($user in $UserName) {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$user]"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$user [$user]"

$parameter = @{
URI = $resourceURi -f $user
Method = "GET"
Credential = $Credential
}
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
if ($result = Invoke-JiraMethod @parameter) {
Write-Output (ConvertTo-JiraUser -InputObject $result)
}
else {
$errorMessage = @{
Category = "ObjectNotFound"
CategoryActivity = "Getting user"
Message = "No results when getting user $user"
}
Write-Error @errorMessage
}
}
}
"ByFilter" {
$resourceURi = $searchResourceUri

foreach ($user in $Filter) {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$user]"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$user [$user]"

$parameter = @{
URI = $resourceURi -f $user
Method = "GET"
Expand Down
6 changes: 3 additions & 3 deletions Tests/Get-JiraUser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ Describe "Get-JiraUser" {
}

It "Allow it search for multiple users" {
Get-JiraUser -UserName "%"
Get-JiraUser -Filter "%"

Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter {
$URI -like "$jiraServer/rest/api/*/user/search?*username=%25*"
}
}

It "Allows to change the max number of users to be returned" {
Get-JiraUser -UserName "%" -MaxResults 100
Get-JiraUser -Filter "%" -MaxResults 100

Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter {
$URI -like "$jiraServer/rest/api/*/user/search?*maxResults=100*"
}
}

It "Can skip a certain amount of results" {
Get-JiraUser -UserName "%" -Skip 10
Get-JiraUser -Filter "%" -Skip 10

Assert-MockCalled -CommandName Invoke-JiraMethod -Exactly 1 -Scope It -ParameterFilter {
$URI -like "$jiraServer/rest/api/*/user/search?*startAt=10*"
Expand Down
42 changes: 36 additions & 6 deletions docs/en-US/commands/Get-JiraUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ Get-JiraUser [-Credential <PSCredential>] [<CommonParameters>]
### ByUserName

```powershell
Get-JiraUser [-UserName] <String[]> [-IncludeInactive] [[-MaxResults] <UInt32>] [[-Skip] <UInt64>] [-Credential <PSCredential>] [<CommonParameters>]
Get-JiraUser [-UserName] <String[]> [-Credential <PSCredential>] [<CommonParameters>]
```

### ByFilter

```powershell
Get-JiraUser [-Filter] <String[]> [-IncludeInactive] [[-MaxResults] <UInt32>] [[-Skip] <UInt64>] [-Credential <PSCredential>] [<CommonParameters>]
```

### ByInputObject
Expand All @@ -49,13 +55,21 @@ Returns information about the user user1

### EXAMPLE 2

```powershell
Get-JiraUser -Filter 'John'
```

Returns information about all user(s) whose username, display name or email address matches 'John'. The string is matched to the starting letters of any word in the searched fields. For example, 'and' matches to the username 'Andrei' but not 'Alexander'

### EXAMPLE 3

```powershell
Get-ADUser -filter "Name -like 'John*Smith'" | Select-Object -ExpandProperty samAccountName | Get-JiraUser -Credential $cred
```

This example searches Active Directory for "John*Smith", then obtains their JIRA user accounts.

### EXAMPLE 3
### EXAMPLE 4

```powershell
Get-JiraUser -Credential $cred
Expand All @@ -67,7 +81,7 @@ This example returns the JIRA user that is executing the command.

### -UserName

Name of the user to search for.
Name of the user to return information for.

```yaml
Type: String[]
Expand All @@ -81,6 +95,22 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -Filter

Name of the user to search for.

```yaml
Type: String[]
Parameter Sets: ByFilter
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -InputObject

User Object of the user.
Expand All @@ -103,7 +133,7 @@ Include inactive users in the search

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Parameter Sets: ByFilter
Aliases:

Required: False
Expand All @@ -121,7 +151,7 @@ Maximum number of user to be returned.

```yaml
Type: UInt32
Parameter Sets: ByUserName
Parameter Sets: ByFilter
Aliases:

Required: False
Expand All @@ -139,7 +169,7 @@ Defaults to 0.

```yaml
Type: UInt64
Parameter Sets: ByUserName
Parameter Sets: ByFilter
Aliases:

Required: False
Expand Down