仓库 web 挂钩允许您在仓库内发生特定事件时接收 HTTP POST
有效负载。 The webhook REST APIs enable you to manage repository, organization, and app webhooks. You can also use the REST API to change the configuration of the webhook. 例如,您可以修改有效负载 URL、内容类型、SSL 验证和机密。 更多信息请参阅:
如果您要设置一个 web 挂钩来接收来自组织所有仓库的事件,请参阅关于组织 web 挂钩的 API 文档。
除了 REST API 之外, GitHub 还可以作为仓库的 PubSubHubbub 枢纽。
仓库 web 挂钩
get /repos/{owner}/{repo}/hooks
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
per_page |
integer | query |
Results per page (max 100) Default:30 |
page |
integer | query |
Page number of the results to fetch. Default:1 |
代� �示例
Shell
curl \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks
JavaScript (@octokit/core.js)
await octokit.request('GET /repos/{owner}/{repo}/hooks', {
owner: 'octocat',
repo: 'hello-world'
})
Response
Status: 200 OK
[
{
"type": "Repository",
"id": 12345678,
"name": "web",
"active": true,
"events": [
"push",
"pull_request"
],
"config": {
"content_type": "json",
"insecure_ssl": "0",
"url": "https://example.com/webhook"
},
"updated_at": "2019-06-03T00:57:16Z",
"created_at": "2019-06-03T00:57:16Z",
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
}
]
Resource not found
Status: 404 Not Found
Notes
Create a repository webhook
Repositories can have multiple webhooks installed. Each webhook should have a unique config
. Multiple webhooks can
share the same config
as long as those webhooks do not have any events
that overlap.
post /repos/{owner}/{repo}/hooks
参数
Name | Type | In | Description | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept |
string | header |
Setting to
|
||||||||||||||
owner |
string | path | |||||||||||||||
repo |
string | path | |||||||||||||||
name |
string | body |
Use |
||||||||||||||
config |
object | body |
Key/value pairs to provide settings for this webhook. These are defined below. |
||||||||||||||
Properties of the
|
Name (Type) | Description |
---|---|
url (string)
|
The URL to which the payloads will be delivered. |
content_type (string)
|
The media type used to serialize the payloads. Supported values include |
secret (string)
|
If provided, the |
insecure_ssl (string or number)
|
Determines whether the SSL certificate of the host for |
token (string)
|
|
digest (string)
|
events
Determines what events the hook is triggered for.
Default:push
active
Determines if notifications are sent when the webhook is triggered. Set to true
to send notifications.
true
代� �示例
Shell
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks \
-d '{"name":"name"}'
JavaScript (@octokit/core.js)
await octokit.request('POST /repos/{owner}/{repo}/hooks', {
owner: 'octocat',
repo: 'hello-world',
name: 'name'
})
Response
Status: 201 Created
{
"type": "Repository",
"id": 12345678,
"name": "web",
"active": true,
"events": [
"push",
"pull_request"
],
"config": {
"content_type": "json",
"insecure_ssl": "0",
"url": "https://example.com/webhook"
},
"updated_at": "2019-06-03T00:57:16Z",
"created_at": "2019-06-03T00:57:16Z",
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
}
Forbidden
Status: 403 Forbidden
Resource not found
Status: 404 Not Found
Validation failed
Status: 422 Unprocessable Entity
Notes
Get a repository webhook
Returns a webhook configured in a repository. To get only the webhook config
properties, see "Get a webhook configuration for a repository."
get /repos/{owner}/{repo}/hooks/{hook_id}
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path |
代� �示例
Shell
curl \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42
JavaScript (@octokit/core.js)
await octokit.request('GET /repos/{owner}/{repo}/hooks/{hook_id}', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42
})
Response
Status: 200 OK
{
"type": "Repository",
"id": 12345678,
"name": "web",
"active": true,
"events": [
"push",
"pull_request"
],
"config": {
"content_type": "json",
"insecure_ssl": "0",
"url": "https://example.com/webhook"
},
"updated_at": "2019-06-03T00:57:16Z",
"created_at": "2019-06-03T00:57:16Z",
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
}
Resource not found
Status: 404 Not Found
Notes
Update a repository webhook
Updates a webhook configured in a repository. If you previously had a secret
set, you must provide the same secret
or set a new secret
or the secret will be removed. If you are only updating individual webhook config
properties, use "Update a webhook configuration for a repository."
patch /repos/{owner}/{repo}/hooks/{hook_id}
参数
Name | Type | In | Description | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept |
string | header |
Setting to
|
||||||||||||||
owner |
string | path | |||||||||||||||
repo |
string | path | |||||||||||||||
hook_id |
integer | path | |||||||||||||||
config |
object | body |
Key/value pairs to provide settings for this webhook. These are defined below. |
||||||||||||||
Properties of the
|
Name (Type) | Description |
---|---|
url (string)
|
Required. The URL to which the payloads will be delivered. |
content_type (string)
|
The media type used to serialize the payloads. Supported values include |
secret (string)
|
If provided, the |
insecure_ssl (string or number)
|
Determines whether the SSL certificate of the host for |
address (string)
|
|
room (string)
|
events
Determines what events the hook is triggered for. This replaces the entire array of events.
Default:push
add_events
Determines a list of events to be added to the list of events that the Hook triggers for.
remove_events
Determines a list of events to be removed from the list of events that the Hook triggers for.
active
Determines if notifications are sent when the webhook is triggered. Set to true
to send notifications.
true
代� �示例
Shell
curl \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42 \
-d '{"config":{"url":"url","content_type":"content_type","secret":"secret","insecure_ssl":"insecure_ssl","address":"address","room":"room"}}'
JavaScript (@octokit/core.js)
await octokit.request('PATCH /repos/{owner}/{repo}/hooks/{hook_id}', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42,
config: {
url: 'url',
content_type: 'content_type',
secret: 'secret',
insecure_ssl: 'insecure_ssl',
address: 'address',
room: 'room'
}
})
Response
Status: 200 OK
{
"type": "Repository",
"id": 12345678,
"name": "web",
"active": true,
"events": [
"push",
"pull_request"
],
"config": {
"content_type": "json",
"insecure_ssl": "0",
"url": "https://example.com/webhook"
},
"updated_at": "2019-06-03T00:57:16Z",
"created_at": "2019-06-03T00:57:16Z",
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
}
Resource not found
Status: 404 Not Found
Validation failed
Status: 422 Unprocessable Entity
Notes
delete /repos/{owner}/{repo}/hooks/{hook_id}
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path |
代� �示例
Shell
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42
JavaScript (@octokit/core.js)
await octokit.request('DELETE /repos/{owner}/{repo}/hooks/{hook_id}', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42
})
Response
Status: 204 No Content
Resource not found
Status: 404 Not Found
Notes
Ping a repository webhook
This will trigger a ping event to be sent to the hook.
post /repos/{owner}/{repo}/hooks/{hook_id}/pings
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path |
代� �示例
Shell
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42/pings
JavaScript (@octokit/core.js)
await octokit.request('POST /repos/{owner}/{repo}/hooks/{hook_id}/pings', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42
})
Response
Status: 204 No Content
Resource not found
Status: 404 Not Found
Notes
Test the push repository webhook
This will trigger the hook with the latest push to the current repository if the hook is subscribed to push
events. If the hook is not subscribed to push
events, the server will respond with 204 but no test POST will be generated.
Note: Previously /repos/:owner/:repo/hooks/:hook_id/test
post /repos/{owner}/{repo}/hooks/{hook_id}/tests
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path |
代� �示例
Shell
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42/tests
JavaScript (@octokit/core.js)
await octokit.request('POST /repos/{owner}/{repo}/hooks/{hook_id}/tests', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42
})
Response
Status: 204 No Content
Resource not found
Status: 404 Not Found
Notes
Repository webhook configuration
Get a webhook configuration for a repository
Returns the webhook configuration for a repository. To get more information about the webhook, including the active
state and events
, use "Get a repository webhook."
Access tokens must have the read:repo_hook
or repo
scope, and GitHub Apps must have the repository_hooks:read
permission.
get /repos/{owner}/{repo}/hooks/{hook_id}/config
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path |
代� �示例
Shell
curl \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42/config
JavaScript (@octokit/core.js)
await octokit.request('GET /repos/{owner}/{repo}/hooks/{hook_id}/config', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42
})
Response
Status: 200 OK
{
"content_type": "json",
"insecure_ssl": "0",
"secret": "********",
"url": "https://example.com/webhook"
}
Notes
Update a webhook configuration for a repository
Updates the webhook configuration for a repository. To update more information about the webhook, including the active
state and events
, use "Update a repository webhook."
Access tokens must have the write:repo_hook
or repo
scope, and GitHub Apps must have the repository_hooks:write
permission.
patch /repos/{owner}/{repo}/hooks/{hook_id}/config
参数
Name | Type | In | Description |
---|---|---|---|
accept |
string | header |
Setting to
|
owner |
string | path | |
repo |
string | path | |
hook_id |
integer | path | |
url |
string | body |
The URL to which the payloads will be delivered. |
content_type |
string | body |
The media type used to serialize the payloads. Supported values include |
secret |
string | body |
If provided, the |
insecure_ssl |
string or number | body |
Determines whether the SSL certificate of the host for |
代� �示例
Shell
curl \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
http(s)://{hostname}/api/v3/repos/octocat/hello-world/hooks/42/config \
-d '{"url":"url"}'
JavaScript (@octokit/core.js)
await octokit.request('PATCH /repos/{owner}/{repo}/hooks/{hook_id}/config', {
owner: 'octocat',
repo: 'hello-world',
hook_id: 42,
url: 'url'
})
Response
Status: 200 OK
{
"content_type": "json",
"insecure_ssl": "0",
"secret": "********",
"url": "https://example.com/webhook"
}
Notes
Repository webhook deliveries
接收 web 挂钩
为了让 GitHub Enterprise Server 发送 web 挂钩有效负载,您的服务器需要能够从 Internet 访问。 我们还强烈建议使用 SSL,以便我们可以通过 HTTPS 发送� 密的有效负载。
Web 挂钩� �头
GitHub Enterprise Server 发送时将附带� 个 HTTP � �头,以区分事件类型和有效负载� �识符。 更多信息请参阅 web 挂钩� �头。
PubSubHubbub
GitHub 还可以作为所有仓库的 PubSubHubbabub 枢纽。 PSHB 是一个简单的发布/订阅协议,允许服务器注册在主题更新时接收更新。 这些更新随 HTTP POST 请求一起发送到回调 URL。 GitHub 仓库推送的主题 URL 采用以下� �式:
https://github.com/{owner}/{repo}/events/{event}
事件可以是任何可用的 web 挂钩事件。 更多信息请参阅“web 挂钩事件和有效负载”。
响应� �式
默认� �式为现有接收后挂钩应具有的� �式:作为 POST 中的 payload
参数发送的 JSON 正文。 您还可以指定接收带有 Accept
� �头或 .json
扩展名的原始 JSON 正文。
Accept: application/json
https://github.com/{owner}/{repo}/events/push.json
回调 URL
回调 URL 可以使用 http://
协议。
# Send updates to postbin.org
http://postbin.org/123
订阅
GitHub PubSubHubbub 端点为:http(s)://[hostname]/api/v3/hub
。 使用 cURL 的成功请求如下所示:
curl -u "user" -i \
http(s)://[hostname]/api/v3/hub \
-F "hub.mode=subscribe" \
-F "hub.topic=https://github.com/{owner}/{repo}/events/push" \
-F "hub.callback=http://postbin.org/123"
PubSubHubbub 请求可以多次发送。 如果挂钩已经存在,它将� �据请求进行修改。
参数
名称 | 类型 | 描述 |
---|---|---|
hub.mode | 字符串 | 必填。 值为 subscribe 或 unsubscribe 。 |
hub.topic | 字符串 | 必填。 要订阅的 GitHub 仓库的 URI。 路径� �式必须为 /{owner}/{repo}/events/{event} 。 |
hub.callback | 字符串 | 要接收主题更新的 URI。 |
hub.secret | 字符串 | 用于生成� 出正文内容的哈希签名的共享密钥。 您可以通过比较原始请求正文与 X-Hub-Signature 或 X-Hub-Signature-256 � �头 的内容来验证来自 GitHub 的推送。 您可以查看 PubSubHubbub 文档了解详情。 |