JiraMyGroovy脚本
git@github.com:mailru/jira-scripts.git
样例
代码如下:
import com.atlassian.jira.JiraDataType;
import com.atlassian.jira.JiraDataTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.query.Query;
import com.atlassian.jira.jql.parser.JqlParseException;
// import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.parser.JqlQueryParser
import ru.mail.jira.plugins.groovy.util.lucene.IssueIdCollector;
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.MessageSet
import com.atlassian.jira.util.MessageSetImpl
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand
import ru.mail.jira.plugins.groovy.api.jql.ScriptedJqlValuesFunction
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
class DemoFunction implements ScriptedJqlValuesFunction {
private final Logger logger = LoggerFactory.getLogger(DemoFunction.class);
@Override
public JiraDataType getDataType() {
return JiraDataTypes.ISSUE;
}
@Override
public MessageSet validate(ApplicationUser searcher, FunctionOperand operand, TerminalClause terminalClause) {
def i18n = ComponentAccessor.getI18nHelperFactory().getInstance(searcher)
def numberValidMessage = new NumberOfArgumentsValidator(1i, i18n).validate(operand);
if (numberValidMessage.hasAnyErrors()) {
return numberValidMessage
}
def messageSet = new MessageSetImpl();
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
try {
def query = jqlQueryParser.parseQuery(operand.getArgs().get(0))
// messageSet.addMessage(MessageSet.Level.WARNING, "jqlQueryParser :${query}");
} catch (any) {
messageSet.addErrorMessage("not valid jql:${operand.getArgs().get(0)}");
messageSet.addErrorMessage("${any}");
}
return messageSet
}
public List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand functionOperand, TerminalClause terminalClause) {
final List<QueryLiteral> literals = new LinkedList<>();
String jql = functionOperand.getArgs().get(0);
for (MutableIssue issue: getIssuesByJQL(jql, queryCreationContext.getApplicationUser()) ){
literals.add(new QueryLiteral(functionOperand, issue.getKey()));
for (Issue subIssue: issue.getSubTaskObjects()){
literals.add(new QueryLiteral(functionOperand, subIssue.getKey()));
}
}
return literals;
}
private Collection<MutableIssue> getIssuesByJQL(String jql, ApplicationUser user) {
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class);
SearchProvider searchProvider = ComponentAccessor.getComponent(SearchProvider.class);
IssueManager issueManager = ComponentAccessor.getIssueManager();
try{
Query query = jqlQueryParser.parseQuery(jql);
Collection<MutableIssue> issues = new ArrayList<>();
IssueIdCollector collector = new IssueIdCollector();
searchProvider.search(query, user, collector);
logger.error("query:{}, user:{}, collector:{}, IssueIds:{} ", query, user, collector, collector.getIssueIds());
for(String issueId: collector.getIssueIds()){
logger.error("issueId:{}, issue:{}",issueId, issueManager.getIssueObject(new Long(issueId)));
//
// issues.add(issueManager.getIssueObject(issueId));
issues.add(issueManager.getIssueObject(new Long(issueId)));
}
return issues;
}catch (JqlParseException jqlParseException){
System.out.println("jqlParseException:" + jqlParseException);
}catch (Exception exception){
System.out.println("exception:" + exception);
}
return null;
}
}