Am I assigning the users in the right way?

Shagufta Gurmukhdas November 28, 2016

I am using the following code to assign user in a postfunction depending on a condition:

 

 

for(Issue subtask: issue.getSubTaskObjects()){

    mutableIssue = issueManager.getIssueObject(subtask.getId())

if(mutableIssue.summary=='SomeSummary'){

    ApplicationUser usera=userManager.getUserByName("SomePerson")

mutableIssue.setAssignee(usera);

issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    } 

 

 

After creation, it is assigned to SomePerson. I also have a Kanban board which has a filter of displaying the issues which are only assigned to SomePerson. This issue doesn't show up there. When I go to the issue and manually assign it to some other user and then back to this user (SomePerson), this issue shows up on the kanban board. So I am assuming this is due to some error in assigning the issue programmatically. What is the problem here?

 

 

 

2 answers

5 votes
Jon Mort [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2016

Hi Shagufta,

The following code will assign the issue TP-1 to the user with username admin

def issueKey = 'TP-1'
def result = put('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .body([
        fields:[
                assignee: [name: 'admin']
        ]
])
        .asString()
if (result.status == 204) {
    println 'Successfully assigned to admin'
} else {
    println "Failed to assign to admin: ${result.status}: ${result.body}"
}

Regards, Jon

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2016

Why would you want to do an REST call from an post function? 

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2016

In JIRA Cloud, REST APIs are the only way to interact with JIRA. See the ScriptRunner documentation for more information.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 29, 2016

Thanks Jon for clarification.

1 vote
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2016

Dear @Shagufta Gurmukhdas, try this code:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssignee(usera.directoryUser);
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}
Shagufta Gurmukhdas November 28, 2016

But the setAssignee function is expecting an ApplicationUser object @Vasiliy Zverev

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2016

This code will not work in JIRA Cloud - see the ScriptRunner documentation for more info.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2016

Class depends on JIRA version. Which one do you use?

Shagufta Gurmukhdas November 29, 2016

The latest

Shagufta Gurmukhdas November 29, 2016

I did convert it using ApplicationUsers.from(usera.directoryUser) too, but the original issue is still there

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2016

For me it is better to use this:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssigneeId(usera.getName());
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}
Shagufta Gurmukhdas November 29, 2016

It does not work. The above code just assigns that string with the name to the issue which is not clickable( a hyperlink of user's profile) . I mean, it does not assign it to the user.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2016

Hm, let try this one:

for(Issue subtask: issue.getSubTaskObjects()) {
    mutableIssue = issueManager.getIssueObject(subtask.getId())
    if (mutableIssue.summary == 'SomeSummary') {
        ApplicationUser usera = userManager.getUserByName("SomePerson")
        mutableIssue.setAssigneeId(usera.getKey());
        issueManager.updateIssue(user, mutableIssue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false);
    }
}
Shagufta Gurmukhdas November 29, 2016

This works, but the original issue is still there(the kanban board of this user doesnt display the issue until manually assigned. ) 

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2016

Is this postfunction is used on create transiton?

Shagufta Gurmukhdas November 29, 2016

No, this is on the progress transition

Shagufta Gurmukhdas November 29, 2016

when this issue is transitioned to inprogress, the subtasks should be assigned to appropriate users automatically. And that is happening fine in the code. The only problem is, it isnt shpowing up on the users' kanban boards, but after i again manually assign it to them, it is showing. This is so strange..

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2016

Hm, is there reindex postfunction after this script one?

Shagufta Gurmukhdas November 30, 2016

Yes there is reindex after 2-3 steps. This script is the first step.

Shagufta Gurmukhdas November 30, 2016

scnnn.png

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2016

I have never faced this case. I think that you should create another answer about it.

Suggest an answer

Log in or Sign up to answer