#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 禅道 API 示例脚本 - 创建 Bug """ import requests import json # 禅道配置(需要根据实际情况修改) ZENTAO_URL = "http://your-zentao-domain.com" # 禅道地址 ZENTAO_ACCOUNT = "your_username" # 禅道账号 ZENTAO_PASSWORD = "your_password" # 禅道密码 def get_token(): """获取禅道 API Token""" url = f"{ZENTAO_URL}/api.php/v1/tokens" headers = { "Content-Type": "application/json" } data = { "account": ZENTAO_ACCOUNT, "password": ZENTAO_PASSWORD } response = requests.post(url, headers=headers, json=data) result = response.json() if response.status_code == 200 and result.get("status") == "success": return result.get("token") else: raise Exception(f"获取 Token 失败: {result}") def create_bug(token, product_id, title, steps, severity=2, priority=2, module='', assigned_to=''): """ 创建Bug 参数: token: API Token product_id: 产品ID title: Bug标题 steps: 重现步骤 severity: 严重程度 (1-4, 默认2: 一般) priority: 优先级 (1-4, 默认2: 中等) module: 模块ID (可选) assigned_to: 指派给谁 (可选) """ url = f"{ZENTAO_URL}/api.php/v1/products/{product_id}/bugs" headers = { "Content-Type": "application/json", "Authorization": token } data = { "product": product_id, "module": module, "title": title, "steps": steps, "severity": severity, "priority": priority, "assignedTo": assigned_to } # 移除值为空的字段 data = {k: v for k, v in data.items() if v} response = requests.post(url, headers=headers, json=data) result = response.json() if response.status_code == 200 and result.get("status") == "success": print(f"✅ Bug 创建成功!Bug ID: {result.get('data', {}).get('id')}") return result else: print(f"❌ Bug 创建失败: {result}") return None def main(): try: # 1. 获取 Token print("正在获取 Token...") token = get_token() print(f"✓ Token 获取成功: {token[:10]}...") # 2. 创建 Bug 示例 # TODO: 请根据实际情况修改以下参数 product_id = 100 # 产品ID,需要在禅道中查询 title = "【测试】API 创建 Bug 示例" steps = """1. 登录系统 2. 进入某个模块 3. 执行某个操作 4. 观察结果""" print("\n正在创建 Bug...") create_bug(token, product_id, title, steps) except Exception as e: print(f"❌ 发生错误: {e}") if __name__ == "__main__": main()