Help us learn about your current experience with the documentation. Take the survey.

系统钩子 API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab Self-Managed

所有方法都需要管理员授权。

您可以从 GitLab 用户界面配置系统钩子的 URL 端点:

  1. 在左侧边栏底部,选择 管理员
  2. 选择 系统钩子 (/admin/hooks)。

了解更多关于 系统钩子 的信息。

列出系统钩子

获取所有系统钩子的列表。

GET /hooks

示例请求:

curl --request GET \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/hooks"

示例响应:

[
  {
    "id":1,
    "url":"https://gitlab.example.com/hook",
    "name": "Hook name",
    "description": "Hook description",
    "created_at":"2016-10-31T12:32:15.192Z",
    "push_events":true,
    "tag_push_events":false,
    "merge_requests_events": true,
    "repository_update_events": true,
    "enable_ssl_verification":true,
    "url_variables": []
  }
]

获取系统钩子

根据 ID 获取系统钩子。

GET /hooks/:id
Attribute Type Required Description
id integer 钩子的 ID

示例请求:

curl --request GET \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/hooks/1"

示例响应:

{
  "id": 1,
  "url": "https://gitlab.example.com/hook",
  "name": "Hook name",
  "description": "Hook description",
  "created_at": "2016-10-31T12:32:15.192Z",
  "push_events": true,
  "tag_push_events": false,
  "merge_requests_events": true,
  "repository_update_events": true,
  "enable_ssl_verification": true,
  "url_variables": []
}

添加新系统钩子

添加新的系统钩子。

POST /hooks
Attribute Type Required Description
url string 钩子 URL
name string 钩子名称(在 GitLab 17.1 中 引入
description string 钩子描述(在 GitLab 17.1 中 引入
token string 用于验证接收到的负载的密钥;此值不会在响应中返回
push_events boolean 为 true 时,钩子在推送事件时触发
tag_push_events boolean 为 true 时,钩子在推送新标签时触发
merge_requests_events boolean 在合并请求事件时触发钩子
repository_update_events boolean 在仓库更新事件时触发钩子
enable_ssl_verification boolean 触发钩子时进行 SSL 验证
push_events_branch_filter string 仅在匹配分支的推送事件时触发钩子
branch_filter_strategy string 按分支过滤推送事件。可能的值为 wildcard(默认)、regexall_branches

示例请求:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/hooks?url=https://gitlab.example.com/hook"

示例响应:

[
  {
    "id":1,
    "url":"https://gitlab.example.com/hook",
    "name": "Hook name",
    "description": "Hook description",
    "created_at":"2016-10-31T12:32:15.192Z",
    "push_events":true,
    "tag_push_events":false,
    "merge_requests_events": true,
    "repository_update_events": true,
    "enable_ssl_verification":true,
    "url_variables": []
  }
]

更新系统钩子

更新现有的系统钩子。

PUT /hooks/:hook_id
Attribute Type Required Description
hook_id integer 系统钩子的 ID
url string 钩子 URL
token string 用于验证接收到的负载的密钥;此值不会在响应中返回
push_events boolean 为 true 时,钩子在推送事件时触发
tag_push_events boolean 为 true 时,钩子在推送新标签时触发
merge_requests_events boolean 在合并请求事件时触发钩子
repository_update_events boolean 在仓库更新事件时触发钩子
enable_ssl_verification boolean 触发钩子时进行 SSL 验证
push_events_branch_filter string 仅在匹配分支的推送事件时触发钩子
branch_filter_strategy string 按分支过滤推送事件。可能的值为 wildcard(默认)、regexall_branches

测试系统钩子

使用模拟数据执行系统钩子。

POST /hooks/:id
Attribute Type Required Description
id integer 钩子的 ID

示例请求:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/hooks/1"

响应始终是模拟数据:

{
   "project_id" : 1,
   "owner_email" : "example@gitlabhq.com",
   "owner_name" : "Someone",
   "name" : "Ruby",
   "path" : "ruby",
   "event_name" : "project_create"
}

删除系统钩子

删除系统钩子。

DELETE /hooks/:id
Attribute Type Required Description
id integer 钩子的 ID

示例请求:

curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/hooks/2"

设置 URL 变量

PUT /hooks/:hook_id/url_variables/:key

支持的属性:

Attribute Type Required Description
hook_id integer 系统钩子的 ID。
key string URL 变量的键。
value string URL 变量的值。

成功时,此端点返回响应代码 204 No Content

删除 URL 变量

DELETE /hooks/:hook_id/url_variables/:key

支持的属性:

Attribute Type Required Description
hook_id integer 系统钩子的 ID。
key string URL 变量的键。

成功时,此端点返回响应代码 204 No Content