Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Get issue id of last created issue of a certain issue type

- November 20, 2020

Hi all,

One of the dev teams that I work with at my job want to utilize round robin assignment of users for a certain issue type. We have 4 listed users under the project role, "Workers", and for every Story issue that gets created in the project, the 4 users (person A [bmorris], person B [kbaker], person C [mkim], and person D [mlubag]) should be assigned in sequential order via round robin. Since the list of users is sorted by user key, the assignment goes from person A -> person D and again in that order infinitely.

In reference to this groovy script from Adaptavist Library (https://library.adaptavist.com/entity/round-robin-assign-issue-to-users-in-a-certain-project-role), I was able to use this script and render its use for our purpose by changing the value for final roleName to 'Workers':

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

// The role you want assignees to set from
final roleName = 'Workers'

// If it is true, the assigned issues will be reassigned
final reassignedIssues = true

def issueManager = ComponentAccessor.issueManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

// Get all of the users associated with the specified project role
def projectRole = projectRoleManager.getProjectRole(roleName)

// Sort the users of the project role using the user key
def users = projectRoleManager.getProjectRoleActors(projectRole, issue.projectObject)
.applicationUsers
.toSorted { it.key }

// There are no users in the specific project role
if (!users) {
log.info ("No users for project role $roleName")
return
}

if (!reassignedIssues && issue.assignee) {
log.info ('The issue is already assigned')
return
}

// Find the latest created issue id that has an assignee
def lastIssueIdWithAssignee = issueManager.getIssueIdsForProject(issue.projectObject.id)
.sort()
.reverse()
.find { issueManager.getIssueObject(it).assignee }

if (!lastIssueIdWithAssignee) {
issue.setAssignee(users.first())
return
}

def lastAssignee = issueManager.getIssueObject(lastIssueIdWithAssignee).assignee
def lastAssigneeIndex = users.indexOf(lastAssignee)
def nextAssignee = users[(lastAssigneeIndex + 1) % users.size()]

issue.setAssignee(nextAssignee)

 

This almost works the way I want it, but there's one problem...In the part of the script that pulls the most recently created issue that has an assignee: 

def lastIssueIdWithAssignee = issueManager.getIssueIdsForProject(issue.projectObject.id)
.sort()
.reverse()
.find { issueManager.getIssueObject(it).assignee }

if (!lastIssueIdWithAssignee) {
issue.setAssignee(users.first())
return
}

This code seems to pull the most recently created issue, regardless of the issue type and whether or not it's assigned to anyone. We only want the round robin script to focus on issues of the issue type Story.

Is there a way in Groovy to get the most recently created issue (of a certain issue type) in a project?

P.S. I have this set as a post-function for the Create transition in a workflow specifically set for the Story issue type. I thought that would be enough, but it still looks at all the issues in the project, and not just stories.

Any help would be highly appreciated.

Thanks all!

-Ian Balas

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
- December 1, 2020

As per repost: https://community.atlassian.com/t5/Adaptavist-questions/Get-issue-id-of-last-created-issue-and-check-issue-type/qaq-p/1545522?utm_source=atlcomm&utm_medium=email&utm_campaign=mentions_answer&utm_content=topic#U1546501

 

I had to make the following changes to the script:

def lastIssueIdWithAssignee = issueManager.getIssueIdsForProject(issue.projectObject.id)
.sort()
.reverse()
.find {issue.getAsString("issuetype") == "Certification" }
TAGS
AUG Leaders

Atlassian Community Events