Run Groove scripts from Java

Hi guys.

I have one script written in Groovy

package com.onresolve.jira.groovy.canned.admin

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventDispatcher
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.util.SimpleErrorCollection
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
import com.onresolve.jira.groovy.canned.utils.FilterUtils
import org.apache.log4j.Category
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.util.BuildUtils
import com.atlassian.jira.issue.IssueManager

public class ReindexIssues implements CannedScript {

    public static String FIELD_FILTER_ID = "FIELD_FILTER_ID"
    public static String FIELD_PROJECT_ID = "FIELD_PROJECT_ID"

    ComponentManager componentManager = ComponentManager.getInstance()
    JiraAuthenticationContext authenticationContext = componentManager.getJiraAuthenticationContext()
    JiraServiceContext ctx = new JiraServiceContextImpl(authenticationContext.getUser());
    def searchRequestService = componentManager.getSearchRequestService()
    IssueIndexManager indexManager = ComponentManager.getInstance().getIndexManager()
    IssueManager issueManager = ComponentManager.getInstance().getIssueManager()

    Category log = Category.getInstance(ReindexIssues.class)

    public String getName() {
        "Reindex issues"

    List getCategories() {

    public String getDescription() {
        """Reindex issues for a project or corresponding to a filter, perhaps after an indexing problem or editing the database.

    public List getParameters(Map params) {
                Label:"Filter ID",
                Description:"""All issues returned by this filter will have an event generated.""",
                Values: CannedScriptUtils.getAllOwnedFilters(true),
                Label:"Project Key",
                Description:"""All issues for this project will have an event generated.""",
                Values: CannedScriptUtils.getProjectOptions(true),

    public ErrorCollection doValidate(Map params, boolean forPreview) {
        ErrorCollection errorCollection = new SimpleErrorCollection()
        if (! (params[FIELD_FILTER_ID] as Boolean ^ (params[FIELD_PROJECT_ID]) as Boolean)) {
            errorCollection.addErrorMessage("Please enter a project OR filter ID")
            return errorCollection

        if (params[FIELD_FILTER_ID]) {
            long filterId = params[FIELD_FILTER_ID] as Long
            SearchRequest sr = searchRequestService.getFilter(ctx, filterId)
            if (! sr) {
                errorCollection.addError(FIELD_FILTER_ID, "Invalid filter ID... do you own this?")

        return errorCollection

    Map doScript(Map params) {
        SearchRequest sr = getOrCreateSearchRequest(params)
        SearchResults results = getSearchResults(sr)
        List issues = results.getIssues();

        for (Issue issue in issues)
            boolean wasIndexing = ImportUtils.isIndexIssues();
            if ((BuildUtils.getCurrentBuildNumber() as Long) < 614) {
                ManagerFactory.getCacheManager().flush(com.atlassian.jira.issue.cache.CacheManager.ISSUE_CACHE, issue)
            log.debug("Reindex issue ${issue.key}")

        ["output":"${issues.size()} issues were reindexed."]

    SearchRequest getOrCreateSearchRequest(Map params) {

    String getDescription(Map params, boolean forPreview) {
        SearchRequest sr = getOrCreateSearchRequest(params)
        String projectId = params[FIELD_PROJECT_ID]

        SearchResults results = getSearchResults(sr)
        """Will operate on ${results.getTotal()} issue(s) from the \"${sr.getName() ?: projectId}\" filter... """

    private SearchResults getSearchResults(SearchRequest sr) {
        return FilterUtils.getSearchResults(sr)

    public Boolean isFinalParamsPage(Map params) {

    public String getHelpUrl() {

Can I execute it from my jira plugin written in java?

1 answer

1 accepted

> I have one script written in Groovy

That's actually my script...

You can get hold of the script runner component from your java plugin, using component-import or whatever, and with the relevant bundle-imports in your pom or plugin.xml.

But it's pretty small, you may as well just rewrite it in java.

Hi, Jamie!

Is there any docs for converting groovy syntax to Java syntax. For example, I'm trying to implement the method getParameters based on one of your scripts, but can't figure out the data structure the caller expects... Can you help me, at least, with this example?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 13, 2019 in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


246 views 0 6
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