# 适用于存储库预接收挂钩的 REST API 终结点

使用 REST API 查看和修改存储库中可用的预接收挂钩的执行情况。

## 关于存储库预接收挂钩

> \[!NOTE]
> 这些终结点仅支持使用 personal access token (classic) 进行身份验证。 有关详细信息，请参阅“[管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic)”。

| 名称                  | 类型       | 说明           |
| ------------------- | -------- | ------------ |
| `name`              | `string` | 挂钩的名称。       |
| `enforcement`       | `string` | 此仓库中挂钩的实施状态。 |
| `configuration_url` | `string` | 设置实施的端点 URL。 |

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

`configuration_url` 可能是此仓库、其组织所有者或全局配置的链接。
`configuration_url` 端点的访问权限由所有者或站点管理员级别确定。

> \[!NOTE]
> Most endpoints use `Authorization: Bearer <YOUR-TOKEN>` and `Accept: application/vnd.github+json` headers, plus `X-GitHub-Api-Version: 2026-03-10`. Curl examples below omit these standard headers for brevity.

## List pre-receive hooks for a repository

```
GET /repos/{owner}/{repo}/pre-receive-hooks
```

List all pre-receive hooks that are enabled or testing for this repository as well as any disabled hooks that are allowed to be enabled at the repository level. Pre-receive hooks that are disabled at a higher level and are not configurable will not be listed.

### Parameters

#### Headers

* **`accept`** (string)
  Setting to `application/vnd.github+json` is recommended.

#### Path and query parameters

* **`owner`** (string) (required)
  The account owner of the repository. The name is not case sensitive.

* **`repo`** (string) (required)
  The name of the repository without the .git extension. The name is not case sensitive.

* **`per_page`** (integer)
  The number of results per page (max 100). For more information, see "Using pagination in the REST API."
  Default: `30`

* **`page`** (integer)
  The page number of the results to fetch. For more information, see "Using pagination in the REST API."
  Default: `1`

* **`direction`** (string)
  The direction to sort the results by.
  Default: `desc`
  Can be one of: `asc`, `desc`

* **`sort`** (string)
  Default: `created`
  Can be one of: `created`, `updated`, `name`

### HTTP response status codes

* **200** - OK

### Code examples

#### Example

**Request:**

```curl
curl -L \
  -X GET \
  http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/pre-receive-hooks
```

**Response schema (Status: 200):**

Array of objects:

* `id`: integer
* `name`: string
* `enforcement`: string
* `configuration_url`: string

## Get a pre-receive hook for a repository

```
GET /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id}
```

### Parameters

#### Headers

* **`accept`** (string)
  Setting to `application/vnd.github+json` is recommended.

#### Path and query parameters

* **`owner`** (string) (required)
  The account owner of the repository. The name is not case sensitive.

* **`repo`** (string) (required)
  The name of the repository without the .git extension. The name is not case sensitive.

* **`pre_receive_hook_id`** (string) (required)
  The unique identifier of the pre-receive hook.

### HTTP response status codes

* **200** - OK

### Code examples

#### Example

**Request:**

```curl
curl -L \
  -X GET \
  http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/pre-receive-hooks/PRE_RECEIVE_HOOK_ID
```

**Response schema (Status: 200):**

* `id`: integer
* `name`: string
* `enforcement`: string
* `configuration_url`: string

## Update pre-receive hook enforcement for a repository

```
PATCH /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id}
```

For pre-receive hooks which are allowed to be configured at the repo level, you can set enforcement

### Parameters

#### Headers

* **`accept`** (string)
  Setting to `application/vnd.github+json` is recommended.

#### Path and query parameters

* **`owner`** (string) (required)
  The account owner of the repository. The name is not case sensitive.

* **`repo`** (string) (required)
  The name of the repository without the .git extension. The name is not case sensitive.

* **`pre_receive_hook_id`** (string) (required)
  The unique identifier of the pre-receive hook.

#### Body parameters

* **`enforcement`** (string)
  The state of enforcement for the hook on this repository.
  Can be one of: `enabled`, `disabled`, `testing`

### HTTP response status codes

* **200** - OK

### Code examples

#### Example

**Request:**

```curl
curl -L \
  -X PATCH \
  http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/pre-receive-hooks/PRE_RECEIVE_HOOK_ID \
  -d '{
  "enforcement": "enabled"
}'
```

**Response schema (Status: 200):**

Same response schema as [Get a pre-receive hook for a repository](#get-a-pre-receive-hook-for-a-repository).

## Remove pre-receive hook enforcement for a repository

```
DELETE /repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id}
```

Deletes any overridden enforcement on this repository for the specified hook.
Responds with effective values inherited from owner and/or global level.

### Parameters

#### Headers

* **`accept`** (string)
  Setting to `application/vnd.github+json` is recommended.

#### Path and query parameters

* **`owner`** (string) (required)
  The account owner of the repository. The name is not case sensitive.

* **`repo`** (string) (required)
  The name of the repository without the .git extension. The name is not case sensitive.

* **`pre_receive_hook_id`** (string) (required)
  The unique identifier of the pre-receive hook.

### HTTP response status codes

* **200** - Responds with effective values inherited from owner and/or global level.

### Code examples

#### Example

**Request:**

```curl
curl -L \
  -X DELETE \
  http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/pre-receive-hooks/PRE_RECEIVE_HOOK_ID
```

**Response schema (Status: 200):**

Same response schema as [Get a pre-receive hook for a repository](#get-a-pre-receive-hook-for-a-repository).