jira 查询任务的子任务

这篇博客介绍了如何利用JiraMyGroovy脚本来实现自定义的JQL查询功能。代码示例展示了如何解析JQL、验证查询参数并获取相关问题及其子任务。在应用中,该脚本可以辅助用户更有效地搜索和管理Jira中的问题。
摘要由CSDN通过智能技术生成

 

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;
  }
}

 

效果如下:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值