Getting Bundles Containing the Issue

In this example issue key is hardcoded to "ERP-23" but in your case you will probably get the issue key from another API for example current issue inside script listener. In this example I'm converting list of component versions in the bundle to a table markup and adding it as a comment to issue. 


Getting Issues Containing a Bundle
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.project.version.VersionManager

@WithPlugin("com.deniz.jira.versioning")
@PluginModule
BundleService bundleService;
@WithPlugin("com.deniz.jira.versioning")
@PluginModule
CmpVersionMappingService cmpVersionMappingService;

//get the issue
def im = ComponentAccessor.getIssueManager();
def versionManager = ComponentAccessor.getVersionManager();
def issue = im.getIssueObject("ERP-23");


def versions = issue.getAffectedVersions();  //for found in bundle we use affect(s) version, for fixed in bundle we use fix versions
def components = issue.getComponents();

if (versions.size() == 0 || components.size() == 0) {
    return;
}

//def bundleNameFormatter = new BundleNameFormatter(cmpVersionMappingService, null, null); FOR VERSIONS BEFORE 1.12.0
def bundleNameFormatter = new BundleNameFormatter(versionManager, null, null); //FOR VERSIONS 1.12.0+

//get all bundles (affected) containing the version and component pairs of the issue
def affectedBundles = bundleService.getBundles(bundleNameFormatter, versions, components);
System.out.println("affectedBundles:"+affectedBundles)

//get content of bundle as String
def tableMarkup = new StringBuilder("||Component||Version||\n");
for (bundle in affectedBundles) {
	//def bundleContents = cmpVersionMappingService.getComponentsOfBundle(bundle.getId()); FOR VERSIONS BEFORE 1.12.0
	def bundleContents = bundleService.getComponentsOfBundle(bundle.getId()); //FOR VERSIONS 1.12.0+
    for (componentVersion in bundleContents) {
        System.out.println("component:" + componentVersion.getComponentName());
        System.out.println("version:" + componentVersion.getVersionName());
        tableMarkup.append("|" + componentVersion.getComponentName());
        tableMarkup.append("|" + componentVersion.getVersionName() + "|\n");
    }    
}
System.out.println(tableMarkup)

//add bundle table as a comment to issue
def cm = ComponentAccessor.getCommentManager();
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
cm.create(issue, user, tableMarkup.toString(), true)