Can't change fix version in post-function script

VashchenkovA August 2, 2016

I need to add fix version for the linked issue. But script I've wrote doesn't take effect.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser

String processBlockingIssue(MutableIssue origin, Issue issue) {

   ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
   issue.fixVersions.addAll(origin.fixVersions)
   ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_ASSIGNED, true);
   return origin.fixVersions.join(",");
}

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("LIC-197");
MutableIssue myIssue = issue
List<IssueLink> links = ComponentAccessor.getIssueLinkManager().getOutwardLinks(myIssue.id)
String str = "";
for (IssueLink link : links) {
   if ("Blocks".equalsIgnoreCase(link.issueLinkType.name)) {
      str += processBlockingIssue(myIssue, link.destinationObject)
   }
}
str;

I get result "0,5" after calling script in script console. That is only version that alredy assign to issue LIC-197. I have issue LIC-196 that blocks by LIC-197.
Issue LIC-196 and fixVersion define correctly. But issue is not updated.

1 answer

0 votes
Jeff Louwerse
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.
August 2, 2016

when updating most native fields like fixVersion I always have to do a

issue.store();

and based on the way you are creating your issue, you should also do a reindex at the end of the post function.

VashchenkovA August 2, 2016

Change processBlockingIssue to this 

ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.user
issue.setFixVersions(origin.getFixVersions())
issue.store();

But it doesn't work.

Jeff Louwerse
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.
August 2, 2016

start putting in some log statements and make sure you are updating what you think you are updating.  That your loop actually finding LIC-196 and trying to update it with what ever is in LIC-197 (your Origin).

you could also try casing issue as a MutableIssue.  and see if that helps.

As I also mentioned you probably need to reindex it.

 

 

VashchenkovA August 2, 2016

script was change to 

String processBlockingIssue(MutableIssue origin, MutableIssue issue) {
	Project project = issue.getProjectObject()
	Version version = ComponentAccessor.getVersionManager().getVersion(project.getId(), "0.5")
	def res = "\n Updating issue ${issue}."
	issue.setFixVersions([version])
	issue.description = "changed description"
	issue.store();
	boolean wasIndexing = ImportUtils.isIndexIssues();
	ImportUtils.setIndexIssues(true);
	ComponentAccessor.getIssueIndexManager().reIndex(ComponentAccessor.getIssueManager().getIssueObject(issue.getId()));
	ImportUtils.setIndexIssues(wasIndexing);

	res += " Set fix version = [" + issue.fixVersions.join(",") +"]. Saved fix versions = ["+ ComponentAccessor.getIssueManager().getIssueObject(issue.getId()).fixVersions.join(",")+"]"
	return res;
}

After execution in console I have result 

pdating issue LIC-196. Set fix version = [0.5]. Saved fix versions = []

But description have been changed what is wrong? May be there is other way to save fixVersions?

Jeff Louwerse
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.
August 3, 2016

Yes there is another way.. There are actually a couple of ways.  here is some code I use that works... and BTW, you version 0.5  .. it wouldn't happen to be archived would it?

 

Also looking at some older code, this should have worked for you.

issueManager.updateIssue(userManager.getUserObject('automation'), issue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

 

public void setFixVersions4Issue(Long versionID, MutableIssue issue) {
    IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
    OrderableField field = ComponentAccessor.getFieldManager().getOrderableField("fixVersions");

    Collection<Version> Col_Versions = issue.getFixVersions();
    Collection<Version> archVers = VersionUnArchive(Col_Versions);
    Col_Versions.clear();
    if (versionID == null) {
        issue.setFixVersions(null);
    } else {
        Version tempVersion = versionManager.getVersion(versionID);
        Col_Versions.add(tempVersion);
        issue.setFixVersions(Col_Versions);
    }
    field.updateValue(null, issue, new ModifiedValue(issue.getFixVersions(), Col_Versions), changeHolder);
    VersionReArchive(archVers);
	indexManager.reIndex(issue);
}

public Collection<Version> VersionUnArchive(Collection<Version> inVersions) {
    Logger thislog = Logger.getLogger("VersionUnArchive");
    thislog.setLevel(Level.ERROR);
    thislog.debug("Starting VersionArchive");
    Collection<Version> archVers = new ArrayList<>();
    for (Version version : inVersions) {
        if (version.isArchived()) {
            archVers.add(version);
            version.setArchived(false);
        }
    }
    return archVers;
}

public void VersionReArchive(Collection<Version> inVersions) {
    Logger thislog = Logger.getLogger("VersionReArchive");
    thislog.setLevel(Level.ERROR);
    thislog.debug("Starting VersionArchive");
    for (Version version : inVersions) {
        version.setArchived(true);
    }
}
VashchenkovA August 3, 2016

I have found error. All I need it's to change one line. 

ComponentAccessor.getIssueManager().updateIssue(user.directoryUser, (MutableIssue) issue, EventDispatchOption.ISSUE_UPDATED, Boolean.FALSE)

 EventDispatchOption.ISSUE_ASSIGNED must be replaced by EventDispatchOption.ISSUE_UPDATED

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events