Fix Version modified by Script Runner do not impact the version itself

Jean-Christophe Gay February 28, 2019

Hi,

some users would like to automaticaly add a fix version to stories that are created in an epic with a fix version. I did the script below as a custom listner on the 'Issue Created' event :

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItemImpl;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.project.version.VersionManager;

log.setLevel(org.apache.log4j.Level.INFO);
log.info("Script Runner: On IssueCreated -> TEST JCG");

def issue = event.getIssue();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.getIssueManager();
def versionManager = ComponentAccessor.getVersionManager();

// Check if Issue Type is Story
if (issue.getIssueType().getName() == "Story" ) {

// Get Custom Field as string (Epic Link)
def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link")

// Get Custom Field Value as String
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
String EpicName = issue.getCustomFieldValue(epicLink);

if(EpicName){
// Get Epic from Issue
def epicIssue = issue.getCustomFieldValue(epicLinkCf) as Issue

// Get Fix Version from Epic
def currentValue = epicIssue.getFixVersions()
log.info("Set version to: " + currentValue);
// Set Fix Version to Story
def collection = versionManager.getFixVersionsFor(issue);
log.info("Current fixed versions: " + collection);
versionManager.updateIssueFixVersions(issue, currentValue);
versionManager.updateIssueAffectsVersions(issue, currentValue);
collection = versionManager.getFixVersionsFor(issue);
log.info("New fixed versions: " + collection);
// issue.setFixVersions(currentValue);
}
}

This wirks all right and the field Fix Version on my new storie is correctly set. However, I I then go to my board and filter on the version, the new story is not displayed. If I go to the version tab the story is not displayed. If I do the search on issues that have this version as fix version, the story is not displayed.

If I go to the issue tab, deleted the fix version to reset it to the version value, everything is fine. I'm sure I'm missing something somewhere but can't find it...

Can anyone help me on this ?

2 answers

1 accepted

1 vote
Answer accepted
Marc Minten _EVS_
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.
March 5, 2019

Hi, it sounds you need re-indexing the issue :

import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService

boolean
wasIndexing = ImportUtils.isIndexIssues();
IssueIndexingService indexing = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class)
indexing.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

Jean-Christophe Gay March 5, 2019

Thanks a lot, this did the job!

Marc Minten _EVS_
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.
March 6, 2019

Perfect!

0 votes
Ivan Tovbin
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.
March 1, 2019

Hi,

Can you confirm that when a fix version is set via your script, this change is reflected in your issue's change history?

Jean-Christophe Gay March 5, 2019

This is the case.

Suggest an answer

Log in or Sign up to answer