Trae版本更新 | 测试小白使用trae完成jira和飞书联动

我正在参加Trae「超级体验官」创意实践征文,本文所使用的 Trae 免费下载链接:[www.trae.com.cn/?utm_source...] (www.trae.com.cn/?utm_source... "www.trae.com.cn/?utm_source...")

需求整理

  1. 每周把jira-未解决的问题筛选出来,发送到对应项目群中,提醒大家解决
  2. 每周jira-需求,待上线的内容整理出来,发动到团队群中,告知大家本周上线内容

步骤一:安装trae

  1. 安装trae:www.trae.com.cn/?utm_source...

步骤二:配置config.yaml

yaml 复制代码
jira:
  server: "公司jira的server"
  api_token: "jira的api_token"
  timeout: 60  # 可选参数,超时时间
  bug_jql: jira的bugjql
  demands_jql: jira的需求jql

feishu:

  webhook: 飞书机器人的webhook地址

步骤三:使用trae一步一步完成整个需求

1. jira获取数据:帮我写一个方法,通过jql获取jira数据,其中jira的信息从config获取

python 复制代码
def _load_config(self):
    """
    加载config数据
    :return: 
    """
    base_dir = os.path.dirname(os.path.abspath(__file__))
    config_path = os.path.join(os.path.dirname(base_dir), 'config', 'config.yaml')
    with open(config_path) as f:
            return yaml.safe_load(f)

def get_jira_by_jql(self, jql, limit=500):
    """
    获取jira数据
    :param jql:
    :param limit: 默认500
    :return:
    """
    url = f"{self.config['jira']['server']}/rest/api/2/search"
    headers = {
        "Accept": "application/json",
        'Authorization': self.config['jira']['api_token']
    }
    response = requests.get(
        url,
        headers=headers,
        params={
            'jql': jql,
            'maxResults': limit
        },
        timeout=self.config['jira']['timeout']
    )

    jira_data = response.json()["issues"]

    return jira_data

2. 解析jira-bug:根据congif中的bug_jql,获取jira数据,返回总数、状态分布、经办人分布

python 复制代码
def analyze_bug_jira(self):
    """
    分析BUG类Jira数据并返回统计信息
    Returns:
        dict: 包含总数、状态分布、经办人分布的字典
    """
    try:
        jql = self.config['jira']['bug_jql']
        issues = self.get_jira_by_jql(jql)
        
        status_dist = {}
        assignee_dist = {}
        
        for issue in issues:
            # 统计状态分布
            status = issue['fields']['status']['name']
            status_dist[status] = status_dist.get(status, 0) + 1
            
            # 统计经办人分布
            assignee = issue['fields'].get('assignee')
            assignee_name = assignee['displayName'] if assignee else "Unassigned"
            assignee_dist[assignee_name] = assignee_dist.get(assignee_name, 0) + 1
        
        return {
            "total": len(issues),
            "status_distribution": status_dist,
            "assignee_distribution": assignee_dist
        }
        
    except KeyError as e:
        logging.error(f"配置缺少必要参数: {str(e)}")
        return {}
    except Exception as e:
        logging.error(f"数据分析失败: {str(e)}")
        return {}

3. 解析jira-需求:根据congif中的demands_jql,获取jira-key,jira-概要,以及每条jira上关联的产品负责人、经办人、测试负责人

python 复制代码
def analyze_demand_jira(self):
    """
    获取需求类Jira数据并返回结构化信息
    Returns:
        dict: 包含总数和需求详情的字典
    """
    try:
        jql = self.config['jira']['demands_jql']
        issues = self.get_jira_by_jql(jql)
        
        demands = []
        for issue in issues:
            # 获取负责人信息(根据实际字段调整)
            assignee = issue['fields'].get('assignee', {}).get('displayName', 'Unassigned')
            product_owner = issue['fields'].get('customfield_12345', {}).get('value', 'N/A')  # 替换为实际产品负责人字段
            test_owner = issue['fields'].get('customfield_67890', {}).get('value', 'N/A')     # 替换为实际测试负责人字段
            
            demands.append({
                "jira_key": issue['key'],
                "summary": issue['fields']['summary'],
                "product_owner": product_owner,
                "assignee": assignee,
                "test_owner": test_owner
            })
        
        return {
            "total": len(demands),
            "demands": demands
        }
        
    except KeyError as e:
        logging.error(f"配置或字段缺失: {str(e)}")
        return {}
    except Exception as e:
        logging.error(f"需求数据解析失败: {str(e)}")
        return {}

4. 发送飞书bug通知:根据congif中的jira-webhook,发送bug的飞书消息,格式如下

本周未解决oncall共××个

状态分布为:排查中××个,开发中××个,测试中××个,暂不解决××个

负责人分布为:经办人统计

python 复制代码
def send_bug_report_to_feishu(self):
    """
    发送BUG统计报告到飞书
    """
    try:
        stats = self.analyze_bug_jira()
        webhook_url = self.config['jira']['feishu_webhook']
        
        # 构建消息内容
        status_counts = [
            f"{status}×{count}个"
            for status, count in stats['status_distribution'].items()
        ]
        
        assignee_counts = [
            f"{assignee}×{count}个"
            for assignee, count in stats['assignee_distribution'].items()
        ]

        message = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": "本周Oncall未解决BUG统计",
                        "content": [
                            [{
                                "tag": "text",
                                "text": f"本周未解决oncall共{stats['total']}个\n"
                                        f"状态分布为:{','.join(status_counts)}\n"
                                        f"负责人分布为:{','.join(assignee_counts)}\n"
                                        "@所有人"
                            }]
                        ]
                    }
                }
            }
        }
        
        # 发送飞书消息
        response = requests.post(
            webhook_url,
            json=message,
            timeout=10
        )
        response.raise_for_status()
        
    except KeyError as e:
        logging.error(f"缺少必要配置项: {e}")
    except Exception as e:
        logging.error(f"飞书消息发送失败: {str(e)}")

5. 发送飞书需求通知:根据congif中的jira-webhook,发送需求的飞书消息,格式如下

本周上线内容如下: 需求: jira-KEY,内容,安特产品、经办人、测试人员

bug: jira-KEY,内容,安特经办人、测试人员

产品及时验收,对应开发检查下有没有遗漏代码,对应测试及时回归

python 复制代码
def send_demand_report_to_feishu(self):
    """
    发送需求上线报告到飞书
    """
    try:
        demand_data = self.analyze_demand_jira()
        bug_data = self.analyze_bug_jira()
        webhook_url = self.config['jira']['feishu_webhook']
        
        # 构建消息内容
        demand_lines = [
            f"{d['jira_key']},{d['summary']},产品:{d['product_owner']},开发:{d['assignee']},测试:{d['test_owner']}"
            for d in demand_data.get('demands', [])
        ]
        
        bug_lines = [
            f"{issue['key']},{issue['fields']['summary']},经办:{issue['fields'].get('assignee', {}).get('displayName', 'Unassigned')},测试:{self._get_test_owner(issue)}"
            for issue in self.get_jira_by_jql(self.config['jira']['bug_jql'])
        ]

        message = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": "本周上线内容通知",
                        "content": [
                            [{
                                "tag": "text",
                                "text": "📢 本周上线内容如下:\n\n"
                                        "✅ 需求:\n" + "\n".join(demand_lines) + "\n\n"
                                        "🐞 Bug:\n" + "\n".join(bug_lines) + "\n\n"
                                        "⚠️ 提醒:\n"
                                        "1. 产品及时验收\n"
                                        "2. 开发检查代码是否遗漏\n"
                                        "3. 测试及时回归验证\n"
                                        "@所有人"
                            }]
                        ]
                    }
                }
            }
        }
        
        # 发送飞书消息
        response = requests.post(
            webhook_url,
            json=message,
            timeout=10
        )
        response.raise_for_status()
        
    except KeyError as e:
        logging.error(f"缺少必要配置项: {e}")
    except Exception as e:
        logging.error(f"飞书消息发送失败: {str(e)}")

步骤四:调试并改bug

很意外,就一个问题,然后就跑通了!!!

报错1:ERROR:root:飞书消息发送失败: 'JiraReporter' object has no attribute '_get_test_owner'

图一是我跑通的图,图二是我整个调试的终端,很快就通了,图三是trae帮我改的bug

相关推荐
大话性能3 小时前
使用枚举(Enum)提升你的python代码质量
测试
哔哩哔哩技术10 小时前
B站端侧DCDN容灾演练实践
测试·cdn
WebInfra1 天前
🔥 Midscene 重磅更新:支持 AI 驱动的 Android 自动化
android·前端·测试
用户7858298243061 天前
Selenium3.0 平台级自动化测试框架综合实战(完结)
测试
用户7858298243061 天前
Selenium3+Pytest+Allure 落地 Python Web 自动化测试
测试
felix19912 天前
pytest-dsl: 用自然语言编写自动化测试,让测试代码不再难懂
测试
越学不动啦4 天前
十、自动化函数+实战
运维·软件测试·自动化·测试
Hy小杨4 天前
压测JMeter经验分享文档-Concurrency Thread Group
测试
waves浪游5 天前
自动化测试常用函数
测试用例·bug·测试