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

Issues created in groovy not being reindexed

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 28, 2017

I'm trying to create a script that creates several tasks automatically when a new Epic is created. I've created a post-function groovy script using Script Runner and I'm able to create the issues successfully.

The problem I'm running into is that the new issues don't appear to be reindexed. I believe this is because I'm taking the following approach when creating a new issue in the script.

  • Create a new issue with a summary, priority, reporter, component, and couple custom field values.
  • In order to override the default assignee because of component leads, I have to then update the new issue after it's created with the appropriate assignee. I think this is where I'm running into issues - is there another way to work around for this in groovy?

Like I said, I can create the issues and the assignees are correct. However, the issues appear in the Issue Navigator as Unassigned, even though they have an assignee set correctly. If I open one of the new issues and go back to the Issue Navigator, the Assignee appears correctly in the Issue Navigator. The same is the case if I reindex the project, but I don't want to reindex the whole project every time I create a new epic.

Here is my current script. Any suggestions to help here are greatly appreciated! :)

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.util.ImportUtils

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField newCourseContentField = customFieldManager.getCustomFieldObjectByName("New Course/Content")

if (issue.getIssueType().getName().equals("Epic") && issue.getCustomFieldValue(newCourseContentField).toString().equals("Yes")) {
// create ComponentAccessor manager/service objects so we can create issues
IssueService issueService = ComponentAccessor.getIssueService()
IssueManager issueManager = ComponentAccessor.getIssueManager()
UserManager userManager = ComponentAccessor.getUserManager()

// store issues in a list to reindex all when finished
ArrayList<Issue> issues = new ArrayList<Issue>()

// get info needed for script to create issues in project
long projectId = issue.getProjectId()
String epicKey = issue.getKey()
String epicPriorityId = issue.getPriority().getId()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField epicLinkField = customFieldManager.getCustomFieldObjectByName("Epic Link")
CustomField courseCodeField = customFieldManager.getCustomFieldObjectByName("Course Code")
String courseCodeValue = issue.getCustomFieldValue(courseCodeField).toString()

// issue type ids
String task = "3"

// component ids
long content = 42475
long delivery = 43102
long lms = 42473
long portfolio = 43101
long userExp = 42476

// users who need to be assignees
String user1 = "user1"
String user2 = "user2"
String user3 = "user3"
String user4 = "user4"
String user5 = "user5"
String user6 = "user6"
String user7 = "user7"
String user8 = "user8"

// create content tasks
issues.add(createTask(issueService, projectId, task, "title 1", currentUser, epicPriorityId, content, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user1, issueManager))
issues.add(createTask(issueService, projectId, task, "title 2", currentUser, epicPriorityId, content, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user2, issueManager))
issues.add(createTask(issueService, projectId, task, "title 3", currentUser, epicPriorityId, content, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user1, issueManager))
issues.add(createTask(issueService, projectId, task, "title 4", currentUser, epicPriorityId, content, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user1, issueManager))
issues.add(createTask(issueService, projectId, task, "title 5", currentUser, epicPriorityId, content, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user1, issueManager))

// create delivery tasks
issues.add(createTask(issueService, projectId, task, "title 6", currentUser, epicPriorityId, delivery, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user3, issueManager))
issues.add(createTask(issueService, projectId, task, "title 7", currentUser, epicPriorityId, delivery, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user6, issueManager))
issues.add(createTask(issueService, projectId, task, "title 8", currentUser, epicPriorityId, delivery, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user7, issueManager))
issues.add(createTask(issueService, projectId, task, "title 9", currentUser, epicPriorityId, delivery, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user8, issueManager))

// create LMS tasks
issues.add(createTask(issueService, projectId, task, "title 10", currentUser, epicPriorityId, lms, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user3, issueManager))
issues.add(createTask(issueService, projectId, task, "title 11", currentUser, epicPriorityId, lms, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user3, issueManager))

// create portfolio tasks
issues.add(createTask(issueService, projectId, task, "title 12", currentUser, epicPriorityId, portfolio, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user5, issueManager))
issues.add(createTask(issueService, projectId, task, "title 13", currentUser, epicPriorityId, portfolio, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user5, issueManager))
issues.add(createTask(issueService, projectId, task, "title 14", currentUser, epicPriorityId, portfolio, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user5, issueManager))
issues.add(createTask(issueService, projectId, task, "title 15", currentUser, epicPriorityId, portfolio, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user5, issueManager))
issues.add(createTask(issueService, projectId, task, "title 16", currentUser, epicPriorityId, portfolio, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user5, issueManager))

// create User Experience tasks
issues.add(createTask(issueService, projectId, task, "title 17", currentUser, epicPriorityId, userExp, epicLinkField, epicKey, courseCodeField, courseCodeValue, userManager, user4, issueManager))

// reindex all issues
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
IssueIndexingService.reIndexIssueObjects(issues)
ImportUtils.setIndexIssues(wasIndexing)
}

// method to create a new task using input parameters
Issue createTask(IssueService issueService,
long projectId,
String task,
String summary,
ApplicationUser reporter,
String priority,
long component,
CustomField epicLinkField,
String epicKey,
CustomField courseCodeField,
String courseCodeValue,
UserManager userManager,
String assignee,
IssueManager issueManager) {

// build new issue parameters
IssueInputParameters params = issueService.newIssueInputParameters()
params.setProjectId(projectId)
.setIssueTypeId(task)
.setSummary(summary)
.setReporterId(reporter.getUsername())
.setComponentIds(component)
.setPriorityId(priority)
.addCustomFieldValue(epicLinkField.getId(), epicKey)
.addCustomFieldValue(courseCodeField.getId(), courseCodeValue)

// validate the issue can be created as to not run into any errors
CreateValidationResult validation = issueService.validateCreate(reporter, params)

// create the issue
if (validation.isValid()) {
IssueResult createResult = issueService.create(reporter, validation)

// to override component lead defaults, assign the issue once it's actually created
def newIssue = createResult.getIssue()
newIssue.setAssignee(userManager.getUserByName(assignee))
issueManager.updateIssue(reporter, newIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

return newIssue
}
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Thanos Batagiannis [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.
September 29, 2017

Hi Alex, 

Did you try to change the order of the post function, I think you should put it first. 

Can you please let me know if this does the trick ?

Kind regards, 

Thanos

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2017

Hey, Thanos - thanks for the response. Yes, I did try this. Unfortunately, there was no change.

I think this is because I'm changing the assignee value after each issue is created, so there's no event or post-function that tells the system to reindex the issue I just updated. Still haven't been able to find a way to reindex an issue or set of issues in my groovy script.

TAGS
AUG Leaders

Atlassian Community Events