taskmanager/.claude/commands/gitea/API-QUICK-REFERENCE.md
bojunC 38c5a466cd feat: 实现 Issue #1 - 项目初始化和环境配置
- 创建 monorepo 结构(Turborepo)
- 初始化前端项目(Next.js + TypeScript + Tailwind CSS)
- 初始化后端项目(NestJS + TypeScript)
- 配置开发工具(ESLint, Prettier, VS Code)
- 创建项目文档(README, 开发规范)

Closes #1
2026-03-19 16:14:26 +08:00

522 lines
12 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

# Gitea API 快速参考
**文档版本**: 1.1 | **更新日期**: 2026-03-19 | **验证状态**: ✅ 100% 通过
---
## 🔑 快速配置
### 环境变量(.env
```bash
# 必需配置
GITEA_URL=http://192.168.2.200:3000 # Gitea 实例地址HTTP/HTTPS 格式)
GITEA_TOKEN=your-api-token-here # 需要 write:user, write:repository, write:issue
GITEA_OWNER=your-username # 用户名或组织名
# 加载配置
export $(cat .env | grep -v '^#' | xargs)
```
### 通用请求头
```bash
# 所有 API 请求都需要的请求头
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json"
```
### 验证配置
```bash
# 测试连接是否正常
curl -s -X GET "${GITEA_URL}/api/v1/user" \
-H "Authorization: token ${GITEA_TOKEN}" | jq '.login'
```
---
## 📦 仓库操作
### 创建仓库
```bash
# API 端点
POST /api/v1/user/repos
# 示例
curl -X POST "${GITEA_URL}/api/v1/user/repos" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"name": "my-repo",
"description": "仓库描述",
"private": false,
"auto_init": true,
"default_branch": "main"
}'
# 状态码: 201 Created
```
### 获取仓库
```bash
# API 端点
GET /api/v1/repos/{owner}/{repo}
# 示例
curl -X GET "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo" \
-H "Authorization: token ${GITEA_TOKEN}"
# 状态码: 200 OK
```
### 列出仓库
```bash
GET /api/v1/user/repos?limit=50
```
### 删除仓库
```bash
DELETE /api/v1/repos/{owner}/{repo}
```
---
## 🐛 Issue 操作
### 创建 Issue
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/issues
# 示例
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/issues" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"title": "实现新功能",
"body": "## 功能描述\n\n详细说明支持 Markdown"
}'
# 状态码: 201 Created
```
### 列出 Issues
```bash
GET /api/v1/repos/{owner}/{repo}/issues?state=open
```
### 更新 Issue
```bash
PATCH /api/v1/repos/{owner}/{repo}/issues/{index}
{
"title": "新标题",
"state": "closed"
}
```
### 添加评论
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/issues/{index}/comments
# 示例
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/issues/1/comments" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"body": "这是一条评论(支持 **Markdown**"}'
# 状态码: 201 Created
```
### 关闭 Issue
```bash
# API 端点
PATCH /api/v1/repos/{owner}/{repo}/issues/{index}
# 示例
curl -X PATCH "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/issues/1" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"state": "closed"}'
# 状态码: 201 Created
```
---
## 🌿 分支操作
### 创建分支
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/branches
# 示例
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/branches" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"new_branch_name": "feature/new-feature",
"old_branch_name": "main"
}'
# 状态码: 201 Created
```
### 删除分支
```bash
DELETE /api/v1/repos/{owner}/{repo}/branches/{branch}
```
---
## 🔀 Pull Request 操作
### 创建 PR
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/pulls
# 示例
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/pulls" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"title": "实现新功能",
"body": "Closes #1\n\n## 实现说明\n\n详细描述...",
"head": "feature/new-feature",
"base": "main"
}'
# 状态码: 201 Created
```
### 创建 PR 审查
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/pulls/{index}/reviews
# 示例(批准)
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/pulls/1/reviews" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"event": "APPROVE",
"body": "✅ 代码审查通过"
}'
# 状态码: 200 OK
```
**⚠️ 重要说明**:
- ✅ 审查创建时已立即生效(有 `submitted_at` 时间戳)
- 返回的 `state` 字段可能显示为 `PENDING`,但审查已生效
- ✅ 不影响 PR 合并,一步创建即可
- 📖 详见: `docs/PR-REVIEW-SOLUTION.md`
### 合并 PR
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/pulls/{index}/merge
# 示例
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/pulls/1/merge" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"Do": "merge",
"merge_message": "合并 PR #1 - 实现新功能"
}'
# 状态码: 200 OK
# Do 选项: merge合并提交, squash压缩合并, rebase变基合并
```
---
## 📄 文件操作
### 创建文件
```bash
# API 端点
POST /api/v1/repos/{owner}/{repo}/contents/{filepath}
# 示例
# 1. Base64 编码文件内容
CONTENT=$(echo "# My File\n\nContent here..." | base64 | tr -d '\n')
# 2. 创建文件
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/contents/README.md" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"message\": \"docs: Add README\",
\"content\": \"${CONTENT}\",
\"branch\": \"main\"
}"
# 状态码: 201 Created
```
### 读取文件
```bash
# API 端点
GET /api/v1/repos/{owner}/{repo}/contents/{filepath}?ref={branch}
# 示例
curl -X GET "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/contents/README.md?ref=main" \
-H "Authorization: token ${GITEA_TOKEN}"
# 状态码: 200 OK
# 返回内容为 base64 编码
```
### 更新文件
```bash
# API 端点
PUT /api/v1/repos/{owner}/{repo}/contents/{filepath}
# 示例
# 1. 获取文件 SHA
SHA=$(curl -s -X GET "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/contents/README.md" \
-H "Authorization: token ${GITEA_TOKEN}" | jq -r '.sha')
# 2. 更新文件
NEW_CONTENT=$(echo "# Updated" | base64 | tr -d '\n')
curl -X PUT "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/my-repo/contents/README.md" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"message\": \"docs: Update README\",
\"content\": \"${NEW_CONTENT}\",
\"sha\": \"${SHA}\",
\"branch\": \"main\"
}"
# 状态码: 200 OK
```
### 删除文件
```bash
DELETE /api/v1/repos/{owner}/{repo}/contents/{filepath}
{
"message": "删除信息",
"sha": "文件SHA",
"branch": "main"
}
```
---
## 💡 完整工作流示例
### 创建仓库 + Issue + PR + 合并
```bash
#!/bin/bash
# 完整工作流示例
# 1. 加载环境变量
export $(cat .env | grep -v '^#' | xargs)
# 2. 创建仓库
curl -X POST "${GITEA_URL}/api/v1/user/repos" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"name":"demo","auto_init":true}'
# 3. 创建 Issue
ISSUE=$(curl -s -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/issues" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"title":"实现功能","body":"描述"}')
ISSUE_NUM=$(echo $ISSUE | jq -r '.number')
# 4. 创建分支
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/branches" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"new_branch_name":"feature/demo"}'
# 5. 添加文件
CONTENT=$(echo "# Demo" | base64 | tr -d '\n')
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/contents/test.md" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"message\":\"Add file\",\"content\":\"${CONTENT}\",\"branch\":\"feature/demo\"}"
# 6. 创建 PR
PR=$(curl -s -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/pulls" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"title\":\"实现功能\",\"body\":\"Closes #${ISSUE_NUM}\",\"head\":\"feature/demo\",\"base\":\"main\"}")
PR_NUM=$(echo $PR | jq -r '.number')
# 7. 创建审查
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/pulls/${PR_NUM}/reviews" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"event":"APPROVE","body":"审查通过"}'
# 8. 合并 PR
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/pulls/${PR_NUM}/merge" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"Do":"merge"}'
# 9. 关闭 Issue
curl -X PATCH "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/demo/issues/${ISSUE_NUM}" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"state":"closed"}'
echo "✅ 工作流完成!"
```
---
## 🎯 快速命令参考
### 最常用命令
### 快速创建仓库
```bash
curl -X POST "${GITEA_URL}/api/v1/user/repos" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"name":"test","auto_init":true}'
```
### 快速创建 Issue
```bash
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/repo/issues" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"title":"Bug report","body":"Description"}'
```
### 快速创建分支
```bash
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/repo/branches" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"new_branch_name":"feature/new"}'
```
### 快速创建 PR
```bash
curl -X POST "${GITEA_URL}/api/v1/repos/${GITEA_OWNER}/repo/pulls" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"title":"New feature","head":"feature/new","base":"main"}'
```
---
## 📊 HTTP 状态码
| 状态码 | 说明 |
|--------|------|
| 200 | 成功 |
| 201 | 创建成功 |
| 204 | 删除成功(无内容) |
| 401 | 未授权 |
| 403 | 权限不足 |
| 404 | 资源不存在 |
| 409 | 冲突 |
| 422 | 参数错误 |
---
## 🔧 辅助函数
### Bash 辅助函数
```bash
# 通用 API 调用函数
gitea_api() {
curl -s -X $1 "${GITEA_URL}/api/v1$2" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
${3:+-d "$3"}
}
# 使用示例
gitea_api GET "/user/repos"
gitea_api POST "/user/repos" '{"name":"test"}'
gitea_api PATCH "/repos/owner/repo/issues/1" '{"state":"closed"}'
gitea_api DELETE "/repos/owner/repo"
```
### Base64 编码辅助
```bash
# 文件内容编码
encode_content() {
echo "$1" | base64 | tr -d '\n'
}
# 使用示例
CONTENT=$(encode_content "# My File\n\nContent here...")
```
---
## 🎓 最佳实践
1. **使用环境变量** - 不要硬编码 Token
2. **添加错误处理** - 检查 HTTP 状态码
3. **实现幂等性** - 检查资源是否存在
4. **添加日志** - 记录操作历史
5. **控制速率** - 避免请求过快
---
## ✅ API 验证状态
**最后验证**: 2026-03-19
| 功能类别 | 测试数 | 通过率 | 状态码 |
|---------|--------|--------|--------|
| 仓库操作 | 2 | 100% | 200, 201 |
| Issue 操作 | 3 | 100% | 201 |
| 分支操作 | 1 | 100% | 201 |
| 文件操作 | 3 | 100% | 200, 201 |
| Pull Request | 3 | 100% | 200, 201 |
| **总计** | **12** | **100%** | ✅ |
**关键发现**:
- ✅ 所有 API 端点已验证通过
- ✅ PR 审查功能正常PENDING 状态不影响功能)
- ✅ 可以放心使用
---
## 🎓 最佳实践
1. **使用环境变量** - 不要硬编码 Token
2. **添加错误处理** - 检查 HTTP 状态码
3. **实现幂等性** - 检查资源是否存在
4. **添加日志** - 记录操作历史
5. **控制速率** - 避免请求过快(添加 `sleep 1`
6. **验证配置** - 首次使用前验证连接
---
## 📚 完整文档
- **API 操作指南**: `docs/API-OPERATIONS-GUIDE.md` - 详细说明和示例
- **API 验证报告**: `docs/API-VALIDATION-TEST-REPORT.md` - 完整测试结果
- **PR 审查说明**: `docs/PR-REVIEW-SOLUTION.md` - PR 审查问题解决
- **示例脚本**: `examples/complete-workflow.sh` - 完整工作流示例
- **配置指南**: `docs/CONFIGURATION.md` - 环境变量配置
---
**快速参考版本**: 1.1
**最后更新**: 2026-03-19
**文档维护**: Claude Code
**验证状态**: ✅ 100% 通过