跳转至

创建仓库的WebHooks

POST /api/v5/repos/{owner}/{repo}/hooks

该接口用于为指定仓库创建 WebHooks,可设置接收事件的远程 HTTP URL、请求密码及加密类型,并配置 Push 代码、提交 Tag、创建或关闭 Issue、评论、合并请求等事件是否触发推送。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口不允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(企业、组织或个人的地址path)
repo path string 是 仓库路径(path)
access_token query string 是 用户授权码

Request Body:

请求体以 JSON(Content-Type: application/json)或表单(application/x-www-form-urlencoded)格式提交,JSON 格式对应的 Schema 如下:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "url": {
      "description": "远程HTTP URL",
      "type": "string"
    },
    "encryption_type": {
      "description": "加密类型:0 密码(X-GitCode-Token),1 签名密钥(X-GitCode-Secret);不传时默认 0",
      "type": "integer",
      "enum": [0, 1],
      "default": 0
    },
    "password": {
      "description": "请求URL时会带上该密码,防止URL被恶意请求",
      "type": "string"
    },
    "push_events": {
      "description": "Push代码到仓库",
      "type": "boolean",
      "default": false
    },
    "tag_push_events": {
      "description": "提交Tag到仓库",
      "type": "boolean",
      "default": false
    },
    "issues_events": {
      "description": "创建/关闭Issue",
      "type": "boolean",
      "default": false
    },
    "note_events": {
      "description": "评论了Issue/代码等等",
      "type": "boolean",
      "default": false
    },
    "merge_requests_events": {
      "description": "合并请求和合并后",
      "type": "boolean",
      "default": false
    }
  },
  "required": ["url"]
}

Note: 除通过 access_token 查询参数鉴权外,请求体也可携带 access_token 字段(请求体对象继承自 AuthTokenBody),服务端优先使用请求体中的令牌。

响应参数

字段 类型 必返 可为空 说明
id integer 是 否 WebHook 的 ID
url string 是 否 远程 HTTP URL
password string 是 是 请求 URL 时携带的密码
result string 是 是 最近一次推送结果,可能为 null
project_id integer 是 否 项目(仓库)ID
result_code integer 是 是 最近一次推送结果状态码,可能为 null
push_events boolean 是 否 是否推送 Push 代码事件
tag_push_events boolean 是 否 是否推送提交 Tag 事件
issues_events boolean 是 否 是否推送创建/关闭 Issue 事件
note_events boolean 是 否 是否推送评论(Issue/代码等)事件
merge_requests_events boolean 是 否 是否推送合并请求(含合并后)事件
created_at string 是 否 创建时间
active boolean 是 是 是否启用,可能为 null

失败响应

失败响应的错误字段以 ApiErrorResp 结构返回(字段名为下划线风格:error_code / error_code_name / error_message)。

400 参数错误

{
  "error_code": 400,
  "error_code_name": "BAD_REQUEST",
  "error_message": "加密类型错误"
}

401 未认证

{
  "error_code": 401,
  "error_code_name": "BadCredentials",
  "error_message": "401, token not found"
}

403 禁止访问

{
  "error_code": 403,
  "error_code_name": "FORBIDDEN",
  "error_message": "no scopes:all_hook"
}

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad Request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 资源不存在(Not found)
429 请求过于频繁,触发限流(Rate limited)
500 系统内部错误(Internal server error)

请求示例

参数说明

<owner> 替换为仓库所属空间地址 path(企业、组织或个人的地址 path); <repo> 替换为仓库路径(path); <token> 替换为你的 AtomGit 用户访问令牌。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/hooks?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://example.com/webhook",
  "encryption_type": 0,
  "password": "123456",
  "push_events": true,
  "tag_push_events": false,
  "issues_events": true,
  "note_events": false,
  "merge_requests_events": true
}'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const owner = "<owner>";
const repo = "<repo>";
const token = "<token>";
const url = `https://api.atomgit.com/api/v5/repos/${owner}/${repo}/hooks?access_token=${token}`;

const response = await fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
  body: JSON.stringify({
    url: "https://example.com/webhook",
    encryption_type: 0,
    password: "123456",
    push_events: true,
    tag_push_events: false,
    issues_events: true,
    note_events: false,
    merge_requests_events: true
  })
});

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

owner = "<owner>"
repo = "<repo>"
token = "<token>"

resp = requests.post(
    f"https://api.atomgit.com/api/v5/repos/{owner}/{repo}/hooks",
    params={"access_token": token},
    json={
        "url": "https://example.com/webhook",
        "encryption_type": 0,
        "password": "123456",
        "push_events": True,
        "tag_push_events": False,
        "issues_events": True,
        "note_events": False,
        "merge_requests_events": True,
    },
    headers={"Accept": "application/json"},
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "id": 9529,
  "url": "https://example.com/webhook",
  "password": "123456",
  "result": null,
  "project_id": 282463,
  "result_code": 0,
  "push_events": true,
  "tag_push_events": false,
  "issues_events": true,
  "note_events": false,
  "merge_requests_events": true,
  "created_at": "2024-09-26T16:13:27+08:00",
  "active": true
}

错误码

错误码 说明
400 参数错误或请求无法解析
401 未认证或凭证无效(缺少/无效的访问凭证)
403 无权限(缺少所需的令牌 scope,如 all_hook)
404 请求路径不存在
429 请求过于频繁,触发限流
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc