GitHub REST API 开发指南
认证方式
1 2 3 4 5 6 7 8 9
| import requests
headers = { "Authorization": "Bearer ghp_your_token", "Accept": "application/vnd.github+json" }
response = requests.get('https://api.github.com/user/repos', headers=headers)
|
核心API操作
创建组织仓库
1 2 3
| curl -X POST "https://api.github.com/orgs/{org}/repos" \ -H "Authorization: Bearer ghp_your_token" \ -d '{"name":"project-x","private":true,"visibility":"internal"}'
|
管理分支保护
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| paths: /repos/{owner}/{repo}/branches/{branch}/protection: put: parameters: - name: required_pull_request_reviews in: body schema: type: object properties: dismiss_stale_reviews: type: boolean require_code_owner_reviews: type: boolean
|
Webhook安全配置
1 2 3 4 5 6 7 8 9
| const crypto = require('crypto');
function verifySignature(req) { const sig = req.headers['x-hub-signature-256']; const hmac = crypto.createHmac('sha256', 'your_webhook_secret'); hmac.update(JSON.stringify(req.body)); return `sha256=${hmac.digest('hex')}` === sig; }
|
最佳实践:使用Fine-grained PATs进行细粒度权限控制(参考GitHub Docs)