#!/bin/bash # 禅道 API 命令行工具 # 使用前需要设置环境变量: # export ZENTAO_URL="http://your-zentao-domain.com" # export ZENTAO_ACCOUNT="your_username" # export ZENTAO_PASSWORD="your_password" # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # 检查依赖 check_dependencies() { if ! command -v curl &> /dev/null; then echo -e "${RED}错误: 请安装 curl${NC}" exit 1 fi if ! command -v jq &> /dev/null; then echo -e "${RED}错误: 请安装 jq${NC}" exit 1 fi } # 获取 Token get_token() { check_dependencies if [[ -z "$ZENTAO_URL" || -z "$ZENTAO_ACCOUNT" || -z "$ZENTAO_PASSWORD" ]]; then echo -e "${RED}错误: 请设置环境变量 ZENTAO_URL, ZENTAO_ACCOUNT, ZENTAO_PASSWORD${NC}" exit 1 fi local url="${ZENTAO_URL}/api.php/v1/tokens" local response=$(curl -s -X POST "$url" \ -H "Content-Type: application/json" \ -d "{\"account\":\"$ZENTAO_ACCOUNT\",\"password\":\"$ZENTAO_PASSWORD\"}") local token=$(echo "$response" | jq -r '.data.token // empty') if [[ -n "$token" ]]; then echo "$token" else echo -e "${RED}获取 Token 失败:${NC}" echo "$response" | jq . exit 1 fi } # 创建 Bug create_bug() { local token="$1" local product_id="$2" local title="$3" local steps="$4" local severity="${5:-2}" local priority="${6:-2}" local module="${7:-}" local assigned_to="${8:-}" if [[ -z "$token" || -z "$product_id" || -z "$title" || -z "$steps" ]]; then echo -e "${YELLOW}用法:${NC} $0 create_bug <steps> [severity] [priority] [module] [assigned_to]" echo "" echo "参数说明:" echo " token - API Token" echo " product_id - 产品ID" echo " title - Bug标题" echo " steps - 重现步骤" echo " severity - 严重程度 (1-4, 默认2: 一般)" echo " priority - 优先级 (1-4, 默认2: 中等)" echo " module - 模块ID (可选)" echo " assigned_to- 指派给谁 (可选)" exit 1 fi local url="${ZENTAO_URL}/api.php/v1/products/${product_id}/bugs" # 构建 JSON 数据 local data="{" data+="\"product\":${product_id}," if [[ -n "$module" ]]; then data+="\"module\":${module}," fi data+="\"title\":\"${title}\"," data+="\"steps\":\"${steps}\"," data+="\"severity\":${severity}," data+="\"priority\":${priority}" if [[ -n "$assigned_to" ]]; then data+=",\"assignedTo\":\"${assigned_to}\"" fi data+="}" local response=$(curl -s -X POST "$url" \ -H "Content-Type: application/json" \ -H "Authorization:${token}" \ -d "$data") echo "$response" | jq . } # 显示使用帮助 show_help() { echo -e "${GREEN}禅道 API 命令行工具${NC}" echo "" echo "用法: $0 <command> [arguments]" echo "" echo "命令:" echo " token 获取 API Token" echo " create_bug 创建 Bug" echo " list_bugs 列出 Bugs" echo " get_bug 获取单个 Bug" echo " update_bug 更新 Bug" echo " resolve_bug 解决 Bug" echo "" echo "示例:" echo " $0 token # 获取 Token" echo " $0 create_bug <token> 100 '测试Bug' '步骤...' # 创建 Bug" } # 主函数 main() { local command="$1" shift || true case "$command" in token) get_token ;; create_bug) create_bug "$@" ;; list_bugs|get_bug|update_bug|resolve_bug) # 其他功能待实现 echo -e "${YELLOW}功能待实现${NC}" ;; help|--help|-h) show_help ;; "") show_help ;; *) echo -e "${RED}未知命令: $command${NC}" show_help exit 1 ;; esac } main "$@"