You want to assign new issues to the user with the least number of tasks to balance the workload across the team.
Solution
This recipe assumes that you already have a project and a workflow associated with it in your Jira installation. If not, go to the Jira documentation to find out how to do this.
Add a SIL post function
Create a draft workflow.
Go to the Create transition.
On the Post Functions tab, select the Add checkbox.
To assign an issue while also maintaining the user load, enter the following code.
string[] prjMembers = projectMembers(project);
string minUser;
number minIssues = -1;
number issuesNumber = -1;
string query = "project = " + project + " AND status in ('Open', 'In Progress', 'Reopened') AND assignee = ";
string jql;
for (string user in prjMembers) {
jql = query + user;
issuesNumber = arraySize(selectIssues(jql));
if ((minIssues == -1) || (issuesNumber < minIssues)) {
minIssues = issuesNumber;
minUser = user;
}
}
assignee = minUser;
Click Add.
It is recommended that you move all SIL™ Post Functions after all other post functions. You can do that by repeatedly clicking "Move Down" until it is at the very bottom of the post functions list.
Test the code
Next, activate your draft workflow by publishing it.
To test the post function, create a new issue. Fill the required fields and click "Create" button. The issue will be assigned to the user with the least number of tasks.
Assign an issue randomly to project users (example)
Recently we encountered one case where we needed the issue to be assigned randomly to project users.
For assigning the issue randomly, you should use the random function and write the following code:
string[] prjMembers = projectMembers(project);
number n = arraySize(prjMembers);
if (n > 0) {
assignee = arrayGetElement(prjMembers, random(n));
} else {
assignee = projectPM(project);
}