问题
How can I get the list of all users using Jira Rest Apis.
I have tried:-
/rest/api/latest/user/search?username=
Here I tried with blank username string but it doesn't return full user list. We only get some results when username is of at-least 1 length.
How can I get full users list from Jira rest API's ??
回答1:
I don't think the Rest API has this ability. As far as I know you'll have to choose a different way, for example you could use the Soap API to do so as shown in this question JIRA SOAP API : get the list of users. Another way might be querying the DB directly.
回答2:
You can make a loop in which you will go through all letters and another loop to show result for each user whos name begins with this letter. something like this:
char[] ch = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int j = ch.length;
for (int i = 0; i < ch.length; i++)
{
System.out.print(Character.toString(Character.toUpperCase(ch[i])));
--j;
String projects = invokeGetMethod(auth, BASE_URL+"/rest/api/2/user/search?username="+Character.toString(ch[i]));
System.out.println(projects);
JSONArray projectArray = new JSONArray(projects);
for (int k = 0; k < projectArray.length(); k++) {
JSONObject proj = projectArray.getJSONObject(k);
System.out.println("Key:"+proj.getString("key"));
}
}
private static String invokeGetMethod(String auth, String url) throws AuthenticationException, ClientHandlerException {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json")
.accept("application/json").get(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
return response.getEntity(String.class);
}
回答3:
/rest/api/2/user/search?username=%
works.
回答4:
I sometime use the SOAP getGroup method on jira-users to get the members of jira-users but it seems to miss some users out at around 5K, and you do have to run it as a JIRA admin user. A better way is to access AD directly if you can.
回答5:
This is an example I use in perl. I am sure it can be improved but it works for me. Just change 'user' and 'password' accordingly.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
use Data::Dumper;
use JIRA::REST;
$Data::Dumper::Sortkeys = 1;
my $jira='http://jira:8080';
my $jirarest = JIRA::REST->new($jira, 'user', 'password');
my %users;
for my $c ( 'a' .. 'z', '0'..'9' ,'_') {
say "Scanning '$c'";
my @u = @{$jirarest->GET("/user/search?username=$c&includeInactive=1&maxResults=1000")};
say scalar (keys @u);
@users{ map {$_->{name}} @u } = @u;
}
say Dumper(\%users);
say scalar (keys %users);
The main assumption is that no single character has more than 1000 entries. I will investigate the startAt parameter and see if it's there to support some kind of paging.
回答6:
You can use /rest/api/2/group/member?groupname=
if you're an admin user. Otherwise, this isn't available.
回答7:
I wanted to get all the users in my email domain. Because JIRA searches username, displayName, and emailAddress with the username parameter, all I needed to do was to specify username=mydomain.com as the parameter and I got a full list of users in my email domain. If you want to broaden the search to multiple domains, try username=.com. Hth.
来源:https://stackoverflow.com/questions/11631997/jira-rest-api-for-getting-full-user-list