Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

[scriptrunner] set assignee

arno
Contributor
February 28, 2025

Hi all

    Some of our colleagues leave the company regularly, and the issue is not transferred to the corresponding colleague before leaving the company, which leads to the failure to deal with some issues in time

     I learned that trial scriptrunner can determine whether users are dimission based on assignee status. Can I set up a regular check when the handler status changes and automatically assign it to someone else

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
arno
Contributor
March 21, 2025

Hi @Fabio Racobaldo _Herzum_ 

    thank you for your help

    I'm really sorry for not replying to you in time. The code you provided has resolved our issue. Thank you once again for your help.

Fabio Racobaldo _Herzum_
Community Champion
March 24, 2025

Hi @arno , 

I'm happy for that. Please mark my answer as accepted ;)

0 votes
Fabio Racobaldo _Herzum_
Community Champion
March 1, 2025

Hi @arno ,

you can setup a script runner job for this purpose (https://docs.adaptavist.com/sr4js/9.5.0/features/jobs). Using jira api you can check status of the assignee and, based on that, define what to do .

In this example issue for specific JQL will be re-assigned to Project Lead if assignee is not active anymore.

 

import java.util.ArrayList;

import java.util.List;

 

import com.atlassian.crowd.embedded.api.Group;

import com.atlassian.jira.bc.issue.search.SearchService;

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.event.type.EventDispatchOption;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.issue.changehistory.ChangeHistoryManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.history.ChangeItemBean;

import com.atlassian.jira.issue.index.IssueIndexingService;

import com.atlassian.jira.issue.link.IssueLinkManager;

import com.atlassian.jira.issue.search.SearchException;

import com.atlassian.jira.issue.search.SearchResults;

import com.atlassian.jira.security.groups.GroupManager;

import com.atlassian.jira.user.ApplicationUser;

import com.atlassian.jira.web.bean.PagerFilter;

import com.atlassian.query.Query;

import com.google.common.base.Strings;

 

String jqlQuery = "YOUR_JQL_HERE";

String scriptResult = "";

 

final SearchService searchService = ComponentAccessor.getComponent(SearchService.class);

final IssueManager issueManager = ComponentAccessor.getIssueManager();

final CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

final IssueIndexingService issueIndexingService = ComponentAccessor.getComponentOfType(IssueIndexingService.class);

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

 

 

//RETRIEVE ISSUES

final List<Issue> issueResult= new ArrayList();

if(!Strings.isNullOrEmpty(jqlQuery)){

final SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);

final Query query = parseResult.getQuery();

try {

final SearchResults<Issue> result = searchService.search(user, query, PagerFilter.getUnlimitedFilter());

final List<Issue> issues = result.getResults();

for(final Issue issue : issues){

issueResult.add(issueManager.getIssueObject(issue.getId()));

}

 

} catch (final SearchException e) {

}

}

int total = 0 ,updated = 0;

for(Issue is : issueResult){

total ++;

List<Group> visibilityGroups = new ArrayList<Group>();

MutableIssue mIssue = (MutableIssue) is;

try {

if(!is.getAssigneeUser().isActive()) {

mIssue.setAssignee(mIssue.getProjectObject().getProjectLead());

Issue issue = issueManager.updateIssue(user, mIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

issueIndexingService.reIndex(issue);

updated++;

}

} catch (Exception e) {

e.printStackTrace();

}

}

long endTime =System.currentTimeMillis();

return "Total : "+total+" - Updated :"+updated;

}

Hope this helps,

Fabio

arno
Contributor
March 7, 2025

Hi @Fabio Racobaldo _Herzum_ 

   thanks for your apply

   I apologize for the delayed response. The code you provided has been very useful to us, and we appreciate your help. However, we would like to further optimize it. May I assign it to the corresponding module leader? I tried using your method to obtain the module information, but it doesn't seem to be supported. Could you please advise on how to optimize it? Thank you.

Fabio Racobaldo _Herzum_
Community Champion
March 7, 2025

@arno what you mean with "Module leader"? Are you talking about component lead?

arno
Contributor
March 7, 2025

Hi @Fabio Racobaldo _Herzum_ 

  yes,I would like to assign the issues of inactive users to the corresponding component leads.

Fabio Racobaldo _Herzum_
Community Champion
March 8, 2025

@arno ,

this script will assign issues assigned to incative user to the first component lead 

{code}

import java.util.ArrayList;
import java.util.List;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.history.ChangeItemBean;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.query.Query;
import com.google.common.base.Strings;
String jqlQuery = "YOUR_JQL_HERE";
String scriptResult = "";
final SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
final IssueManager issueManager = ComponentAccessor.getIssueManager();
final CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
final IssueIndexingService issueIndexingService = ComponentAccessor.getComponentOfType(IssueIndexingService.class);
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
//RETRIEVE ISSUES
final List<Issue> issueResult= new ArrayList();
if(!Strings.isNullOrEmpty(jqlQuery)){
final SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);
final Query query = parseResult.getQuery();
try {
final SearchResults<Issue> result = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
final List<Issue> issues = result.getResults();
for(final Issue issue : issues){
issueResult.add(issueManager.getIssueObject(issue.getId()));
}
} catch (final SearchException e) {}
}
int total = 0 ,updated = 0;
for(Issue is : issueResult){
total ++;
List<Group> visibilityGroups = new ArrayList<Group>();
MutableIssue mIssue = (MutableIssue) is;
try {
if(!is.getAssigneeUser().isActive()) {
if(mIssue.getComponents()!=null && !mIssue.getComponents().isEmpty()) {
mIssue.setAssignee(mIssue.getComponents().iterator().next().getComponentLead());
Issue issue = issueManager.updateIssue(user, mIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
issueIndexingService.reIndex(issue);
updated++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "Total : "+total+" - Updated :"+updated;

{code}

Hope this helps,

Fabio

DEPLOYMENT TYPE
SERVER
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events