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

旧街凉风 提交于 2019-12-24 00:45:37

问题


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).


回答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 http://stackoverflow.com/questions/40424377/get-a-list-of-users-from-atlassians-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



回答2:


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:

  • Documented here: https://github.com/replicaJunction/PSJira#downloading
  • Upgrade to PS5 (optional; but required for this method of installation): https://download.microsoft.com/download/6/F/5/6F5FF66C-6775-42B0-86C4-47D41F2DA187/Win7AndW2K8R2-KB3191566-x64.zip
  • Install NuGet Package Manager: Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
  • Install PSJira Module: Install-Module PSJira

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


来源:https://stackoverflow.com/questions/40424377/get-a-list-of-users-from-atlassians-cloud-on-demand-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!