美文网首页jira
Python JIRA 库的使用

Python JIRA 库的使用

作者: Lorence | 来源:发表于2020-02-09 13:15 被阅读0次

    由于工作的需要,我们每天都会使用到JIRA去记录并跟踪产品设计问题的解决。JIRA里面有大量的数据,Reporter, Assignee, Severity, Description, Comment etc... 而JIRA 网页上生成出来的项目Excel报告,比较难看。
    以前也写过用Excel VBA的方法,来导出JIRA某个项目Project的问题,但这里面出现一个问题,就是要求每个用户的JIRA里面的问题内容的列排序要求一致。且Project ID选择上做的很不友好。
    偶然发现JIRA有官方的Python 库,研究了一轮。


    1 设计了简单的登录界面,输入JIRA的账户,密码。因为考虑到程序是给不同的用户使用。这个必须的。不过如果每次打开都输入,挺麻烦的。可以考虑把账户密码,写入线外的某个文件中,以后就直接自动调用即可。


    image.png

    2 登录完成后,输入产品的型号。由于产品型号对应不同的Project ID,我把Project ID按一定的格式另外存放在一个Excel文件中,然后用Pandas库读取了这份Excel表,进行型号与Project ID的比对。代码片段如下:

        TLA=e3.get()
        n=""
        file=pd.read_excel("D:\\My Documents\\JIRA\\Export DFM Report\\Project_ID.xlsx")
        res1=file[file['TLA PN']==TLA]['Project ID'].values
        if res1.size>0:
            n="".join(res1)
        else:
            n=0
        if n==0:
            print("Can't find the TLA PN, please double check")
        else:
            exportDFM(TLA,n)
    
    1. 通过第二步获取了型号对应的Project ID后,就在JIRA中搜索该Project ID对应的issue。这里写了一句搜索JQL语句
      jql="project = "+prj+" and type=DFM"
      由于JIRA最多只能导出1000条issue,如果该Project的issue超过1000条就无法正常导出了,所以用了个while循环,每1000条问题搜索一次JIRA。r然后把所有问题都写入一个列表allissues中去。
    block_size=1000
        block_num=0
        allissues=[]
        while True:
            start_idx=block_num*block_size
            jql="project = "+prj+" and type=DFM"
            issues=jira.search_issues(jql,start_idx,block_size)
            if len(issues)==0:
                break
            block_num+=1
            for issue in issues:
                allissues.append(issue)
    
    1. 获得要的issue列表后,利用Pandas,把列表的内容整理成表单。这里使用到字典一一对应。字典的键作为表单的列头,值就是列表中对应的内容。
    dfm=pd.DataFrame()
        for item in allissues:
            d={'key':item.key,
            'PCB': item.fields.customfield_10424,
            'Summary':item.fields.summary,
            'Severity': item.fields.customfield_10323,
            'Description':item.fields.description,
            'Reporter':item.fields.reporter,
            'Assignee':item.fields.assignee,
            'Create Stage': item.fields.customfield_10324,
            'Created Date':item.fields.created[0:10],
            'Updated Date':item.fields.updated[0:10],
            'Status':item.fields.status,
            'Solution':None
            }
    

    这里面有一些常用的filed和自定义filed的使用。

    例如:一般问题的ields中的属性分为固定属性和自定义属性,自定义属性格式一般为类似customfield_10012这种。常用的问题的Fields有:

    • assignee:经办人
    • created: 创建时间
    • creator: 创建人
    • labels: 标签
    • priority: 优先级
    • project: 所示项目
    • reporter: 报告人
    • status: 状态
    • summary: 问题描述
    • updated: 更新时间
    • watches: 关注者
    • comments: 评论
    • resolution: 解决方案

    关于Comment的导出处理。Comments field里面一般有很多条留言,以下代码是导出所有留言,并按时间整理,避免混乱。

    solution=""
    comments=jira.comments(item)
    for comment in comments:  #Get DFM comment filed content
         solution=solution+comment.created[0:10] + "  " + str(comment.author.name) + ":  " + comment.body+'\n'
    d['Solution']=solution
    
    1. 当所有问题表单整理完毕后,我们再利用Pandas导出到一份新的excel表中。
    dfm=dfm[['key','Reporter','Assignee','PCB','Status','Severity','Description','Solution','Create Stage','Created Date','Updated Date']]
    outputfile="D:\\My Documents\\JIRA\\Export DFM Report\\"+TLA+" DFM Report.xlsx"
    dfm.to_excel(outputfile,encoding='UTF-8',header=True,index=False)
    

    完成的程序代码如下:

    from tkinter import *
    import tkinter as tk
    import tkinter.font as tf
    from jira import JIRA
    from jira.exceptions import JIRAError
    import pandas as pd
    import openpyxl
    
    def login():
        account=e1.get()
        pw=e2.get()
        global e3
        try:
            jira=JIRA("http://jira-ep.ecp.priv",auth=(account,pw))
            #e1.grid_forget()
            e1.destroy()
            e2.destroy()
            b1.destroy()
            l1.destroy()
            l2.destroy()
            Label(frame2, text="Hi "+account+", Please input or select a TLA PN: ").grid(row=3,column=1, padx=5,pady=5,sticky=E,columnspan=3)
            v3=StringVar()
            e3=Entry(frame2,textvariable=v3)
            e3.grid(row=3,column=6,padx=5,pady=5,sticky=W)
            b2=Button(frame2,text="Submit",width=10,command=CheckDFM)
            b2.grid(row=3,column=8,padx=5,pady=5,sticky=W)
        except:
            pass
            #Label(frame2, text="Login to JIRA failed. Check your username and password").grid(row=1,padx=5,pady=5,sticky=E,columnspan=2)
    
    
    def CheckDFM():
        TLA=e3.get()
        n=""
        file=pd.read_excel("D:\\My Documents\\JIRA\\Export DFM Report\\Project_ID.xlsx")
        res1=file[file['TLA PN']==TLA]['Project ID'].values
        if res1.size>0:
            n="".join(res1)
        else:
            n=0
        if n==0:
            print("Can't find the TLA PN, please double check")
        else:
            exportDFM(TLA,n)
    
    def exportDFM(TLA,prj):
        jira=JIRA("JIRA的域名",auth=('登录账户','密码'))
        block_size=1000
        block_num=0
        allissues=[]
        while True:
            start_idx=block_num*block_size
            jql="project = "+prj+" and type=DFM"
            issues=jira.search_issues(jql,start_idx,block_size)
            if len(issues)==0:
                break
            block_num+=1
            for issue in issues:
                allissues.append(issue)
        dfm=pd.DataFrame()
        for item in allissues:
            d={'key':item.key,
            'PCB': item.fields.customfield_10424,
            'Summary':item.fields.summary,
            'Severity': item.fields.customfield_10323,
            'Description':item.fields.description,
            'Reporter':item.fields.reporter,
            'Assignee':item.fields.assignee,
            'Create Stage': item.fields.customfield_10324,
            'Created Date':item.fields.created[0:10],
            'Updated Date':item.fields.updated[0:10],
            'Status':item.fields.status,
            'Solution':None
            }
            solution=""
            comments=jira.comments(item)
            for comment in comments:  #Get DFM comment filed content
                solution=solution+comment.created[0:10] + "  " + str(comment.author.name) + ":  " + comment.body+'\n'
            d['Solution']=solution
            dfm =dfm.append(d,ignore_index=True)
        dfm=dfm[['key','Reporter','Assignee','PCB','Status','Severity','Description','Solution','Create Stage','Created Date','Updated Date']]
        outputfile="D:\\My Documents\\JIRA\\Export DFM Report\\"+TLA+" DFM Report.xlsx"
        dfm.to_excel(outputfile,encoding='UTF-8',header=True,index=False)
        print("Done")
    
    
    
    app=Tk()
    app.title("JIRA DFM Report")
    ft=tf.Font(family="Segoe UI",size=12)
    frame1=Frame(app)
    frame2=Frame(app)
    frame3=Frame(app)
    frame1.grid(row=1,column=0)
    frame2.grid(row=2,column=0)
    frame3.grid(row=3,column=0)
    Label(frame1,text="Welcome,this tool is used to export DFM report from JIRA").grid(row=0,column=0,padx=5,pady=5)
    l1=Label(frame2, text="JIRA Account:  ")
    l1.grid(row=3,column=0,padx=5,pady=5,sticky=E)
    v1=StringVar()
    e1=Entry(frame2,textvariable=v1)
    e1.grid(row=3,column=1,padx=5,pady=5,sticky=E)
    l2=Label(frame2, text="Password:  ")
    l2.grid(row=3,column=2,padx=5,pady=5,sticky=W)
    v2=StringVar()
    e2=Entry(frame2,textvariable=v2)
    e2.grid(row=3,column=3,padx=5,pady=5,sticky=E)
    b1=Button(frame2,text="Login",width=10,command=login)
    b1.grid(row=3,column=4,padx=10,pady=5,sticky=E)
    
    app.mainloop()
    

    程序参考资料:

    https://courses.cs.ut.ee/LTAT.05.008/2018_spring/uploads/Main/HW4-2018-pdf
    https://jira.readthedocs.io/en/latest/

    相关文章

      网友评论

        本文标题:Python JIRA 库的使用

        本文链接:https://www.haomeiwen.com/subject/zsegxhtx.html