# gitlink-cli webhook +info

查看指定 Webhook 的详细信息。

## 命令格式

```bash
gitlink-cli webhook +info [--owner OWNER] [--repo REPO] --id WEBHOOK_ID
```

## 参数说明

| 参数 | 短参数 | 说明 | 是否必须 | 默认值 |
|------|--------|------|----------|--------|
| `--owner` | `-o` | 仓库所有者 | 否 | 自动从 git remote 解析 |
| `--repo` | `-r` | 仓库名称 | 否 | 自动从 git remote 解析 |
| `--id` | `-i` | Webhook ID | **是** | - |

## 返回值

### 成功返回
```json
{
  "ok": true,
  "data": {
    "id": "456",
    "hook_url": "https://ci.example.com/webhook",
    "events": ["push", "pull_request", "issue"],
    "is_active": true,
    "content_type": "json",
    "description": "CI/CD automation webhook",
    "project": {
      "owner": "myuser",
      "repo": "myrepo",
      "identifier": "myuser/myrepo"
    },
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-15T12:30:00Z",
    "last_delivery": {
      "timestamp": "2024-01-15T14:25:00Z",
      "status": "success",
      "event": "push",
      "duration_ms": 245
    },
    "delivery_statistics": {
      "total_deliveries": 1523,
      "successful_deliveries": 1498,
      "failed_deliveries": 25,
      "success_rate": 98.36
    }
  },
  "meta": {
    "identity": "user:myuser"
  }
}
```

### 错误返回
```json
{
  "ok": false,
  "error": {
    "code": 404,
    "message": "Webhook not found",
    "suggestion": "Please check the webhook ID"
  }
}
```

## 使用示例

### 基本用法
```bash
# 查看 Webhook 详情
gitlink-cli webhook +info --owner myuser --repo myrepo --id 456

# 在 git 仓库目录中查看（自动解析 owner/repo）
gitlink-cli webhook +info --id 456

# 使用短参数
gitlink-cli webhook +info -i 456
```

### 不同输出格式
```bash
# JSON 格式（默认，便于解析）
gitlink-cli webhook +info --id 456 --format json

# Table 格式（更易阅读）
gitlink-cli webhook +info --id 456 --format table

# YAML 格式
gitlink-cli webhook +info --id 456 --format yaml
```

### 提取特定信息
```bash
# 使用 jq 提取 Webhook URL
gitlink-cli webhook +info --id 456 --format json | jq -r '.data.hook_url'

# 查看 Webhook 是否激活
gitlink-cli webhook +info --id 456 --format json | jq -r '.data.is_active'

# 查看监听的事件类型
gitlink-cli webhook +info --id 456 --format json | jq -r '.data.events[]'

# 查看统计信息
gitlink-cli webhook +info --id 456 --format json | jq '.data.delivery_statistics'
```

### 比较两个 Webhook
```bash
# 比较两个 Webhook 的配置
echo "=== Webhook 456 ==="
gitlink-cli webhook +info --id 456

echo "=== Webhook 789 ==="
gitlink-cli webhook +info --id 789
```

## 错误处理

### 常见错误

#### 1. Webhook 不存在
```bash
Error: [404] Webhook not found
```
**原因**: 指定的 Webhook ID 不存在
**解决方案**:
```bash
# 先列出所有 Webhook 找到正确 ID
gitlink-cli webhook +list
```

#### 2. 权限不足
```bash
Error: [403] You don't have permission to view webhook details
```
**原因**: 用户没有仓库访问权限
**解决方案**: 确认您是仓库成员

#### 3. ID 参数缺失
```bash
Error: required flag --id is missing
```
**原因**: 没有提供 Webhook ID
**解决方案**: 指定要查看的 Webhook ID

## 最佳实践

### 1. 更新前查看
```bash
# 更新 Webhook 前先查看当前配置
WEBHOOK_ID=456
echo "Current configuration:"
gitlink-cli webhook +info --id $WEBHOOK_ID

# 然后进行更新
gitlink-cli webhook +update --id $WEBHOOK_ID --url $NEW_URL
```

### 2. 批量查看 Webhook 信息
```bash
# 查看所有 Webhook 的简要信息
for id in $(gitlink-cli webhook +list --format json | jq -r '.data.webhooks[].id'); do
    echo "=== Webhook $id ==="
    gitlink-cli webhook +info --id $WEBHOOK_ID --format json | jq -r '.data | "\(.hook_url) - \(.description)"'
done
```

### 3. 验证 Webhook 配置
```bash
# 检查 Webhook 是否正确配置
check_webhook_config() {
    WEBHOOK_ID=$1

    # 获取 Webhook 信息
    info=$(gitlink-cli webhook +info --id $WEBHOOK_ID --format json)

    # 检查是否激活
    is_active=$(echo $info | jq -r '.data.is_active')
    if [ "$is_active" != "true" ]; then
        echo "WARNING: Webhook is not active"
    fi

    # 检查是否有事件
    events=$(echo $info | jq -r '.data.events | length')
    if [ "$events" -eq 0 ]; then
        echo "WARNING: No events configured"
    fi

    # 检查 URL 是否有效
    url=$(echo $info | jq -r '.data.hook_url')
    if [[ ! $url =~ ^https:// ]]; then
        echo "WARNING: URL does not use HTTPS"
    fi

    # 显示成功率
    success_rate=$(echo $info | jq -r '.data.delivery_statistics.success_rate')
    echo "Success rate: $success_rate%"
}
```

## AI Agent 使用建议

### 自动化 Webhook 配置检查
```bash
# AI Agent 检查 Webhook 配置的自动化脚本
analyze_webhook() {
    WEBHOOK_ID=$1
    OUTPUT_FORMAT="${2:-json}"

    info=$(gitlink-cli webhook +info --id $WEBHOOK_ID --format $OUTPUT_FORMAT)

    if [ "$OUTPUT_FORMAT" = "json" ]; then
        # JSON 格式便于解析
        echo "$info" | jq '.data | {
            id,
            url: .hook_url,
            active: .is_active,
            events: .events,
            success_rate: .delivery_statistics.success_rate,
            last_delivery: .last_delivery.timestamp
        }'
    else
        # 其他格式直接输出
        echo "$info"
    fi
}

# 批量分析所有 Webhook
for id in $(gitlink-cli webhook +list --format json | jq -r '.data.webhooks[].id'); do
    analyze_webhook $id
done
```

### Webhook 健康检查
```bash
# 检查 Webhook 健康状态
check_webhook_health() {
    WEBHOOK_ID=$1

    info=$(gitlink-cli webhook +info --id $WEBHOOK_ID --format json)

    # 提取关键指标
    is_active=$(echo $info | jq -r '.data.is_active')
    success_rate=$(echo $info | jq -r '.data.delivery_statistics.success_rate')
    last_delivery=$(echo $info | jq -r '.data.last_delivery.timestamp')

    # 健康评分
    health_score=100
    issues=()

    if [ "$is_active" != "true" ]; then
        health_score=$((health_score - 50))
        issues+=("Webhook is not active")
    fi

    if (( $(echo "$success_rate < 95" | bc -l) )); then
        health_score=$((health_score - 30))
        issues+=("Success rate below 95%: $success_rate%")
    fi

    if [ -z "$last_delivery" ] || [ "$last_delivery" = "null" ]; then
        health_score=$((health_score - 20))
        issues+=("No recent deliveries")
    fi

    # 输出结果
    echo "Webhook $WEBHOOK_ID Health Check"
    echo "Health Score: $health_score/100"
    if [ ${#issues[@]} -gt 0 ]; then
        echo "Issues found:"
        printf '%s\n' "${issues[@]}"
    else
        echo "✓ Webhook is healthy"
    fi
}
```

### 配置差异分析
```bash
# 比较两个 Webhook 的配置差异
compare_webhooks() {
    ID1=$1
    ID2=$2

    info1=$(gitlink-cli webhook +info --id $ID1 --format json)
    info2=$(gitlink-cli webhook +info --id $ID2 --format json)

    echo "=== Webhook Comparison ==="
    echo "Webhook 1: $ID1"
    echo "Webhook 2: $ID2"
    echo

    # 比较 URL
    url1=$(echo $info1 | jq -r '.data.hook_url')
    url2=$(echo $info2 | jq -r '.data.hook_url')
    echo "URL:"
    echo "  $ID1: $url1"
    echo "  $ID2: $url2"
    [ "$url1" = "$url2" ] && echo "  Status: Same" || echo "  Status: Different"
    echo

    # 比较事件
    events1=$(echo $info1 | jq -r '.data.events | sort | join(",")')
    events2=$(echo $info2 | jq -r '.data.events | sort | join(",")')
    echo "Events:"
    echo "  $ID1: $events1"
    echo "  $ID2: $events2"
    [ "$events1" = "$events2" ] && echo "  Status: Same" || echo "  Status: Different"
    echo

    # 比较激活状态
    active1=$(echo $info1 | jq -r '.data.is_active')
    active2=$(echo $info2 | jq -r '.data.is_active')
    echo "Active Status:"
    echo "  $ID1: $active1"
    echo "  $ID2: $active2"
    [ "$active1" = "$active2" ] && echo "  Status: Same" || echo "  Status: Different"
}
```

## 注意事项

1. **权限要求**: 至少需要仓库读取权限
2. **详细信息**: 包含 Webhook 的所有配置和统计信息
3. **统计数据**: 部分统计信息可能为空，特别是新创建的 Webhook
4. **时间格式**: 所有时间戳均为 ISO 8601 格式（UTC）
5. **敏感信息**: 输出可能包含敏感信息，注意保护

## 常见使用场景

### 场景1: 确认 Webhook 配置
```bash
# 确认 Webhook 配置是否正确
gitlink-cli webhook +info --id 456

# 检查关键配置
gitlink-cli webhook +info --id 456 --format json | jq -r '{
    url: .data.hook_url,
    events: .data.events,
    active: .data.is_active,
    success_rate: .data.delivery_statistics.success_rate
}'
```

### 场景2: 故障排查
```bash
# Webhook 出问题时查看详细信息
gitlink-cli webhook +info --id 456

# 检查最近一次投递情况
gitlink-cli webhook +info --id 456 --format json | jq '.data.last_delivery'

# 查看失败统计
gitlink-cli webhook +info --id 456 --format json | jq '.data.delivery_statistics'
```

### 场景3: 配置审计
```bash
# 审计所有 Webhook 配置
echo "=== Webhook Configuration Audit ==="
for id in $(gitlink-cli webhook +list --format json | jq -r '.data.webhooks[].id'); do
    echo "Webhook $id:"
    gitlink-cli webhook +info --id $id --format json | jq -r '{
        url: .data.hook_url,
        events: .data.events | join(","),
        active: .data.is_active,
        description: .description
    }'
    echo
done
```

## 相关命令

- `webhook +list` - 列出所有 Webhook
- `webhook +create` - 创建新 Webhook
- `webhook +update` - 更新 Webhook 配置
- `webhook +test` - 测试 Webhook
