Skip to main content

此版本的 GitHub Enterprise Server 已于以下日期停止服务 2024-07-09. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

REST API 现已经过版本控制。 有关详细信息,请参阅“关于 API 版本控制”。

适用于预接收挂钩的 REST API 终结点

使用 REST API 创建、列出、更新和删除预接收挂钩。

关于预接收挂钩

这些终结点仅适用于经过身份验证的网站管理员。 普通用户将收到 404 响应。

对象属性

预接收挂钩

名称Type说明
namestring挂钩的名称。
scriptstring挂钩运行的脚本。
script_repositoryobject保存脚本的 GitHub 仓库。
environmentobject执行脚本的预接收环境。
enforcementstring此挂钩的实施状态。
allow_downstream_configurationboolean是否可以在组织或仓库级别上覆盖实施。

强制执行的可能值为 enableddisabledtestingdisabled 表示预接收挂钩不会运行。 enabled 表示它将运行并拒绝任何导致非零状态的推送。 testing 表示脚本将运行但不会导致任何推送被拒绝。

List pre-receive hooks

“List pre-receive hooks”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

查询参数
名称, 类型, 说明
per_page integer

The number of results per page (max 100). For more information, see "Using pagination in the REST API."

默认: 30

page integer

The page number of the results to fetch. For more information, see "Using pagination in the REST API."

默认: 1

direction string

The direction to sort the results by.

默认: desc

可以是以下选项之一: asc, desc

sort string

The property to sort the results by.

默认: created

可以是以下选项之一: created, updated, name

“List pre-receive hooks”的 HTTP 响应状态代码

状态代码说明
200

OK

“List pre-receive hooks”的示例代码

请求示例

get/admin/pre-receive-hooks
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/admin/pre-receive-hooks

Response

Status: 200
[ { "id": 1, "name": "Check Commits", "enforcement": "disabled", "script": "scripts/commmit_check.sh", "script_repository": { "id": 595, "full_name": "DevIT/hooks", "url": "https://github.example.com/api/v3/repos/DevIT/hooks", "html_url": "https://github.example.com/DevIT/hooks" }, "environment": { "id": 2, "name": "DevTools Hook Env", "image_url": "https://my_file_server/path/to/devtools_env.tar.gz", "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2", "html_url": "https://github.example.com/admin/pre-receive-environments/2", "default_environment": false, "created_at": "2016-05-20T11:35:45-05:00", "hooks_count": 1, "download": { "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2/downloads/latest", "state": "success", "downloaded_at": "2016-05-26T07:42:53-05:00", "message": null } }, "allow_downstream_configuration": false } ]

Create a pre-receive hook

“Create a pre-receive hook”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

正文参数
名称, 类型, 说明
name string 必须

The name of the hook.

script string 必须

The script that the hook runs.

script_repository object 必须

The GitHub repository where the script is kept.

environment object 必须

The pre-receive environment where the script is executed.

enforcement string

The state of enforcement for this hook. default: disabled

allow_downstream_configuration boolean

Whether enforcement can be overridden at the org or repo level. default: false

“Create a pre-receive hook”的 HTTP 响应状态代码

状态代码说明
201

Created

“Create a pre-receive hook”的示例代码

请求示例

post/admin/pre-receive-hooks
curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/admin/pre-receive-hooks \ -d '{"name":"Check Commits","script":"scripts/commit_check.sh","enforcement":"disabled","allow_downstream_configuration":false,"script_repository":{"full_name":"DevIT/hooks"},"environment":{"id":2}}'

Response

Status: 201
{ "id": 1, "name": "Check Commits", "enforcement": "disabled", "script": "scripts/commmit_check.sh", "script_repository": { "id": 595, "full_name": "DevIT/hooks", "url": "https://github.example.com/api/v3/repos/DevIT/hooks", "html_url": "https://github.example.com/DevIT/hooks" }, "environment": { "id": 2, "name": "DevTools Hook Env", "image_url": "https://my_file_server/path/to/devtools_env.tar.gz", "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2", "html_url": "https://github.example.com/admin/pre-receive-environments/2", "default_environment": false, "created_at": "2016-05-20T11:35:45-05:00", "hooks_count": 1, "download": { "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2/downloads/latest", "state": "success", "downloaded_at": "2016-05-26T07:42:53-05:00", "message": null } }, "allow_downstream_configuration": false }

Get a pre-receive hook

“Get a pre-receive hook”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
pre_receive_hook_id integer 必须

The unique identifier of the pre-receive hook.

“Get a pre-receive hook”的 HTTP 响应状态代码

状态代码说明
200

OK

“Get a pre-receive hook”的示例代码

请求示例

get/admin/pre-receive-hooks/{pre_receive_hook_id}
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/admin/pre-receive-hooks/PRE_RECEIVE_HOOK_ID

Response

Status: 200
{ "id": 1, "name": "Check Commits", "enforcement": "disabled", "script": "scripts/commmit_check.sh", "script_repository": { "id": 595, "full_name": "DevIT/hooks", "url": "https://github.example.com/api/v3/repos/DevIT/hooks", "html_url": "https://github.example.com/DevIT/hooks" }, "environment": { "id": 2, "name": "DevTools Hook Env", "image_url": "https://my_file_server/path/to/devtools_env.tar.gz", "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2", "html_url": "https://github.example.com/admin/pre-receive-environments/2", "default_environment": false, "created_at": "2016-05-20T11:35:45-05:00", "hooks_count": 1, "download": { "url": "https://github.example.com/api/v3/admin/pre-receive-environments/2/downloads/latest", "state": "success", "downloaded_at": "2016-05-26T07:42:53-05:00", "message": null } }, "allow_downstream_configuration": false }

Update a pre-receive hook

“Update a pre-receive hook”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
pre_receive_hook_id integer 必须

The unique identifier of the pre-receive hook.

正文参数
名称, 类型, 说明
name string

The name of the hook.

script string

The script that the hook runs.

script_repository object

The GitHub repository where the script is kept.

environment object

The pre-receive environment where the script is executed.

enforcement string

The state of enforcement for this hook.

allow_downstream_configuration boolean

Whether enforcement can be overridden at the org or repo level.

“Update a pre-receive hook”的 HTTP 响应状态代码

状态代码说明
200

OK

“Update a pre-receive hook”的示例代码

请求示例

patch/admin/pre-receive-hooks/{pre_receive_hook_id}
curl -L \ -X PATCH \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/admin/pre-receive-hooks/PRE_RECEIVE_HOOK_ID \ -d '{"name":"Check Commits","environment":{"id":1},"allow_downstream_configuration":true}'

Response

Status: 200
{ "id": 1, "name": "Check Commits", "enforcement": "disabled", "script": "scripts/commmit_check.sh", "script_repository": { "id": 595, "full_name": "DevIT/hooks", "url": "https://github.example.com/api/v3/repos/DevIT/hooks", "html_url": "https://github.example.com/DevIT/hooks" }, "environment": { "id": 1, "name": "Default", "image_url": "githubenterprise://internal", "url": "https://github.example.com/api/v3/admin/pre-receive-environments/1", "html_url": "https://github.example.com/admin/pre-receive-environments/1", "default_environment": true, "created_at": "2016-05-20T11:35:45-05:00", "hooks_count": 1, "download": { "url": "https://github.example.com/api/v3/admin/pre-receive-environments/1/downloads/latest", "state": "success", "downloaded_at": "2016-05-26T07:42:53-05:00", "message": null } }, "allow_downstream_configuration": true }

Delete a pre-receive hook

“Delete a pre-receive hook”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
pre_receive_hook_id integer 必须

The unique identifier of the pre-receive hook.

“Delete a pre-receive hook”的 HTTP 响应状态代码

状态代码说明
204

No Content

“Delete a pre-receive hook”的示例代码

请求示例

delete/admin/pre-receive-hooks/{pre_receive_hook_id}
curl -L \ -X DELETE \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/admin/pre-receive-hooks/PRE_RECEIVE_HOOK_ID

Response

Status: 204