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.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.JiraAuthenticationContext
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() {
        ["ADMIN"]
    }

    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) {
        [
            [
                Name:FIELD_FILTER_ID,
                Label:"Filter ID",
                Description:"""All issues returned by this filter will have an event generated.""",
                Type:"list",
                Values: CannedScriptUtils.getAllOwnedFilters(true),
            ],
            [
                Name:FIELD_PROJECT_ID,
                Label:"Project Key",
                Description:"""All issues for this project will have an event generated.""",
                Type:"list",
                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();
            ImportUtils.setIndexIssues(true);
            if ((BuildUtils.getCurrentBuildNumber() as Long) < 614) {
                ManagerFactory.getCacheManager().flush(com.atlassian.jira.issue.cache.CacheManager.ISSUE_CACHE, issue)
            }
            log.debug("Reindex issue ${issue.key}")
            indexManager.reIndex(issueManager.getIssue(issue.id));
            ImportUtils.setIndexIssues(wasIndexing);
        }

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

    SearchRequest getOrCreateSearchRequest(Map params) {
        FilterUtils.getOrCreateSearchRequest(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) {
        true
    }

    public String getHelpUrl() {
        "https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-ConditionTester"
    }
}

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

1 answer

1 accepted

2 votes

> 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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

255 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