代码之家  ›  专栏  ›  技术社区  ›  pierroz

Jira SOAP API:获取用户列表

  •  4
  • pierroz  · 技术社区  · 17 年前

    我正在开发一个C中的工具,它与JiraSOAP API接口。我读过这里的医生: http://docs.atlassian.com/software/jira/docs/api/rpc-jira-plugin/latest/index.html

    有人知道我如何获得特定项目的所有可分配用户的列表吗?我还没找到怎么做…

    3 回复  |  直到 12 年前
        1
  •  5
  •   pierroz    17 年前

    好的,我今天的身体一定好一些,所以这里有我的问题的解决办法

        /// <summary>
        /// object interface to the JIRA API
        /// </summary>
        private readonly JiraSoapServiceClient _JiraService;
    
        /// <summary>
        /// authentication token returned by the login method 
        /// that can be used on all other SOAP methods
        /// </summary>
        private readonly string _Token;
    
        /// <summary>
        /// name of the RemoteProjectRole "Developers"
        /// </summary>
        private const string DEVELOPER_ROLE = "Developers";
    
        /// <summary>
        /// id of the RemoteProjectRole "Developers"
        /// </summary>
        private static long? _DeveloperId;
    
        /// <summary>
        /// return the list of the names of all the users who have
        /// the role "Developers" in a project
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public List<string> GetUsersForProject(string project)
        {
            List<string> users = new List<string>();
            try
            {
                // get the RemoteProject
                RemoteProject rp = _JiraService.getProjectByKey(_Token, project);
    
                // get the "Developers" Prject Role
                RemoteProjectRole developerRole = getDeveloperRole();
    
                if (developerRole != null)
                {
                    // we can use this method only if the user logged in is an administrator
                    RemoteRoleActors actors = _JiraService.getProjectRoleActors(_Token, developerRole, rp);
                    foreach (RemoteRoleActor actor in actors.roleActors)
                    {
                        foreach (RemoteUser user in actor.users)
                        {
                            users.Add(user.name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO log the error
    
                users.Clear();
            }
            users.Sort();
            return users;
        }
    
        /// <summary>
        /// return the RemoteProjectRole "Developers"
        /// </summary>
        /// <returns></returns>
        private RemoteProjectRole getDeveloperRole()
        {
            RemoteProjectRole developerRole = null;
            if (_DeveloperId == null)
            {
                // the first time we call this function we don't know the id of this role
                // that's why we are obliged to find it with a foreach on all the project roles
                foreach (RemoteProjectRole role in _JiraService.getProjectRoles(_Token))
                {
                    if (role.name == DEVELOPER_ROLE)
                    {
                        developerRole = role;
                        _DeveloperId = role.id;
                        break;
                    }
                }
            }
            else
            {
                // we have the id so we can get directly the RemoteProjectRole from the JIRA SOAP API
                developerRole = _JiraService.getProjectRole(_Token, (long)_DeveloperId);
            }
    
            return developerRole;
        }
    

    欢迎评论。 显然,对于不同的角色,我们可以使用相同的方法。只需确保用于登录JIRA API的用户具有一些管理权限

        2
  •  2
  •   Thor Harley    16 年前

    请注意,当从flex调用getprojectroleactors时(至少),如果您的登录用户不是管理员,那么您将不会得到错误或空的响应,而是完全没有响应,这是非常令人沮丧的,除非您记住将该用户设置为管理员。

        3
  •  2
  •   Josh Koenig    15 年前

    这有点违反直觉,因为没有一般的“get users”类型命令,您需要加载一个项目和角色对象,然后才能提出问题。

    下面是PHP中相同的基本实现(因为我刚刚编写了它),但是键入“用户”角色而不是“开发人员”:

    $base_url = 'https://yourjira.domain.com';
    $wsdl = $base_url . '/rpc/soap/jirasoapservice-v2?wsdl';
    
    $username = 'username';
    $password = 'password';
    
    $client = new SoapClient($wsdl);
    
    try {
        $token = $client->login($username, $password);
    }
    catch (SoapFault $fault) {
        echo "Error logging in to JIRA";
        print_r($fault);
    }
    
    $code = 'MYPROJECT'
    
    $project = $client->getProjectByKey($token, $code); 
    
    $role = $client->getProjectRole($token, 10000); // 10000 is typically the "users" role
    
    $users = $client->getProjectRoleActors($token, $role, $project);
    
    print_r($users);