Versionmanager works properly in JIRA 6.4.x but NOT in JIRA 7 in scriptrunner

I have this script that is supposed to propagate changes to versions in specific project to another projects that target the same version. The main goal was to eliminate maintenance across different projects that use that same exact version (create, delete, update, release...etc).
This is meant to be a workaround for providing a set of global fixversions that all projects can subscribe to. In nutshell, we have around five projects that target same set of versions that belong to an upstream open-source project working on Linux Kernel. so the version of interest is 'Kernel*'. Script works fine on JIRA 6.4.* all changes to any 'Kernel*' versions on that Kernel project are mirrored on the subscribing projcts. When tested on JIRA 7, only creations seems to work but version updates do not work. 
PROBLEM:
versionmanager.editVersion* methods don't seem to work properly and don't throw exception either. versionmanager.createVersion does work and i can see versions created in other projects, but not editing. 
Based on Jamie Echlin's advice, this is not a bug with ScriptRunner. So what could the problem be?

import com.atlassian.jira.event.project.VersionCreateEvent
import com.atlassian.jira.event.project.VersionDeleteEvent
import com.atlassian.jira.event.project.VersionUpdatedEvent
import com.atlassian.jira.event.project.VersionReleaseEvent
import com.atlassian.jira.event.project.VersionUnreleaseEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.version.VersionManager
import java.text.SimpleDateFormat

if (event.version.projectObject.name.trim().equalsIgnoreCase('kernel')){
    def pattern = ~/\b[KkEeRrNnEeLl]\w*\b/
    def kernelVersion = pattern.matcher(event.version.name).getCount()
    if (kernelVersion >= 1){
        ProjectManager pm = ComponentAccessor.getComponent(ProjectManager.class)
        VersionManager vm = ComponentAccessor.getComponent(VersionManager.class)
        def subscriberProjs = pm.getProjectObjects().findAll {
            it.key == 'PROJ1' || it.key == 'PROJ2' || it.key == 'PROJ3' || it.key == 'PROJ4' || it.key == 'PROJ5'
        }
        if (event instanceof VersionCreateEvent){
            def vce = event as VersionCreateEvent
            def newVersion = vce.version.name.trim()
            for (project in subscriberProjs){
                def versionFound = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(newVersion)}
                if (versionFound == null){
                    vm.createVersion(newVersion,vce.version.startDate,
                            vce.version.releaseDate, vce.version.description, project.id, null)
                }
            }
        }
        else if (event instanceof VersionUpdatedEvent){
            SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
            def vue = event as VersionUpdatedEvent
            def oldVersion = vue.originalVersion
            def newVersion = vue.version
            for (project in subscriberProjs){
                def versionFound = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(oldVersion.name.trim()) &&
                        fmt.format(it.releaseDate).equals(fmt.format(oldVersion.releaseDate)) &&
                        fmt.format(it.startDate).equals(fmt.format(oldVersion.startDate))}                
                if (versionFound != null){
                    try { //execution falls through here. reaches end but no update happens on other version and no exception thrown either!!
                        vm.editVersionDetails(versionFound, newVersion.name, newVersion.description)
                        vm.editVersionReleaseDate(versionFound, newVersion.releaseDate)
                        vm.editVersionStartDate(versionFound, newVersion.startDate)
                        vm.update(versionFound)
                    }
                    catch (Exception e){
                        log.debug e.toString()
                    }
                }
                else {
                    def versionWithSimilarName = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(oldVersion.name.trim())}
                    if (versionWithSimilarName == null){
                        vm.createVersion(newVersion.name.trim(),vue.version.startDate,
                                vue.version.releaseDate, vue.version.description, project.id, null)
                    }
                }
            }
        }
        else if (event instanceof VersionDeleteEvent){
            def vde = event as VersionDeleteEvent
            def deletedVersion = vde.version.name.trim()
            def usages = 0
            def counter = 0
            def foundVersions = [:]
            for (project in subscriberProjs){
                def versionFound = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(deletedVersion)}
                if (versionFound != null){
                    counter += 1
                    foundVersions.put(counter,versionFound)
                    if (!vm.getIssueIdsWithFixVersion(versionFound).isEmpty()){
                        usages += 1
                        vm.createVersion(deletedVersion,vde.version.startDate,
                                vde.version.releaseDate, vde.version.description, vde.version.projectId, null)
                    }
                }
            }
            if (usages ==0 && foundVersions.keySet().size() != 0){
                foundVersions.each { k, v ->
                    vm.deleteVersion(v)
                }
            }
        }
        else if (event instanceof VersionReleaseEvent){
            def vre = event as VersionReleaseEvent
            def releasedVersion = vre.version
            for (project in subscriberProjs){
                def versionFound = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(releasedVersion.name.trim())}
                if (versionFound != null){
                    vm.releaseVersion(versionFound, true)
                }
            }
        }
        else if (event instanceof VersionUnreleaseEvent){
            def vure = event as VersionUnreleaseEvent
            def unreleasedVersion = vure.version
            for (project in subscriberProjs){
                def versionFound = vm.getVersions(project.id).find {it.name.trim().equalsIgnoreCase(unreleasedVersion.name.trim())}
                if (versionFound != null){
                    vm.releaseVersion(versionFound, false)
                }
            }
        }
    }
}

2 answers

1 accepted

This widget could not be displayed.

This is now resolved! I haven't noticed that versionmanager.editVersion*() is now returning a Version obj in JIRA7 rather than void as it was in JIRA 6.4.X. So off course, i am updating different objects and leaving them hanging in the air and this is why i neither see errors nor anything gets updated.

Time to punish myself and go into solitary confinement for not paying attentions sad

@Luca Sokoll, @Boris Berenberg and @Jamie Echlin [Adaptavist] Thank you so much for giving a hand in this.

Thanks a lot for this very helpful hint. I was running in the same problem!

This widget could not be displayed.

I'm going to guess that your problem is in here:

try { //execution falls through here. reaches end but no update happens on other version and no exception thrown either!!
    vm.editVersionDetails(versionFound, newVersion.name, newVersion.description)
    vm.editVersionReleaseDate(versionFound, newVersion.releaseDate)
    vm.editVersionStartDate(versionFound, newVersion.startDate)
    vm.update(versionFound)
}
catch (Exception e){
    log.debug e.toString()
}

I'll take a number of stabs in the dark.

First, you are only logging the exception at the "debug" level, and you probably have a higher threshold of logging on this logger. Just get rid of the try/catch and let it fail in a bad way, so you get the exception and stack in the logs. Only catch exceptions if you are expecting them, and at least log them like:

log.warn ("Exception updating version", e)

Second, I'll hazard that the release date is before the start date, and because you set that first it fails. But removing the try/catch will tell you what's the problem.

Also:

vm.update(versionFound)

seems redundant. It looks like it may be overwriting the version with the original version details. I would try removing that.

Thank you Jamie for help with this! I tried your suggestions: removed try/catch, swapped lines editing release and start dates, used both warn and debug and removed vm.update(versionFound) but still no error of any kind shows in the log. I am writing "versionFound: -> Kernel 8.0.0" and "Starting to update version(s):" below in the log file right before calling vm.editVersion*. Here is the pertinent chunk from the debug log below: ================================================================================================== 2015-12-16 00:32:21,512 http-nio-8080-exec-2 WARN amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [c.o.scriptrunner.runner.ScriptRunnerImpl] versionFound: -> Kernel 8.0.0 2015-12-16 00:32:21,512 http-nio-8080-exec-2 WARN amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [c.o.scriptrunner.runner.ScriptRunnerImpl] Starting to update version(s): 2015-12-16 00:32:21,512 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.objectweb.jotm.jta] Current.getStatus() 2015-12-16 00:32:21,514 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.objectweb.jotm.jta] Current.getStatus() 2015-12-16 00:32:21,515 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.objectweb.jotm.jta] Current.getStatus() 2015-12-16 00:32:21,517 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.objectweb.jotm.jta] Current.getStatus() 2015-12-16 00:32:21,517 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.s.b.factory.support.DefaultListableBeanFactory] Creating instance of bean 'scopedTarget.ctxUriInfo' 2015-12-16 00:32:21,517 http-nio-8080-exec-2 DEBUG amro 32x21771x1 ap0ray 184.175.21.22,127.0.0.1 /rest/api/2/version/10676 [o.s.b.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'requestScope

Did you try removing the vm.update() line?

Yes Jamie, I removed vm.update()

That log snippet up there is after i modified everything you mentioned.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

514 views 3 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you