`
cutecoot
  • 浏览: 119856 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jira通过scriptrunner整合日志与confluence上周报并输出pdf

阅读更多

背景是为了整合在jira的Tempo中记录的工时,与在confluence上记录的周报,并自动合成pdf周报。

 

开始寻找导出工具,考虑输出excel,word等方式,最后选择了jira的插件Better PDF Exporter for Jira来输出pdf。这个插件能定制化格式,并引入jira中多种类型数据,比较好用。使用vm模板issue-fo就包含了多种数据类型,包括Tempo日志。

 

流程是:在JIra中检索需输出的issue生成issue列表-->获取issue的相关信息如获取相关的confluence页面-->将issue列表和<issue key, 对应内容信息>传给pdfApi.getPdf-->调用vm模板(Velocity语言)-->按照模板生成pdf-->发送email

 

1. 以scriptrunner中使用Midori(better pdf Exporter)的例子做为基础扩展。可参见第一个Email的例子 -- https://scriptrunner.adaptavist.com/latest/jira/plugins/working-with-midori.html

在scriptrunner的script console中,使用此例子,可以把要导出的问题列表换位搜索出的问题列表。Debug使用log.warn(***)

 

def jqlSearch = "project = RPM AND issuetype = 项目流程管理 AND status in (Open, Pending, 规划, 设计, 开发, 维护, 实施) " 
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)
def parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    List Results = searchResult.getResults()
 再把输出函数中,传入的issue列表换为检索出的Results。pdfResult = pdfApi.getPdf(templateName, pdftitle.toString(), Results, [:])

 

 

2 判断获取的issue列表中issue是否符合日期条件,使用java的LocalDate

LocalDate today = LocalDate.now()
date = content.substring(strStartIndex, strEndIndex)
date = date.replace("/","-")
LocalDate reportDay = LocalDate.parse(date)
if (today.toEpochDay()-reportDay.toEpochDay()>9){                                
   continue
}                       

 

3 从issue的comment中获取网址,使用pattern

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
for (comment in comments){
    def content = comment.getBody()
    if (content.contains("http")){
        Pattern pattern = Pattern.compile("https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()){ ContentMap.put(issue.key,matcher.group().substring(matcher.group().indexOf('/',10)))
    }
}

 

4 https协议下登录confluence页面,验证并获取页面内容

    SSLContextBuilder builder = new SSLContextBuilder()
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),NoopHostnameVerifier.INSTANCE)
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()
    CredentialsProvider credsProvider = new BasicCredentialsProvider()
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials("user","PASSWD", "", "")) 

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache()
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme()
    HttpHost target = new HttpHost("IP", PORT, "https");     
    authCache.put(target, basicAuth)
    // Make sure the same context is used to execute logically related requests 
    HttpClientContext context = HttpClientContext.create() 
    context.setCredentialsProvider(credsProvider)
    context.setAuthCache(authCache) 
          
        HttpGet httpget = new HttpGet(entry.getValue())
        CloseableHttpResponse response = httpclient.execute(target, httpget, context)               
        try {
            StringBuilder sb = new StringBuilder(2048)
            HttpEntity entityRes = response.getEntity()
            Reader reader = new InputStreamReader(entityRes.getContent(),"UTF-8")
            def data
            while ((data = reader.readLine()) !=null )
            {
                sb.append(data)
             }
         }
response.getEntity().getContent().close()

 

5 精简获取到的页面内容

String regex_title = "<title>.*?</title>"
String regex_table = "<table.*>.*</table>"
final Pattern pa_title = Pattern.compile(regex_title, Pattern.CANON_EQ);
final Pattern pa_talbe = Pattern.compile(regex_table, Pattern.CANON_EQ);
final Matcher ma_table = pa_talbe.matcher(sb)
final Matcher ma_title = pa_title.matcher(sb);
while (ma_title.find())
{
    list.add(ma_title.group())
}

 

 6 使用Jsoup(https://jsoup.org/cookbook/)处理并解析获得的网页内容,clean清理不必要的标签,使用Document和Elements解析html标签。

 

Whitelist relax = Whitelist.relaxed();
relax.addTags("title");
String JResult = Jsoup.clean(html, relax)
Document doc = Jsoup.parse(JResult)
Elements Title = doc.select("title")  
contentResult.add(Title.text())
Elements titles = doc.getElementsByTag("th")
List<String> titleResult = new ArrayList<String>()
for (Element title : titles) { 
   String sTitle = title.text()
   if (!sTitle.contains("报告"))
   {
         titleResult.add(sTitle)
    }
}
 

 

7 输出pdf,issue列表为issueList,自定义参数Report传递map变量ResultMap<String,LIst>(issue key与对应信息)

 

def pdfApiClass = this.class.forName('com.midori.jira.plugin.pdfview.api.PdfApi', true, ComponentAccessor.pluginAccessor.classLoader)
def pdfApi = ComponentAccessor.getOSGiComponentInstanceOfType(pdfApiClass)
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(user)
pdfResult = pdfApi.getPdf(templateName, pdftitle.toString(), issueList, [Report:ResultMap])
 

 

8 自定义vm模板。在JIRA的插件-->Better PDF Exporter-->PDF Templates中,可以依据现有vm自定义vm模板。语言使用Velocity,格式为XSL-FO格式(https://www.runoob.com/xslfo/xslfo-tutorial.html)。$velocityCount是自带变量,记录循环次数,可直接使用。Report变量是从pdfAPI.getPdf传入的Map变量。

#foreach($item in $Report.entrySet())
	#if($item.key == $issue.key)
            <fo:table-row>
            <fo:table-cell padding="4px">
            #foreach($content in $item.value)
                 #if($velocityCount == 1)
                     <fo:block padding="4px" start-indent="4px" end-indent="4px" background-color="#F5F5F5" font-weight="bold"  text-align="center">$i18n.getText($content)</fo:block>
                 #else
                     <fo:block padding="4px" start-indent="4px" end-indent="4px">$i18n.getText($content)</fo:block>
                 #end
             #end
             </fo:table-cell>
             </fo:table-row>
       #end
#end

 

9 获取字符串中最后一个指定字符串后的所有字符

StringUtils.substringAfterLast(c_table, "<table")

 

 

10. 所需要的import

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import javax.activation.DataHandler
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import com.atlassian.jira.mail.util.ByteArrayDataSource
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser

//时间处理
import java.time.LocalDate
import java.time.Period

//Pattern检索
import java.util.regex.Matcher
import java.util.regex.Pattern

//https授权登录相关
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.conn.ssl.TrustSelfSignedStrategy
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.HttpEntity
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.auth.NTCredentials
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.HttpHost
import org.apache.http.client.AuthCache
import org.apache.http.impl.auth.BasicScheme
import org.apache.http.impl.client.BasicAuthCache

//Jsoup相关
import org.jsoup.Jsoup
import org.jsoup.safety.Whitelist
import org.jsoup.nodes.Element
import org.jsoup.nodes.Document
import org.jsoup.select.Elements

//字符串处理
import org.apache.commons.lang3.StringUtils

 

 

 

分享到:
评论

相关推荐

    如何整合Jira跟confluence的用户管理

    如果Jira与Confluence都安装在同一台服务器上,可以通过简单的几步完成用户管理的整合: 1. **搭建Jira**:首先按照官方文档完成Jira的安装和基本配置。 2. **搭建Confluence**:接着按照官方文档完成Confluence的...

    Jira ScriptRunner脚本-根据子任务修改父任务自定义字段状态

    Jira ScriptRunner脚本-根据子任务修改父任务自定义字段状态

    JIRA 与 Confluence 同步故障排除

    然而,在实际部署和使用过程中,可能会遇到两者间的数据同步问题,本文将详细介绍如何排查并解决 JIRA 与 Confluence 的同步故障。 #### 故障现象 当 Confluence 与 JIRA 之间的数据同步出现问题时,常见的表现...

    scriptrunner:Adaptavist的Jira的ScriptRunner的Groovy脚本和代码段

    Adaptavist的Jira的ScriptRunner的Groovy脚本和代码段 ListCustomFieldsUsage.groovy是一个脚本,它将在您的Jira Server(或数据中心)实例中查找所有自定义字段,如果该字段是全局的(即,该字段没有上下文),则...

    jira8.22和confluence7.16备份恢复文档

    【Jira 8.22.0 数据备份与恢复】 Jira 8.22.0 的备份主要包括项目数据和附件。系统管理功能允许管理员执行全量备份项目数据,而较大的附件数据则需要通过手动方式单独备份。附件存储在 `/var/atlassian/application...

    Jira&Confluence安装配置说明

    "Jira&Confluence安装配置说明" 本文档主要讲述了Jira和Confluence的安装配置过程。下面将对Jira的安装配置进行详细的讲解。 一、JDK的安装 JDK的安装是Jira安装的前提条件。首先,需要下载JDK5.0,并安装到D:\...

    Crowd单点登录-管理jira与confluence

    crowd2.7下载:http://downloads.atlassian.com/software/crowd/downloads/atlassian-crowd-2.7.0.zip crowd安装+破解+集成jira+集成confluence

    Confluence JIRA 报表(JIRA Report)蓝图 - CWIKIOSSEZ

    ### Confluence与JIRA报表(JIRA Report)蓝图详解 #### 概述 Confluence与JIRA报表(JIRA Report)蓝图是一种强大的工具,旨在帮助用户在Atlassian的Confluence平台中创建清晰、直观的报表,以便更好地追踪和管理...

    linux下mysql、jira、confluence整合.docx

    linux下mysql、jira、confluence整合.docxlinux下mysql、jira、confluence整合.docx

    jira与testlink整合

    jira与testlink整合(jira用oracle数据库)

    JIRA+Confluence实现敏捷协同开发

    本节课程将介绍JIRA及Confluence集成于敏捷项目中的应用实践。重点在Scrum计划会议、立会、反思及检讨会议如何透过JIRA及Confluence实现协同合作,进而帮助敏捷团队持续过程改进。

    CentOS系统安装JDK+Crowd+JIRA+Confluence教程

    JDK+Crowd+JIRA+Confluence安装教程 JDK+Crowd+JIRA+Confluence安装教程

    jira confluence 语言包+破解包

    jira_6.0.* confluence 5.0.* 语言包+破解包 请低调使用。毕竟没有花钱的东西。里面如何使用都有详细的readme。

    Confluence_7-3-0_CompleteGuide.pdf

    Confluence是Atlassian公司开发的一款企业级知识管理与协同软件,广泛应用于项目管理、内容协作、知识共享等领域。Confluence 7.3版本提供了许多新功能和改进,以下将基于手册内容,详细介绍Confluence 7.3的主要...

    JIRA+Confluence实现敏捷协同开发视频地址

    ### JIRA与Confluence在敏捷协同开发中的应用 #### 一、引言 在现代软件开发过程中,项目管理和协作工具的选择对于提升团队效率至关重要。JIRA 和 Confluence 作为 Atlassian 公司旗下的两款明星产品,在敏捷开发...

    Jira 6.0 文档PDF格式

    7. **集成能力**:Jira 6.0 可与众多其他工具无缝集成,如Confluence(文档协作)、Bitbucket(代码版本控制)和Crucible(代码审查)。这些集成增强了协作和开发流程的效率。 8. **权限与安全**:Jira 提供精细的...

    与JIRA有关的几个PDF文档

    1. JIRA用户使用指南-简化版.pdf 2. 工作流介绍.pdf 3. 使用JIRA搭建企业问题跟踪系统.pdf 4. jira 4.0.1 汉化包 ------------------------------------------------------------ 以前下载要求资源分, 现在取消了. ...

    JIRA.rar_jira_jira bug跟踪_jira pdf_jira工具使用

    4. **集成能力**:JIRA可与Git、Confluence、Bitbucket等工具无缝集成,实现代码管理、文档协作和版本控制的一体化。 综上,JIRA是一个强大的项目管理工具,无论是在bug跟踪还是敏捷开发中,都能提供高效的解决方案...

    jira项目管理系统使用指南.pdf

    jira项目管理系统使用指南.pdf jira项目管理系统使用指南.pdf是一份关于jira项目管理系统的使用指南,旨在帮助用户快速掌握jira系统的使用方法,提高项目管理效率。 在该指南中,首先介绍了项目管理系统的简介,...

    JIRA与Confluence管理员切换用户身份插件

    JIRA 、Confluence Switch User 插件 就像 linux 里面的 su 命令一样,管理员可以切换到其它用户身份来使用系统,而无需要知道或改变该用户的密码,方便调试。 其中 Confluence Switch User 插件是有管理和配置界面...

Global site tag (gtag.js) - Google Analytics