从Atlassian的Cloud / On-Demand服务获取用户列表

问题描述 投票:1回答:2

我正在尝试从Atlassian Confluence / Jira实例中提取用户列表。但是,我很难找到有关REST服务可用的良好文档,似乎不推荐使用SOAP服务。

以下代码确实获得了结果,但我们有超过100个用户,并返回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

ConvertTo-Json只是为了让扩展的结果集更简单)。

{
    "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":  [

                                                 ]
                                  }
                              ]
               }
}

我认为结果是试图给我JIRA和Confluence用户API的URL;但我无法弄清楚这些相对URL如何映射到根URL(我尝试在URL中的各个位置附加,所有这些都给我一个404dead link错误)。

jira powershell-v4.0 jira-rest-api confluence-rest-api
2个回答
1
投票

以下调用中的查询参数是名称或电子邮件地址上的搜索查询。参考:https://docs.atlassian.com/jira/REST/cloud/#api/2/groupuserpicker

您可以使用maxResults参数获得超过50个结果。

遗憾的是,这个REST API调用不会在一次调用中为您提供所有用户。

我知道使用Jira来获取所有用户的唯一方法是通过开始写一个字母进行一次调用(迭代每个字母):

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

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

示例代码

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

0
投票

作为另一个答案,我最近在GitHub上发现了PSJira项目:https://github.com/replicaJunction/PSJira

这个库围绕JIRA服务提供了一组很好的包装器函数,并且似乎有很好的文档和维护。

要达到上述要求,请按以下步骤操作:

安装包:

配置PSJira:

  • Set-JiraConfigServer -Server "https://$Tenant.atlassian.net"(将$Tenant分配给您的实例名称)

使用:

  • 为您的Jira / Atlassian帐户创建凭据:$cred = get-credential $JiraUsername
  • 获取用户列表:Get-JiraUser -UserName '%' -IncludeInactive -Credential $cred | select Name, DisplayName, Active, EmailAddress
© www.soinside.com 2019 - 2024. All rights reserved.