日报小工具 - 钉钉定时发送Wiki日报

前言:

测试日报是用来记录每天测试工作的完成情况、发现的问题,以及问题修复进度,以便团队了解项目进展、及时调整计划,并保持信息同步交流。每日发送测试日报属于重复性工作,开发日报小工具结合Jenkins进行定时自动发送,有效节约人力成本,提高了整体测试效能。

整体流程图:

1、获取Wiki有效内容:

python 复制代码
import configparser
import math
import requests
from loguru import logger
from bs4 import BeautifulSoup
import pandas as pd
import re

def confluence_read(content_id):
    config = configparser.ConfigParser()
    config.read('Conf/common.conf')
    # 构建API请求的headers
    headers = {
        'Content-Type': 'application/json'
    }
    url = f'https://xxxx.com/rest/api/content/{content_id}?expand=space,body.view,version,container'
    # 发送GET请求获取content
    response = requests.get(url, headers=headers, auth=('xxx', 'xxx'))
    # 检查响应状态码
    if response.status_code == 200:
        return response.json()
    else:
        logger.info(f'请求失败,code= {response.status_code}')

html_data = confluence_read(xxxxxx)

涉及获取Wiki文档json数据知识:
https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/
https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-createContent

configparser使用:https://docs.python.org/zh-cn/3/library/configparser.html

2、处理json数据并解析文档

python 复制代码
def beaut_table_data(html_data):
    content = html_data['body']['view']['value']
    # 处理获取到的content数据
    # 使用BeautifulSoup解析HTML内容
    return BeautifulSoup(content, 'html5lib')

# 读取Wiki日报表格数据:
table = beaut_table_data(html_data).find_all('table')

涉及BeautifulSoup知识:BeautifulSoup简单用法

3、读取对应日报表格数据

python 复制代码
# 观察Wiki日报结构,发现有个模板表格,所以取第二个table(table[1])
df = pd.read_html(str(table[1]))[0]

涉及pandas知识:https://www.pypandas.cn/docs/getting_started/basics.html

4、获取bug信息

python 复制代码
def get_bug_info(jira_filter, replacements):
    template = Template(jira_filter)
    jira_filter_result = template.substitute(replacements)
    # logger.info(f'jql:{jira_filter_result}')
    jira = ConnectJira.connect_jira()
    issues = jira.search_issues(jira_filter_result, 0, 9999, 1, '', '', '')
    return [f'经办人: {i.fields.assignee}, {i.fields.summary}' for i in issues]

jira_filter = "status in (等待修复, 修复中, 自测中) AND affectedVersion = ${affectedVersion}"
replacements = '1.1.0'
bug_info = get_bug_info(jira_filter, replacements)

涉及JQL知识:JQL语法及Python查询 Jira issue信息

Template使用:Python string模块中Template的substitute()使用

5、拼接日报内容

python 复制代码
    html_data = confluence_read(xxxxx)
    # print(html_data)
    table = beaut_table_data(html_data).find_all('table')
    # print(table[1])
    df = pd.read_html(str(table[1]))[0]
    # print(df)
    process_col_title = ['iOS', 'Android', 'H5/服务端']
    doing = []
    done = []
    affected_versions = []
    replacements = {}
    for index, row in df.iterrows():
        # print(index)
        # print(row)
        if index == 0:
            table_title = row[0].split('(')
            # print(table_title)
            affected_versions.append(table_title[0])
            # print(affected_versions[0])
            replacements = {'affectedVersion': affected_versions[0]}
        if row[0] != '需求名称' and row[0] != '主需求' and index >= 1:
            col_values = [row[i] if not (isinstance(row[i], float) and math.isnan(row[i])) else '' for i in range(3, 6)]
            # print(col_values)
            if not any(col_values) or not any(re.search(pattern, col_value) is not None for col_value in col_values):
                continue
            title_col_value = row[0]
            # print(title_col_value)
            if any(re.search(pattern, col_value) is not None and set(col_value.split(',')) != {'100%'} for col_value in col_values):
                col_values = ['不涉及' if re.search(pattern, i) is None else i for i in col_values]
                # print(col_values)
                doing_rate_info = [f'{process_col_title[i]}-{col_values[i]}' for i in range(len(process_col_title))]
                # print(doing_rate_info)
                doing.append(f'{title_col_value},【进度:{", ".join(doing_rate_info)}】')
            else:
                done.append(title_col_value)
    # print(doing)
    # print(done)
    doing = '\n\n> - '.join(doing)
    done = '\n\n> - '.join(done)
    buglist = '\n\n> - '.join(bug_info) if bug_info else '暂无'
    # print(doing)
    # print(done)

6、钉钉发送日报信息

python 复制代码
def send_dingtalk_message(webhook_url, message, msg_type, title):
    headers = {'Content-Type': 'application/json'}
    if msg_type == 'markdown':
        data = {'msgtype': msg_type, 'markdown': {'title': title, 'text': message}}
    else:
        data = {'msgtype': msg_type, 'text': {'content': message}}
    response = requests.post(url=webhook_url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        logger.info("Message sent successfully to DingTalk!")
    else:
        logger.info("Failed to send message to DingTalk")
        logger.info(f"Response status code: {response.status_code}")
        logger.info(f"Response content: {response.text}")

webhook_url获取:钉钉添加自定义机器人,实现每周定时@某人

7、最终效果

使用Markdown格式发送日报:

Markdown 基本语法:https://markdown.com.cn/basic-syntax/

相关推荐
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴3 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再3 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手5 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ7 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha7 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy7 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法