gitlink-cli/skills/gitlink-webhook/references/webhook-delete.md

283 lines
6.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# gitlink-cli webhook +delete
删除指定的 Webhook。
## 命令格式
```bash
gitlink-cli webhook +delete [--owner OWNER] [--repo REPO] --id WEBHOOK_ID
```
## 参数说明
| 参数 | 短参数 | 说明 | 是否必须 | 默认值 |
|------|--------|------|----------|--------|
| `--owner` | `-o` | 仓库所有者 | 否 | 自动从 git remote 解析 |
| `--repo` | `-r` | 仓库名称 | 否 | 自动从 git remote 解析 |
| `--id` | `-i` | Webhook ID | **是** | - |
## 返回值
### 成功返回
```json
{
"ok": true,
"data": {
"message": "Webhook deleted successfully"
},
"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 +delete --owner myuser --repo myrepo --id 456
# 在 git 仓库目录中删除(自动解析 owner/repo
gitlink-cli webhook +delete --id 456
# 使用短参数
gitlink-cli webhook +delete -i 456
```
### 删除多个 Webhook
```bash
# 批量删除多个 Webhook
for id in 123 456 789; do
gitlink-cli webhook +delete --id $id
done
```
### 交互式删除
```bash
# 先查看 Webhook 详情确认
gitlink-cli webhook +info --id 456
# 确认后删除
gitlink-cli webhook +delete --id 456
```
## 错误处理
### 常见错误
#### 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 delete webhooks
```
**原因**: 用户不是仓库管理员
**解决方案**: 确认您有仓库管理员权限
#### 3. ID 参数缺失
```bash
Error: required flag --id is missing
```
**原因**: 没有提供 Webhook ID
**解决方案**: 指定要删除的 Webhook ID
## 最佳实践
### 1. 删除前确认
```bash
# 删除前先查看 Webhook 详情
WEBHOOK_ID=456
echo "About to delete webhook:"
gitlink-cli webhook +info --id $WEBHOOK_ID
# 确认后删除
read -p "Confirm deletion? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
gitlink-cli webhook +delete --id $WEBHOOK_ID
fi
```
### 2. 记录删除的 Webhook
```bash
# 删除前记录 Webhook 配置
WEBHOOK_ID=456
BACKUP_FILE="webhook_backup_$WEBHOOK_ID.json"
gitlink-cli webhook +info --id $WEBHOOK_ID --format json > $BACKUP_FILE
echo "Webhook config backed up to $BACKUP_FILE"
# 然后删除
gitlink-cli webhook +delete --id $WEBHOOK_ID
```
### 3. 批量清理不活跃的 Webhook
```bash
# 列出所有不活跃的 Webhook 并删除
inactive_webhooks=$(gitlink-cli webhook +list --format json | jq -r '.data.webhooks[] | select(.is_active == false) | .id')
for id in $inactive_webhooks; do
echo "Deleting inactive webhook: $id"
gitlink-cli webhook +delete --id $id
done
```
## AI Agent 使用建议
### 安全删除流程
```bash
# AI Agent 删除 Webhook 的安全流程
delete_webhook_safely() {
WEBHOOK_ID=$1
# 1. 检查 Webhook 是否存在
if ! gitlink-cli webhook +info --id $WEBHOOK_ID --format json >/dev/null 2>&1; then
echo "Webhook $WEBHOOK_ID not found"
return 1
fi
# 2. 备份配置
gitlink-cli webhook +info --id $WEBHOOK_ID --format json > "webhook_backup_$WEBHOOK_ID.json"
# 3. 删除 Webhook
if gitlink-cli webhook +delete --id $WEBHOOK_ID; then
echo "Webhook $WEBHOOK_ID deleted successfully"
return 0
else
echo "Failed to delete webhook $WEBHOOK_ID"
return 1
fi
}
```
### 批量删除 Webhook
```bash
# 删除所有匹配特定条件的 Webhook
delete_webhooks_by_url() {
URL_PATTERN=$1
# 找到匹配的 Webhook
webhook_ids=$(gitlink-cli webhook +list --format json | \
jq -r ".data.webhooks[] | select(.hook_url | contains(\"$URL_PATTERN\")) | .id")
# 逐个删除
for id in $webhook_ids; do
echo "Deleting webhook $id with URL matching $URL_PATTERN"
gitlink-cli webhook +delete --id $id
done
}
# 使用示例:删除所有指向旧服务器的 Webhook
delete_webhooks_by_url "old-server.example.com"
```
### 验证删除
```bash
# 删除 Webhook 并验证
WEBHOOK_ID=456
# 删除前检查
if gitlink-cli webhook +info --id $WEBHOOK_ID >/dev/null 2>&1; then
echo "Webhook exists, deleting..."
gitlink-cli webhook +delete --id $WEBHOOK_ID
# 验证删除成功
if ! gitlink-cli webhook +info --id $WEBHOOK_ID >/dev/null 2>&1; then
echo "Webhook deleted successfully"
else
echo "Webhook still exists after deletion"
fi
else
echo "Webhook not found"
fi
```
## 注意事项
1. **不可恢复**: 删除操作不可逆,请谨慎操作
2. **立即生效**: 删除后立即停止接收事件
3. **权限要求**: 需要仓库管理员权限
4. **API 特性**: GitLink API 在删除时可能返回错误信息,但实际删除成功
5. **验证删除**: 建议删除后验证 Webhook 是否已删除
## 常见使用场景
### 场景1: 清理测试 Webhook
```bash
# 删除所有测试环境的 Webhook
test_webhooks=$(gitlink-cli webhook +list --format json | \
jq -r '.data.webhooks[] | select(.description | contains("test")) | .id')
for id in $test_webhooks; do
echo "Deleting test webhook: $id"
gitlink-cli webhook +delete --id $id
done
```
### 场景2: 迁移到新 URL
```bash
# 迁移 Webhook 到新 URL
OLD_WEBHOOK_ID=456
OLD_URL=$(gitlink-cli webhook +info --id $OLD_WEBHOOK_ID --format json | jq -r '.data.hook_url')
NEW_URL="https://new-server.example.com/webhook"
# 创建新 Webhook
NEW_WEBHOOK_ID=$(gitlink-cli webhook +create --url $NEW_URL --events push --format json | jq -r '.data.id')
# 测试新 Webhook
gitlink-cli webhook +test --id $NEW_WEBHOOK_ID
# 确认新 Webhook 工作后删除旧 Webhook
gitlink-cli webhook +delete --id $OLD_WEBHOOK_ID
```
### 场景3: 批量重构 Webhook
```bash
# 重构所有 Webhook重新创建后删除旧的
# 1. 备份现有配置
gitlink-cli webhook +list --format json > webhook_config_backup.json
# 2. 根据备份创建新配置(可能使用不同的 URL 或事件)
# 3. 删除旧的 Webhook
old_ids=$(jq -r '.data.webhooks[].id' webhook_config_backup.json)
for id in $old_ids; do
gitlink-cli webhook +delete --id $id
done
```
## 安全建议
1. **删除前备份**: 删除前备份 Webhook 配置
2. **确认操作**: 删除前确认 Webhook ID 和配置
3. **逐步删除**: 批量删除时逐步进行,避免误删
4. **验证删除**: 删除后验证 Webhook 已被删除
5. **权限控制**: 限制删除权限给授权用户
## 相关命令
- `webhook +list` - 列出所有 Webhook
- `webhook +info` - 查看 Webhook 详情
- `webhook +create` - 创建新 Webhook
- `webhook +update` - 更新 Webhook可以先用 `--active false` 停用)