python實(shí)現(xiàn)提取jira bug列表的方法示例
公司要求內(nèi)部每日整理jira bug發(fā)郵件,手動(dòng)執(zhí)行了一段時(shí)間,想著用自動(dòng)化的方式實(shí)現(xiàn),故用了3天的時(shí)間做出了此腳本。
第一版基礎(chǔ)版
# -*- 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=""): ''' 關(guān)聯(lián)函數(shù)左右邊界提取數(shù)取 ''' rule=LB + r"(.+?)" + RB datalist=re.findall(rule,data) return datalist def get_bugs_list(): '''根據(jù)規(guī)則提取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請(qǐng)關(guān)閉已經(jīng)打開的bug列表") return 0 else: print("\n \033[31mbug列表輸出地址:" + outbuglist + "\n" + "-"*50 + "-----bug內(nèi)容如上:--------\n") def login_bugwriter(username, password, jql, tempfilepath): """ login_bugwriter(username='str',password='str', jql='str') 登錄jira提取bug列表寫入文件 username:登錄Jira的帳號(hào) password:密碼 jql:jira過濾的語法 tempfilepath:過濾的bug文件臨時(shí)存儲(chǔ)目錄文件 """ '''''' 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,開始提取數(shù)據(jù)") 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("請(qǐng)確認(rèn)過濾條件:==>" + 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無法訪問,請(qǐng)檢查網(wǎng)絡(luò)。") if __name__ == '__main__': login_bugwriter(username, password, jql, tempfilepath) get_bugs_list()
第二版基礎(chǔ)版添加日期相差功能
# -*- 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('日期格式錯(cuò)誤') def diff_date(d1, d2): d1 = datetime.strptime(d1,'%d/%m/%y').date() return (d2 - d1).days def findall_data(data,LB="",RB=""): ''' 關(guān)聯(lián)函數(shù)左右邊界提取數(shù)取 ''' rule=LB + r"(.+?)" + RB datalist=re.findall(rule,data) return datalist def get_bugs_list(): '''根據(jù)規(guī)則提取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請(qǐng)關(guān)閉已經(jīng)打開的bug列表") return 0 else: print("\n \033[31mbug列表輸出地址:" + outbuglist + "\n" + "-"*50 + "-----bug內(nèi)容如上:--------\n") def login_bugwriter(username, password, jql, tempfilepath): """ login_bugwriter(username='str',password='str', jql='str') 登錄jira提取bug列表寫入文件 username:登錄Jira的帳號(hào) password:密碼 jql:jira過濾的語法 tempfilepath:過濾的bug文件臨時(shí)存儲(chǔ)目錄文件 """ '''''' 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,開始提取數(shù)據(jù)") 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("請(qǐng)確認(rèn)過濾條件:==>" + 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無法訪問,請(qǐng)檢查網(wǎng)絡(luò)。") if __name__ == '__main__': #login_bugwriter(username, password, jql, tempfilepath) get_bugs_list()
第三版優(yōu)化了提取器,實(shí)現(xiàn)一鍵提取bug
實(shí)現(xiàn)的代碼
# -*- 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號(hào) #查詢條件 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鏈接,詳細(xì)描述,BUG等級(jí),經(jīng)辦人,報(bào)告人,JIRA狀態(tài),JIRA創(chuàng)建時(shí)間,bug持續(xù)時(shí)間"#["JIRA鏈接","詳細(xì)描述","BUG等級(jí)","經(jīng)辦人","報(bào)告人","JIRA狀態(tài)","JIRA創(chuàng)建時(shí)間","bug持續(xù)時(shí)間"] 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('日期格式錯(cuò)誤') def diff_date(d1, d2): d1 = datetime.strptime(d1,'%d/%m/%y').date() return (d2 - d1).days def findall_data(data,LB="",RB=""): ''' 關(guān)聯(lián)函數(shù)左右邊界提取數(shù)取 ''' rule=LB + r"(.+?)" + RB datalist=re.findall(rule,data) return datalist def get_bugs_list(): '''根據(jù)規(guī)則提取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èi)容如上:--------\n") return bugs_list def login_bugwriter(username, password, jql, tempfilepath): """ login_bugwriter(username='str',password='str', jql='str') 登錄jira提取bug列表寫入文件 username:登錄Jira的帳號(hào) password:密碼 jql:jira過濾的語法 tempfilepath:過濾的bug文件臨時(shí)存儲(chǔ)目錄文件 """ '''''' 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,開始提取數(shù)據(jù)") 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("請(qǐng)確認(rèn)過濾條件:==>" + 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無法訪問,請(qǐng)檢查網(wǎng)絡(luò)。") 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("列表長(zhǎng)度小于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)
實(shí)現(xiàn)一鍵提?。?/p>
編寫2個(gè)bat文件:
init_env.bat #初始化環(huán)境,安裝所需的庫(kù)
pip install requests pip install bs4
get_jira_bugs.bat #調(diào)用主程序?qū)崿F(xiàn)bug列表生成一個(gè)csv文件。
@ECHO OFF python ./jiraCollection.py echo run success! pause
到此這篇關(guān)于python實(shí)現(xiàn)提取jira bug列表的方法示例的文章就介紹到這了,更多相關(guān)python提取jira bug列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例
由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細(xì)的介紹了pygame+opencv實(shí)現(xiàn)讀取視頻幀,感興趣的可以了解一下2021-12-12Python內(nèi)置函數(shù)memoryview()的實(shí)現(xiàn)示例
本文主要介紹了Python內(nèi)置函數(shù)memoryview()的實(shí)現(xiàn)示例,它允許你在不復(fù)制其內(nèi)容的情況下操作同一個(gè)數(shù)組的不同切片,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05跟老齊學(xué)Python之傳說中的函數(shù)編寫條規(guī)
在使用函數(shù)的時(shí)候,首先要把它放在對(duì)象的層面考量,它不是什么特殊的東西,盡管我們使用了不少篇幅講述它,但它終歸還是一個(gè)對(duì)象。2014-10-10使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)
這篇文章主要介紹了使用Python和百度語音識(shí)別生成視頻字幕,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python+Selenium自動(dòng)化環(huán)境搭建與操作基礎(chǔ)詳解
Selenium是如今最常用的自動(dòng)化測(cè)試工具之一,支持快速開發(fā)自動(dòng)化測(cè)試框架,且支持在多種瀏覽器上執(zhí)行測(cè)試。本文將介紹關(guān)于Selenium?Python自動(dòng)化腳本環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2022-03-03有關(guān)Tensorflow梯度下降常用的優(yōu)化方法分享
今天小編就為大家分享一篇有關(guān)Tensorflow梯度下降常用的優(yōu)化方法分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02