此版本的 GitHub Enterprise Server 已于以下日期停止服务 2024-12-19. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持。
适用于预接收挂钩的 REST API 终结点
使用 REST API 创建、列出、更新和删除预接收挂钩。
关于预接收挂钩
这些终结点仅适用于经过身份验证的网站管理员。 普通用户将收到 404 响应。
Note
这些终结点仅支持使用 personal access token (classic) 进行身份验证。 有关详细信息,请参阅“管理个人访问令牌”。
对象属性
预接收挂钩
| 名称 | Type | 说明 | 
|---|---|---|
name | string | 挂钩的名称。 | 
script | string | 挂钩运行的脚本。 | 
script_repository | object | 保存脚本的 GitHub 仓库。 | 
environment | object | 执行脚本的预接收环境。 | 
enforcement | string | 此挂钩的实施状态。 | 
allow_downstream_configuration | boolean | 是否可以在组织或仓库级别上覆盖实施。 | 
强制执行的可能值为 enabled、disabled 和 testing。 disabled 表示预接收挂钩不会运行。 enabled 表示它将运行并拒绝任何导致非零状态的推送。 testing 表示脚本将运行但不会导致任何推送被拒绝。
List pre-receive hooks
“List pre-receive hooks”的细粒度访问令牌
此终结点不适用于 GitHub 应用程序用户访问令牌、GitHub 应用程序安装访问令牌或细粒度个人访问令牌。
“List pre-receive hooks”的参数
| 名称, 类型, 说明 | 
|---|
accept string Setting to   | 
| 名称, 类型, 说明 | 
|---|
per_page integer The number of results per page (max 100). For more information, see "Using pagination in the REST API." 默认:   | 
page integer The page number of the results to fetch. For more information, see "Using pagination in the REST API." 默认:   | 
direction string The direction to sort the results by. 默认:  可以是以下选项之一:   | 
sort string The property to sort the results by. 默认:  可以是以下选项之一:   | 
“List pre-receive hooks”的 HTTP 响应状态代码
| 状态代码 | 说明 | 
|---|---|
200 | OK  | 
“List 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-hooksResponse
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”的细粒度访问令牌
此终结点不适用于 GitHub 应用程序用户访问令牌、GitHub 应用程序安装访问令牌或细粒度个人访问令牌。
“Create a pre-receive hook”的参数
| 名称, 类型, 说明 | 
|---|
accept string Setting to   | 
| 名称, 类型, 说明 | 
|---|
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:   | 
allow_downstream_configuration boolean Whether enforcement can be overridden at the org or repo level. default:   | 
“Create a pre-receive hook”的 HTTP 响应状态代码
| 状态代码 | 说明 | 
|---|---|
201 | Created  | 
“Create a pre-receive hook”的示例代码
请求示例
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”的细粒度访问令牌
此终结点不适用于 GitHub 应用程序用户访问令牌、GitHub 应用程序安装访问令牌或细粒度个人访问令牌。
“Get a pre-receive hook”的参数
| 名称, 类型, 说明 | 
|---|
accept string Setting to   | 
| 名称, 类型, 说明 | 
|---|
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”的示例代码
请求示例
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_IDResponse
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”的细粒度访问令牌
此终结点不适用于 GitHub 应用程序用户访问令牌、GitHub 应用程序安装访问令牌或细粒度个人访问令牌。
“Update a pre-receive hook”的参数
| 名称, 类型, 说明 | 
|---|
accept string Setting to   | 
| 名称, 类型, 说明 | 
|---|
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”的示例代码
请求示例
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”的细粒度访问令牌
此终结点不适用于 GitHub 应用程序用户访问令牌、GitHub 应用程序安装访问令牌或细粒度个人访问令牌。
“Delete a pre-receive hook”的参数
| 名称, 类型, 说明 | 
|---|
accept string Setting to   | 
| 名称, 类型, 说明 | 
|---|
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”的示例代码
请求示例
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_IDResponse
Status: 204