python实现提取jira bug列表的方法示例

 更新时间:2021年11月01日 17:15:36   作者:qdPython  
公司要求内部每日整理jira bug发邮件,手动执行了一段时间,想着用自动化的方式实现,所以本文主要介绍了python实现提取jira bug列表,感兴趣的可以了解一下

公司要求内部每日整理jira bug发邮件,手动执行了一段时间,想着用自动化的方式实现,故用了3天的时间做出了此脚本。

第一版基础版

# -*- coding:utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup as bs
import time
import os

jql = "project = SDP and parent = SDP-13330 AND issuetype in (standardIssueTypes(), subTaskIssueTypes(), BUG) AND status in (新建, 解决中, 重新打开) AND priority in (P0, P1, P2) AND reporter in (membersOf(SDP_Tester)) ORDER BY priority DESC"
username = "robin.li"
password = "a12345678"
tempfilepath = r"f:\bug_list.csv"
outbuglist = r"f:\bug_out" + time.strftime("%Y%m%d", time.localtime()) + ".csv"
parentid = "SDP-13330"

bugs_list = []

def findall_data(data,LB="",RB=""):
    '''    关联函数左右边界提取数取    '''
    rule=LB + r"(.+?)" + RB
    datalist=re.findall(rule,data)
    return  datalist


def get_bugs_list():
    '''根据规则提取bug列表'''
    if os.path.exists(outbuglist):  # 如果文件存在
        os.remove(outbuglist)
    with open(r"f:\bug_list.csv", 'r') as f:
        content = str(f.readlines())[1:-1]
        bug = []
        soup = bs(content, features='html.parser')

        for tr in soup.select('tr'):
            for td in tr.select('td'):
                bug.append(str(td.text).strip(r''))
            bugs_list.append(bug)
            bug = []

        for bug in bugs_list:
            clear_list = str(bug[1:-2]).replace("\\n", "").replace("\'", "").replace(" ", "").replace("\,", "").replace("\\xa0", "").replace(parentid, "").replace("[", "").replace("]", "")
            print(clear_list)
            try:
                with open(outbuglist, 'a') as f:
                    f.write(clear_list + "\n")
            except PermissionError:
                print("\033[31m请关闭已经打开的bug列表")
                return 0
        else:
            print("\n \033[31mbug列表输出地址:" + outbuglist + "\n" + "-"*50 + "-----bug内容如上:--------\n")


def login_bugwriter(username, password, jql, tempfilepath):
    """
    login_bugwriter(username='str',password='str', jql='str')
    登录jira提取bug列表写入文件
    username:登录Jira的帐号
    password:密码
    jql:jira过滤的语法
    tempfilepath:过滤的bug文件临时存储目录文件
    """
    ''''''
    data = {'os_username': username, 'os_password': password, 'login': '登录'}
    res = requests.get("https://jira.clouddeep.cn/secure/Dashboard.jspa")

    if res.status_code == 200:
        print("可以访问Jira,开始提取数据")
        jsession = requests.Session()
        cookie_jar = jsession.post("https://jira.clouddeep.cn/login.jsp",data=data).cookies
        login_cookie = requests.utils.dict_from_cookiejar(cookie_jar)
        print("登录成功,整理列表")

        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
            "Sec-Fetch-Dest": "document",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
        }

        r=requests.get("https://jira.clouddeep.cn/browse/SDP-13330",headers=headers, cookies=login_cookie)
        #print(r.text)
        jql_url="https://jira.clouddeep.cn/issues/SDP-13330/?jql=" + jql
        print("请确认过滤条件:==>" + jql_url + "\n" + "-"*30)
        bug_list = requests.get(jql_url ,headers=headers, cookies=login_cookie)
        with open(tempfilepath,'w') as f:
            f.write(bug_list.text)
    else:
        print("jira无法访问,请检查网络。")

if __name__ == '__main__':
    login_bugwriter(username, password, jql, tempfilepath)
    get_bugs_list()

第二版基础版添加日期相差功能

# -*- coding:utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup as bs
import time
import os
from datetime import datetime

jql = "project = SDP and parent = SDP-13330 AND issuetype in (standardIssueTypes(), subTaskIssueTypes(), BUG) AND status in (新建, 解决中, 重新打开) AND priority in (P0, P1, P2) AND reporter in (membersOf(SDP_Tester)) ORDER BY priority DESC"
username = "a.li@rc.cn"
password = "Rc123456"
tempfilepath = r"f:\bug_list.csv"
outbuglist = r"f:\bug_out" + time.strftime("%Y%m%d", time.localtime()) + ".csv"
parentid = "SDP-13330"
bugs_list = []

def trans_month(date):
    ch_date_dict = {'一': '01', '二': '02', '三': '03', '四': '04', '五': '05', '六': '06', '七': '07', '八': '08', '九': '09', '十': '10', '十一': '11', '十二': '12', }
    old_date = date.split('/')
    ch_date = str(old_date[1])[0]
    if ch_date in ch_date_dict:
        old_date[1]=ch_date_dict[ch_date]
        new_date = "/".join(str(i) for i in old_date)
        return new_date
    else:
        print('日期格式错误')

def diff_date(d1, d2):
    d1 = datetime.strptime(d1,'%d/%m/%y').date()
    return (d2 - d1).days

def findall_data(data,LB="",RB=""):
    '''    关联函数左右边界提取数取    '''
    rule=LB + r"(.+?)" + RB
    datalist=re.findall(rule,data)
    return  datalist

def get_bugs_list():
    '''根据规则提取bug列表'''
    if os.path.exists(outbuglist):  # 如果文件存在
        os.remove(outbuglist)
    with open(r"f:\bug_list.csv", 'r') as f:
        content = str(f.readlines())[1:-1]
        bug = []
        soup = bs(content, features='html.parser')

        for tr in soup.select('tr'):
            for td in tr.select('td'):
                if td.text != " \\n":
                    print('2==',len(bug))
                    bug.append(str(td.text).strip(""))
                if "<img" in str(td):  #提取日期
                    get_date = tr.time.text  # 09:30:45'
                    d1 = trans_month(get_date)
                    d2 = datetime.now().date()
                    diff_days = diff_date(d1, d2)

                    s = str(td.img['alt'])
                    print('1==',len(bug))
                    bug.insert(len(bug),str(diff_days))
            bug.append(s)
#print('1',bug) #print('2',bug)  bugs_list.append(bug) #bugs_list = [i for i in bugs_list if i != ''] #print('3',bugs_list) bug = [] for bug in bugs_list: #ps = bug # print(len(bug)) # if len(bug) != 0: clear_list = str(bug[:-2]).replace("\\n", "").replace("\'", "").replace(" ", "").replace("\,", "").replace("\\xa0", "").replace(parentid, "").replace("[", "").replace("]", "") #print(clear_list) #clear_list = clear_list.split(",") #clear_list = [i for i in clear_list if i !=''] print(clear_list) try: with open(outbuglist, 'a') as f: f.write(str(clear_list) + "\n") except PermissionError: print("\033[31m请关闭已经打开的bug列表") return 0 else: print("\n \033[31mbug列表输出地址:" + outbuglist + "\n" + "-"*50 + "-----bug内容如上:--------\n") def login_bugwriter(username, password, jql, tempfilepath): """ login_bugwriter(username='str',password='str', jql='str') 登录jira提取bug列表写入文件 username:登录Jira的帐号 password:密码 jql:jira过滤的语法 tempfilepath:过滤的bug文件临时存储目录文件 """ '''''' data = {'os_username': username, 'os_password': password, 'login': '登录'} res = requests.get("https://jira.clouddeep.cn/secure/Dashboard.jspa") if res.status_code == 200: print("可以访问Jira,开始提取数据") jsession = requests.Session() cookie_jar = jsession.post("https://jira.clouddeep.cn/login.jsp",data=data).cookies login_cookie = requests.utils.dict_from_cookiejar(cookie_jar) print("登录成功,整理列表") headers={ "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1", } r=requests.get("https://jira.clouddeep.cn/browse/SDP-13330",headers=headers, cookies=login_cookie) #print(r.text) jql_url="https://jira.clouddeep.cn/issues/SDP-13330/?jql=" + jql print("请确认过滤条件:==>" + jql_url + "\n" + "-"*30) bug_list = requests.get(jql_url ,headers=headers, cookies=login_cookie) with open(tempfilepath,'w') as f: f.write(bug_list.text) else: print("jira无法访问,请检查网络。") if __name__ == '__main__': #login_bugwriter(username, password, jql, tempfilepath) get_bugs_list()

第三版优化了提取器,实现一键提取bug

实现的代码

# -*- coding:utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup as bs
import time
import os
from datetime import datetime

parentid = "SDP-15642"   #bug汇总 jira号

#查询条件
jql = "project = SDP and parent = " + parentid +" AND issuetype in (standardIssueTypes(), subTaskIssueTypes(), BUG) AND status in (新建, 解决中, 重新打开,解决中) AND priority in (P0, P1, P2) AND reporter in (membersOf(SDP_Tester)) ORDER BY priority DESC"

tr_title = "JIRA链接,详细描述,BUG等级,经办人,报告人,JIRA状态,JIRA创建时间,bug持续时间"#["JIRA链接","详细描述","BUG等级","经办人","报告人","JIRA状态","JIRA创建时间","bug持续时间"]
username = "aa.li@ss.cn"
password = "ss2018()"
tempfilepath = r"./bug_list.csv"
outbuglist = r"./bug_out" + time.strftime("%Y%m%d", time.localtime()) + ".csv"
# orderbuglist = r"./order_bug_out" + time.strftime("%Y%m%d", time.localtime()) + ".csv"


bugs_list = []


def trans_month(date):
    ch_date_dict = {'一': '01', '二': '02', '三': '03', '四': '04', '五': '05', '六': '06', '七': '07', '八': '08', '九': '09', '十': '10', '十一': '11', '十二': '12', }
    old_date = date.split('/')
    ch_date = str(old_date[1])[0]
    if ch_date in ch_date_dict:
        old_date[1]=ch_date_dict[ch_date]
        new_date = "/".join(str(i) for i in old_date)
        return new_date
    else:
        print('日期格式错误')

def diff_date(d1, d2):
    d1 = datetime.strptime(d1,'%d/%m/%y').date()
    return (d2 - d1).days

def findall_data(data,LB="",RB=""):
    '''    关联函数左右边界提取数取    '''
    rule=LB + r"(.+?)" + RB
    datalist=re.findall(rule,data)
    return  datalist


def get_bugs_list():
    '''根据规则提取bug列表'''
    # if os.path.exists(outbuglist):  # 如果文件存在
    #     os.remove(outbuglist)
    with open(r"f:\bug_list.csv", 'r') as f:
        content = str(f.readlines())[1:-1]
        bug = []
        soup = bs(content, features='html.parser')

        for tr in soup.select('tr'):
            for td in tr.select('td'):
                str1 =  re.sub(r"[\\n'\s]","",str(td.text))
                str2 = re.sub("[\s ,\r\n]{1,99}", ",", str1)
                if str2 != "":
                    bug.append(str(str2).strip(""))
                if "<img" in str(td):  #提取日期
                    get_date = tr.time.text  # 09:30:45'
                    d1 = trans_month(get_date)
                    d2 = datetime.now().date()
                    diff_days = diff_date(d1, d2)
                    s = str(td.img['alt'])
                    bug.insert(len(bug),str(diff_days))
                    bug.append(s)

            bugs_list.append(bug)
            bug = []


        else:
            print("\n \033[31mbug列表输出地址:" +  "\n" + "-"*50 + "-----bug内容如上:--------\n")
    return bugs_list


def login_bugwriter(username, password, jql, tempfilepath):
    """
    login_bugwriter(username='str',password='str', jql='str')
    登录jira提取bug列表写入文件
    username:登录Jira的帐号
    password:密码
    jql:jira过滤的语法
    tempfilepath:过滤的bug文件临时存储目录文件
    """
    ''''''
    data = {'os_username': username, 'os_password': password, 'login': '登录'}
    res = requests.get("https://jira.clouddeep.cn/secure/Dashboard.jspa")

    if res.status_code == 200:
        print("可以访问Jira,开始提取数据")
        jsession = requests.Session()
        cookie_jar = jsession.post("https://jira.clouddeep.cn/login.jsp",data=data).cookies
        login_cookie = requests.utils.dict_from_cookiejar(cookie_jar)
        print("登录成功,整理列表")

        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
            "Sec-Fetch-Dest": "document",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
        }

        r=requests.get("https://jira.clouddeep.cn/browse/" + parentid,headers=headers, cookies=login_cookie)
        jql_url="https://jira.clouddeep.cn/issues/" + parentid + "/?jql=" + jql
        print("请确认过滤条件:==>" + jql_url + "\n" + "-"*30)
        bug_list = requests.get(jql_url ,headers=headers, cookies=login_cookie)
        with open(tempfilepath,'w') as f:
            f.write(bug_list.text)
    else:
        print("jira无法访问,请检查网络。")

def order_buglist(buglist):
    with open(outbuglist,'w') as f:
        f.write(tr_title + '\n')
        print(tr_title)
        for l in buglist:
            if len(l) <= 1:
                print("列表长度小于1")
            else:
                s = l[3:4] + str(l[4:5]).split(",")[2:3] +l[2:3] + l[9:11] + l[5:6] + l[8:9] + l[1:2]
                s2 = re.sub(r"'","",str(s)[3:-1])
                s3 = re.sub(r"[, ]{1,8}",",",s2)
                f.write("https://jira.clouddeep.cn/browse/" + str(s3) + '\n')
                print('out>>',s3)
    print("bug列表输出地址:\n",outbuglist)
  if os.path.exists(tempfilepath):
      os.remove(tempfilepath)
  else:
      print("no such file: %s" %tempfilepath)
if __name__ == '__main__': login_bugwriter(username, password, jql, tempfilepath) bug = get_bugs_list() order_buglist(bug)

实现一键提取:

编写2个bat文件:

init_env.bat #初始化环境,安装所需的库

pip install requests 
pip install  bs4 

get_jira_bugs.bat #调用主程序实现bug列表生成一个csv文件。

@ECHO OFF
python ./jiraCollection.py
echo  run success!
pause

到此这篇关于python实现提取jira bug列表的方法示例的文章就介绍到这了,更多相关python提取jira bug列表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现五子棋双人对弈

    python实现五子棋双人对弈

    这篇文章主要为大家详细介绍了python实现五子棋双人对弈,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Python 把序列转换为元组的函数tuple方法

    Python 把序列转换为元组的函数tuple方法

    今天小编就为大家分享一篇Python 把序列转换为元组的函数tuple方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python实现多态、协议和鸭子类型的代码详解

    Python实现多态、协议和鸭子类型的代码详解

    问起面向对象的三大特性,几乎每个人都能对答如流:封装、继承、多态。今天我们就要来说一说Python实现多态、协议和鸭子类型,感兴趣的朋友跟随小编一起看看吧
    2019-05-05
  • Python pandas轴旋转stack和unstack的使用说明

    Python pandas轴旋转stack和unstack的使用说明

    这篇文章主要介绍了Python pandas轴旋转stack和unstack的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • opencv 图像加法与图像融合的实现代码

    opencv 图像加法与图像融合的实现代码

    这篇文章主要介绍了opencv 图像加法与图像融合的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 用Python快速读取Excel数据

    用Python快速读取Excel数据

    嘿,想学会用Python快速读取Excel数据吗?不用担心,这个指南将带你轻松掌握这项技能,让我们一起开始吧!
    2023-12-12
  • python修改注册表终止360进程实例

    python修改注册表终止360进程实例

    这篇文章主要介绍了python修改注册表终止360进程实例,是非常实用的进程操作技巧,需要的朋友可以参考下
    2014-10-10
  • 教你使用Python画棵圣诞树完整代码

    教你使用Python画棵圣诞树完整代码

    圣诞节快到了,今天小编通过代码画颗圣诞树,主要通过t.pensize(10) 修改画笔大小,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-12-12
  • python pandas时序处理相关功能详解

    python pandas时序处理相关功能详解

    这篇文章主要介绍了python pandas时序处理相关功能详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Python尾递归优化实现代码及原理详解

    Python尾递归优化实现代码及原理详解

    这篇文章主要介绍了Python尾递归优化实现代码及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10

最新评论