实现功能的背景
虽然开发都能在bug管理工具里查看到自己的bug,但是领导可能仍然要求测试需要提醒开发修改bug。
如果只是在群里@所有人查看bug,太过笼统,开发可能并不会理会这种群发的消息。如果只@有bug的开发,每次测试下班前都需要去做这个重复的工作。
可以试着编写脚本实现自动在企业微信(飞书等等办公软件)上@对应开发修改bug的功能。
如何实现功能
- 确认是否能够使用脚本在办公软件发信息
要实现这个功能,首先需要确认是否能够使用脚本在办公软件发信息?微信的管制就比较多,不是很方便。这里使用的是企业微信 - 如何@其他人
首先查看企业微信里支持添加群机器人
点击添加群机器人,选择新创建一个机器人,编辑好姓名后可以开始查看机器人配置说明
我们需要单独@别人,查看文本类型的数据格式支持@其他人
并不是开发者获取不到userid,可以使用mentioned_mobile_list来@其他人 - 如何查询指定的bug
bug管理工具,通常支持接口根据开发人员、创建人、bug状态等查询bug
我们可以现在界面上查询bug
比如我们需要把今天新建的bug@给对应的开发
那么首先我们在bug管理工具上查询创建人=我,bug状态=新建的bug
通过F12拿到查询接口的payload和接口地址以及response数据
分析response数据拿到开发人员list,把list数据去重就是我们需要@的开发人员清单
创建开发人员对应字典,key=response中对应的开发人员数据,value=开发人员的手机号
查询的接口就是我们需要发送的数据,至此发送的数据和@相关人员就都完成了。
如果工作时间固定,更进一步可以做成定时发送的
4 代码实现
引用的包
from playwright.sync_api import Playwright, sync_playwright
import requests
from datetime import datetime, timedelta
import json
from urllib.parse import unquote, quote
发送企业微信消息接口
def func_send_wechat_msg(content):
url = "企业微信wehook地址"
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "text",
"text": content
}
with requests.post(url=url, headers=headers, data=json.dumps(data, ensure_ascii=False).encode('utf-8'),
verify=False) as resp:
# 检查响应状态码
if resp.status_code == 200:
print("请求成功!")
else:
print("webhook请求失败,错误码:", resp.status_code, resp.reason)
首先需要拿到访问bug管理工具的cookie 或token
具体的可以使用playwright录制一下登录的过程
python -m playwright codegen
def get_cookie(playwright: Playwright):
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto(
"https://bug管理工具地址")
page.get_by_placeholder("用户名").click()
page.get_by_placeholder("用户名").fill("自己的用户名称")
page.get_by_placeholder("密码").click()
page.get_by_placeholder("密码").fill("自己的密码")
page.get_by_role("button", name="登录").click()
cookies_list = context.cookies(urls='https://bug管理工具')
key_list = ['USER_REALM_KEY', 'PRE-GW-LOAD', 'PRE-GW-SESSION']
cookie = ''
for item in cookies_list:
if item['name'] in key_list:
cookie = cookie + item['name'] + '=' + item['value'] + '; '
else:
continue
cookie = cookie[:-2]
return cookie
查找bug
def func_search(iql: str, cookie: str):
'''
:param iql:
:param cookie:
:return: 返回查找到的bug id
'''
# 设置目标接口的URL
url = 'https://bug管理工具地址'
# 设置header
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN;q=0.9,zh;q=0.8,en;q=0.7",
"Connection": "keep-alive",
"Content-Length": "454",
"Content-Type": "application/json",
"Cookie": cookie,
"DNT": "1",
"Host": "devops.swhysc.com:9000",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"X-Parse-Application-Id": "osc",
"X-Parse-Session-Token": "eyJhZG1pbiI6ZmFsc2UsImNvbXBhbmllcyI6WyJvc2MiXSwiY29tcGFueSI6Im9zYyIsImNvbXBhbnlJZGVudGl0eSI6IkNPTVBBTllfTUVNQkVSIiwiY29tcGFueVBhdGgiOiJvc2MiLCJkaXNwbGF5TmFtZSI6IuWImOa1qSIsImVtYWlsIjoibGl1aGFvMUBzemtpbmdkb20uY29tIiwiaWQiOiIxMTUwMiIsInNBTUFjY291bnROYW1lIjoianpfamxpdWhhbyIsInN0YXR1cyI6IlNVQ0NFU1MiLCJ1U05DcmVhdGVkIjoiMTE1MDIiLCJ1c2VyUHJpbmNpcGFsTmFtZSI6ImxpdWhhbzFAc3praW5nZG9tLmNvbSIsInVzZXJuYW1lIjoianpfamxpdWhhbyJ9",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\""
}
body = {
"iql": iql
, "fields": ["key", "status", "assignee", "priority", "createdBy", "createdAt", "updatedAt", "workspace",
"itemGroup", "itemType", "rowId", "id", "ancestors"],
"refererInfo": {"workspaceKey": "gmjjtgscxt"}}
# 使用with发送POST请求,使用json参数传递JSON数据
with requests.post(url, json=body, headers=headers) as response:
# 检查响应状态码
if response.status_code == 200:
print("请求成功!")
# 处理响应数据
respnose_data = json.loads(response.text).get('payload').get('items')
if len(respnose_data) > 0:
return respnose_data
else:
print("search请求失败,错误码:", response.status_code, response.reason)
return []
得到需要@的开发人员列表并发送出去
def func_send_new_bug(cookie: str, bug_person_list: list):
# 确认bug是否成立
# 得到今天的日期
today = datetime.today().strftime("%Y-%m-%d")
create_bug_iql = f'''(('类型' in ["测试缺陷"] and '创建时间' >= '{today}' and '创建时间' <= '{today}' and '创建人' in [\"jz_jliuhao\",\"fb_wangzongnan\",\"bjrh_zhulingling\",\"jz_xiongxin\"] and '状态' in ['新建']) and (所属空间 = 'xxx')) order by 创建时间 desc'''
items = func_search(create_bug_iql, cookie)
print(f"create_bug_iql = \n{create_bug_iql}")
if len(items) == 0:
print('没有新建bug,不发送bug确认消息')
return
bug_preson = [i['values']['assignee'][0]['nickname'] for i in items]
if not (real_person := set(bug_person_list).intersection(bug_preson)):
print('群里对应的开发没有新建bug,不发送bug确认消息')
return
iql_url = quote(create_bug_iql)
url = f'''
https://bug管理工具地址/1288d967-1553-4b7a-86e8-200c3a0cae26?board=1288d967-1553-4b7a-86e8-200c3a0cae26&component=Default&iql={iql_url}&pageIndex=1&tenant=osc&view=bUFcVMaZM1&workspace=gmjjtgscxt
'''
content_message = {"content": f'''
请注意验证bug是否成立
{url}
''', "mentioned_mobile_list": [mobile_dict[name] for name in real_person if name in mobile_dict]}
func_send_wechat_msg(content_message)
return
至此-企业微信自动@对应开发修改bug的功能就完成了