Get a list of users from Atlassian's Cloud / On-Demand Service
Asked Answered
P

2

2

I'm trying to pull a list of users from our Atlassian Confluence/Jira instance. However I'm struggling to find good documentation on what REST services are available, and it seems the SOAP services are deprecated.

The following code does get results, but we have over 100 users, and this returns 0.

if(-not ($credentials)) { #put this here so I can rerun the same script in the same IDE session without having to reinput credentials each time
    $credentials = get-credential 'myAtlassianUsername'
}
$tenant = 'myCompany'
invoke-restmethod -Method Get -Uri ('https://{0}.atlassian.net/rest/api/2/groupuserpicker?query=users' -f $tenant) -Credential $credentials | ConvertTo-Json -Depth 5

(The ConvertTo-Json is just to make it simpler to see the expanded result set).

{
    "users":  {
                  "users":  [

                            ],
                  "total":  0,
                  "header":  "Showing 0 of 0 matching users"
              },
    "groups":  {
                   "header":  "Showing 2 of 2 matching groups",
                   "total":  2,
                   "groups":  [
                                  {
                                      "name":  "confluence-users",
                                      "html":  "confluence-\u003cb\u003eusers\u003c/b\u003e",
                                      "labels":  [

                                                 ]
                                  },
                                  {
                                      "name":  "jira-users",
                                      "html":  "jira-\u003cb\u003eusers\u003c/b\u003e",
                                      "labels":  [

                                                 ]
                                  }
                              ]
               }
}

I think the result's trying to give me the URLs for the JIRA and Confluence User APIs; but I can't figure out how those relative URLs map to the root URL (I've tried appending at various positions in the URL, all of which give me a 404 or dead link error).

Pyaemia answered 4/11, 2016 at 13:49 Comment(1)
According to Atlassian (for Confluence): XML-RPC and SOAP APIs deprecated since v5.5 but: XML-RPC won't be removed until there is sufficient coverage by the confluence REST API, we've deprecated the XML-RPC to indicate that new code should be written to use rest api where possible. You are still able to use the RPC while the REST API is being developed incrementally. However, a call to list all users is not available in the SOAP or the REST API. On our side, we developed our own plugin to expose this function.Cheliform
C
1

The query parameter in your following call is a search query on the Name or E-mail address. Reference: https://docs.atlassian.com/jira/REST/cloud/#api/2/groupuserpicker.

You can use maxResults parameter to get more than 50 results.

Sadly, this REST API call will not give you all users in one call.

The only way that I know to do with Jira to get all users is to make one call by starting letter (iterate on each letter):

GET .../rest/api/2/user/search?username=a&maxResults=1000
GET .../rest/api/2/user/search?username=b&maxResults=1000
GET .../rest/api/2/user/search?username=c&maxResults=1000
...

Reference: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findUsers

Sample Code

function Get-AtlassianCloudUsers {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][string]$Tenant
        ,
        [Parameter(Mandatory)][System.Management.Automation.Credential()]$Credential
        ,
        [Parameter(Mandatory=$false)][string]$UserFilter = '%' 
        ,
        [Parameter(Mandatory=$false)][int]$MaxResults = 9999
    )
    process {
        #refer to https://mcmap.net/q/663283/-get-a-list-of-users-from-atlassian-39-s-cloud-on-demand-service for additional notes
        [string]$uri = 'https://{0}.atlassian.net/rest/api/2/user/search?username={1}&maxResults={2}' -f $Tenant, $UserFilter, $MaxResults
        Invoke-RestMethod -Method Get -Uri $Uri -Credential $credential | select -Expand syncRoot | Select-Object name, displayName, active, self 
        #| ConvertTo-Json -Depth 5
    }
}

Get-AtlassianCloudUsers -Tenant 'MyCompany' -credential (Get-Credential 'MyUsername') | ft -AutoSize
Cheliform answered 7/11, 2016 at 13:41 Comment(2)
Perfect; thank-you @mtheriault. NB: using the startAt parameter on the querystring I was able to setup up pagination to avoid worrying about appropriate limits on the maxResults value.Pyaemia
Also, I found an answer here, stating that the wildcard % can be used instead of the initial character of the username to get all results: answers.atlassian.com/questions/9396305/answers/39428244Pyaemia
P
0

As an alternate answer, I recently discovered the PSJira project on GitHub: https://github.com/replicaJunction/PSJira.

This library provides a nice set of wrapper functions around the JIRA services, and seems well documented and maintained.

To achieve the above requirement follow the steps below:

Installing the package:

Configuring PSJira:

  • Set-JiraConfigServer -Server "https://$Tenant.atlassian.net" (assigning $Tenant to your instance's name)

Using:

  • Create a credential for your Jira/Atlassian account: $cred = get-credential $JiraUsername
  • Get list of users: Get-JiraUser -UserName '%' -IncludeInactive -Credential $cred | select Name, DisplayName, Active, EmailAddress
Pyaemia answered 7/2, 2017 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.