Issue cloned via issueFactory not indexed soon enough

Hi,

I’m currently developing an add-on for Jira. This Add-on provides (among other functionality) two post functions:

  1. one to clone an issue to a specified project
  2. one to trigger one of two transitions, based on a JQL: If the issue appears in the JQL, transition a is triggered, if it doesn’t transition b is triggered.

Both of these work fine on their own, but combining them leads to a problem.
Our current situation is as follows: In the “Create” transition of an issue PostFunction 2 is applied, and the issue is transitioned from the initial status to one of two others. This works, if the issue is created manually.
If the issue is created by PostFunction 1 however, it is never found - even if it should be included in the JQLs results (and is when running manually at a later point in time).
Issue creation looks as follows:

MutableIssue clonedIssue = issueFactory.cloneIssue(issue);
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
IssueManager issueManager = ComponentAccessor.getIssueManager();
try {
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
issueIndexingService.reIndex(clonedIssue);
ImportUtils.setIndexIssues(wasIndexing);
issueManager.createIssueObject(user, clonedIssue);
}...

The JQL is constructed as follow:

Query conditionQuery;
try {
    conditionQuery = jqlQueryParser.parseQuery(noApprovalRequiredQuery);
    if (JiraUtil.getIssuesFromJQL(searchService, conditionQuery, jiraAuthenticationContext).stream().anyMatch(it -> it.equals(issue))) {
        return true;
}

Both work fine on their own, but when the JQL is triggered in the create transition (placed after the reindex function) the issue is never included in the JQL-Results, if it was created via the other post function.
I tried Thread.sleep for a second which didn’t change anything - it seems like indexing is started synchronously after the post function was triggered, even if it should start before.

Any help is appreciated,
Leo

That’s because the new issue is not yet indexed - Jira indexes issue asynchronously, in a separate thread, even when you explicitly ask for an issue to be re-indexed.
David

Hi David,
thanks for your answer
That’s what I thought, but even with a seriously long “sleep” (like 10 seconds) the issue is never in the index. And if this was a race condition, why does it always work when creating the issue via GUI?
A lot of things about how the Jira API acts around this don’t really make sense to me.
Leo

I’m not sure you’re cloning the issue properly though. For one thing, you’re trying to reindex the issue before even creating it (with issueManager.createIssueObject).
You should be using the IssueService instead to clone the issue, it’ll take care of everything for you.

Hi David,
sorry for taking so long to get back to this.
I tried to use IssueService, but not only is it a hassle to create field values (especially for custom fields) using the IssueInputParameters instead of the MutableIssue you get with the issueFactory, but that also didn’t help. At least not when using issueService.create - what did work was issueService.clone. So i looked at the difference between those two methods and found a solution:

The clone Function creates a Callable, which then gets submitted to the TaskManager, and is being called asynchronously.
Apparently this resolves the reindexing Issue.
Still, that’s not good API-Design in my opinion - why do the clone and the create method in the same interface even have such a fundamentally different way of working?

Thank you for your help!