1

我试图从我们的Atlassian Confluence/Jira实例中提取用户列表。然而,我正在努力寻找关于哪些REST服务可用的良好文档,并且似乎SOAP服务已被弃用。从Atlassian的Cloud/On-Demand服务获取用户列表

下面的代码不会得到结果,但我们有超过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错误)。

+1

根据Atlassian的(对于合流): XML-RPC和SOAP API的自V5.5弃用,但: XML-RPC不会被删除,直到有足够的覆盖率汇合REST API,我们不赞成使用XML-RPC来指示应尽可能使用新代码来使用rest api。您仍然可以在REST API逐步开发的同时使用RPC。 但是,列出所有用户的调用在SOAP或REST API中不可用。在我们这边,我们开发了自己的插件来公开这个函数。 – mtheriault

回答

1

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

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

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

,我知道有吉拉做才能得到所有用户的唯一途径是通过启动信打一个电话,(遍历每个字母):

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

完美;谢谢你@mtheriault。注意:在查询字符串中使用'startAt'参数我可以设置分页以避免担心'maxResults'值的适当限制。 – JohnLBevan

+1

此外,我在这里找到了答案,指出可以使用通配符'%'代替用户名的首字母以获得所有结果:https://answers.atlassian.com/questions/9396305/answers/39428244 – JohnLBevan

0

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

这个库为JIRA服务提供了一套很好的包装函数,似乎有很好的文档记录和维护。

为了实现上述要求,按照下面的步骤:

安装软件包:

配置PSJira:

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

使用:

  • 您吉拉/ Atlassian的帐户创建一个证书:$cred = get-credential $JiraUsername
  • 用户获取列表:Get-JiraUser -UserName '%' -IncludeInactive -Credential $cred | select Name, DisplayName, Active, EmailAddress
相关问题