How to query a user name by a display name

Alex Yang May 18, 2021

In our CI pipeline, when the unit test fails, we need to parse and get the commit's author such as "Alex Yang" (alias is alexya) who is also a Jira system user.

When I call Jira REST API to create an issue and set the assignee, it needs the "name" of the user in the Jira system such as "alexya" instead of a full or display name "Alex Yang". (using the name I can create the issue successfully, but displayName will fail)

e.g. 

POST /rest/api/2/issue

 

```

{
"fields": {
"project": {
"key": "AAAA"
},
"summary": "3rd task created with rest API",
"description": "creating of an issue using ids for projects and issue types using the REST API",
"issuetype": {
"name": "Bug"
},
"assignee": {
"displayName": "Alex Yang"
},
"priority": {
"id": "2"
}
}
}

```

It will fail with the error: 

```

{    "errorMessages": [],    "errors": {        "assignee""expected Object containing a 'name' property"    }}

```

 

Furthermore, I tried to use REST API to query the user information, but

the REST API GET /rest/api/2/user?username=alexya can only accept the parameter "username" or "key", can't use the displayName to query the details of a user so that I can't use the displayName to get the user name. e.g.

```

{    

"errorMessages": [        

"Either the 'username' or the 'key' query parameters need to be provided"    

],    "errors": {}}

```

 

Any help will be much appreciated. 

 

1 answer

0 votes
jpaige August 4, 2021

I was looking for this too. Setting the assignee requires the accountId, but the commands to look up a user by displayName and/or emailAddress were removed for GDPR compliance

The only workaround I have found is that JQL queries still accept displayName or emailAddress.

So you can perform a query to match assignee or reporter in existing issues, and then extract the accountId from those results. It's a hack, and it only works for users who have had at least one issue assigned to them before, but it is better than nothing.

Here is a very quick-and-dirty snippit using jira's python api wrapper that gets the job done:

if '"' in username:
    return None
for field in ["assignee", "reporter"]:
    query = f'{field} = "{username}"'
    result = jira.jql(query, limit=1)
    issues = result.get("issues", [])
    for issue in issues:
        user_obj = issue["fields"].get(field, None)
        if user_obj:
            if username in [user_obj["displayName"], user_obj["emailAddress"]]:
                if user_obj["active"]:
                    return user_obj["accountId"]
Bryan Meyerovich September 11, 2022

Pulling the /users api endpoint seems to include display name, can maybe be parsed out of that?

Suggest an answer

Log in or Sign up to answer