Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
themeMidnight
linenumberstrue
import com.deniz.jira.versioning.subcomponents.SubcomponentsService;
import com.deniz.jira.versioning.subcomponents.ComponentHierarchy;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.deniz.jira.versioning.bundles.BundleService;
import com.deniz.jira.versioning.CmpVersionMappingService;
import com.deniz.jira.versioning.bundles.BundleNameFormatter;
import com.atlassian.jira.bc.issue.comment.CommentService;
import com.atlassian.jira.issue.IssueInputParametersImpl;
 
@WithPlugin("com.deniz.jira.versioning")
@PluginModule
BundleService bundleService;
@WithPlugin("com.deniz.jira.versioning")
@PluginModule
CmpVersionMappingService cmpVersionMappingService;

def versionManager = ComponentAccessor.getVersionManager();
def issueService = ComponentAccessor.getIssueService();
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

//issue object is build in and com.atlassian.jira.issue.IssueImp
def versions = issue.getAffectedVersions();  
def components = issue.getComponents();
if (components.size() == 0 || versions.size() == 0) {
    return; //if no component or version is selected we can exit the script
}

//for simplicity of script, I'm assuming user is only selecting a single component and version
def component = components[0]; 
def version = versions[0];  //version class is: com.atlassian.jira.project.version.VersionImpl

def bundlesForVersion = bundleService.getBundlesForVersion(version.getId());
if (bundlesForVersion.size() == 0) {
    return; //this version doesn't belong to any bundle
}

for (bundle in bundlesForVersion) { //bundle is:com.deniz.jira.versioning.BundleImp
    System.out.println("Found bundle for issue:" + bundle.getBundleName());
    //def bundleContent = cmpVersionMappingService.getComponentsOfBundle(bundle.getId()); //FOR VERSIONS BEFORE 1.3.0
    def bundleContent = bundleService.getComponentsOfBundle(bundle.getId());  //FOR VERSION 1.3.0+
    for (componentVersion in bundleContent) {  // componentVersion is com.deniz.jira.bundle.BundleContentImp
        if (componentVersion.getComponentId() == component.getId())  {
            def pVersion = versionManager.getVersion(componentVersion.getVersionId());
            System.out.println("pVersion:" + pVersion.getName());
            def issueInputParameters = new IssueInputParametersImpl();
            issueInputParameters.setAffectedVersionIds(pVersion.getId());
            def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

            if (validationResult.isValid()) {
			    issueService.update(user, validationResult)
			} else {
			    log.warn validationResult.errorCollection.errors
			}
            break;
        }
    }

}

...