ScriptRunner 开创了一个自动化和定制化的新世界,省去了购买多款插件的成本。
全球有超过 1 万家企业使用 Adaptavist 产品及服务,包含了一半财富 500 强企业。下面介绍 YY 哥曾实践过的 3 个 ScriptRunner for Jira 插件应用场景,希望对大家的工作有所帮助或启发。
01
—
Story 状态变更提醒到钉钉群
使用 Groovy 脚本实现高级自动化,在 ScriptRunner 中新建一个 Script Listener,用来监听 Jira 项目的 Issue Created 和 Updated 事件,如果问题类型是 Story 时,执行如下代码将自定义 Webhook 消息体推送到特定的钉钉群。该方法与 Automation for Jira 插件的功能异曲同工https://xie.infoq.cn/article/0143c5cb040dbc35c6365cbf5。
 // 获取问题的关键字def issueKey = issue.key// 获取问题的概述def summary = issue.fields.summary// 获取问题的描述def description = issue.fields.description// 指定Webhook消息体内容def webhookbody = "{'msgtype': 'markdown','markdown':{'title':'【问题提醒】','text':'【小蜜提醒】\n\r >以下Jira问题已创建或已更新: \n\r >问题Key:${issueKey} \n\r >问题概述: ${summary} \n\r >问题描述: ${description}\n'},'at':{'isAtAll':true}}"// Post the constructed message to Dingtalkdef postToDingTalk = post('https://oapi.dingtalk.com/robot/send?access_token=02f31b8e109d476a35a534fab51865074533af7f256b4ef515f21297798exxxx')        .header('Content-Type', 'application/json')
        .body(webhookbody)        .asObject(Map)        .bodyassert postToDingTalk : "创建钉钉群消息失败,请检查日志查看详情!"logger.info("Issue: {} has been created by: {} in project: {}", issue.key, user.displayName, issue.fields.project.key)
   复制代码
 实际效果如下:
02
—
展示最新 Jira 备注
当工作场景中需要每周或每日更新未完成的 Issue 备注时,为了能快速检查出那些未按时更新的 Issues,则可以在 ScriptRunner 中新建一个 Field,用来保存 Jira Issue 中最新更新的那条备注,在“最新备注”自定义字段最前面增加了备注的更新者和更新日期,详细代码如下。
 import com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.comments.Commentimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def rendererManager = ComponentAccessor.getRendererManager()def comments = commentManager.getComments(issue)String latestcomments = "";
if (comments){
    latestcomments = latestcomments + "<p><span style=\"font-size:normal;color:red\">最新备注日期: " + comments.last().getCreated().format('YYYY-MM-dd') + "</span></p>";
    for(comment in comments){        if(comment.getUpdated().format('YYYY-MM-dd') == comments.last().getCreated().format('YYYY-MM-dd')){            latestcomments = latestcomments +  "<p><span style=\"font-size:normal;color:green\">" + comment.authorFullName + " | " + comment.body + "</span></p>";
        }    }    return latestcomments;}
   复制代码
 
03
—
展示 Jira Issue 已持续时长
当工作场景中需要查看未解决的线上 Bug 持续时长时,以便快速定位那些持续时间比较长的 Bugs,则可以在 ScriptRunner 中新建一个 Field,用来保存 Jira Issue 从创建到当前时间的持续天数,详细代码如下。
 
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.Issue
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()def createDate = issue.getCreated().getTime();def resolutionDate = issue.getResolutionDate();def delta,hours;
if (resolutionDate) {    hours = ((resolutionDate.getTime() - createDate) as Long) / 3600000;}else{    hours = ((System.currentTimeMillis() - createDate) as Long) / 3600000;}
def days = hours/24 ;return days.round(1)
   复制代码
 
如果你喜欢这篇文章,请记得点赞+评论哦,谢谢你的支持!
评论