【问题标题】:Get a list of users from Atlassian's Cloud / On-Demand Service从 Atlassian 的云/按需服务获取用户列表
【发布时间】:2017-03-18 09:18:51
【问题描述】:

我正在尝试从我们的 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 错误)。

【问题讨论】:

  • 根据 Atlassian(用于 Confluence):自 v5.5 起已弃用 XML-RPC 和 SOAP API,但是:在 confluence REST API 有足够的覆盖范围之前,不会删除 XML-RPC,我们已弃用 XML-RPC 以指示应编写新代码以尽可能使用 rest api。在增量开发 REST API 时,您仍然可以使用 RPC。但是,列出所有用户的调用在 SOAP 或 REST API 中不可用。在我们这边,我们开发了自己的插件来公开这个功能。

标签: jira powershell-4.0 jira-rest-api confluence-rest-api


【解决方案1】:

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

您可以使用 ma​​xResults 参数来获得超过 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

【讨论】:

  • 完美;谢谢@mtheriault。注意:使用查询字符串上的startAt 参数,我能够设置分页以避免担心maxResults 值的适当限制。
  • 另外,我在这里找到了一个答案,指出可以使用通配符%代替用户名的初始字符来获得所有结果:answers.atlassian.com/questions/9396305/answers/39428244
【解决方案2】:

作为替代答案,我最近在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2019-05-14
    • 2012-10-15
    相关资源
    最近更新 更多