JIRA SOAP API : get the list of users

前端 未结 3 1676
萌比男神i
萌比男神i 2021-01-06 09:19

I\'m working on a tool in C# that interfaces the JIRA SOAP API. I have read the doc one can find here: http://docs.atlassian.com/software/jira/docs/api/rpc-jira-plugin/lates

相关标签:
3条回答
  • 2021-01-06 09:33

    Ok I must be in better shape today so here is a solution of my problem

        /// <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;
        }
    

    comments are welcome. Obviously, we can use the same way for different role. One just has to be sure that the user used to log in the JIRA api has some admin rights

    0 讨论(0)
  • 2021-01-06 09:40

    Note that when calling getProjectRoleActors from Flex (at least), if your logged-in user is not an administrator, then instead of getting an error or empty response as one might expect, you get no response at all, which is very frustrating until you remember to make the user an admin.

    0 讨论(0)
  • 2021-01-06 09:40

    It's a bit counter-intuitive as there's no general "Get Users" type command, and you need to load a project and role object before you can ask the question.

    Here's the same basic implementation in PHP (since I just wrote it), but keying off the "Users" role rather than "Developers":

    $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);
    
    0 讨论(0)
提交回复
热议问题