2017-11-27 00:44:32 +03:00
|
|
|
|
---
|
|
|
|
|
date: "2016-12-01T16:00:00+02:00"
|
|
|
|
|
title: "Webhooks"
|
|
|
|
|
slug: "webhooks"
|
2023-07-26 05:00:14 +03:00
|
|
|
|
sidebar_position: 30
|
2020-12-09 09:47:06 +03:00
|
|
|
|
toc: false
|
2017-11-27 00:44:32 +03:00
|
|
|
|
draft: false
|
Refactor docs (#23752)
This was intended to be a small followup for
https://github.com/go-gitea/gitea/pull/23712, but...here we are.
1. Our docs currently use `slug` as the entire URL, which makes
refactoring tricky (see https://github.com/go-gitea/gitea/pull/23712).
Instead, this PR attempts to make future refactoring easier by using
slugs as an extension of the section. (Hugo terminology)
- What the above boils down to is this PR attempts to use directory
organization as URL management. e.g. `usage/comparison.en-us.md` ->
`en-us/usage/comparison/`, `usage/packages/overview.en-us.md` ->
`en-us/usage/packages/overview/`
- Technically we could even remove `slug`, as Hugo defaults to using
filename, however at least with this PR it means `slug` only needs to be
the name for the **current file** rather than an entire URL
2. This PR adds appropriate aliases (redirects) for pages, so anything
on the internet that links to our docs should hopefully not break.
3. A minor nit I've had for a while, renaming `seek-help` to `support`.
It's a minor thing, but `seek-help` has a strange connotation to it.
4. The commits are split such that you can review the first which is the
"actual" change, and the second is added redirects so that the first
doesn't break links elsewhere.
---------
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-04-28 06:33:41 +03:00
|
|
|
|
aliases:
|
|
|
|
|
- /zh-cn/webhooks
|
2017-11-27 00:44:32 +03:00
|
|
|
|
menu:
|
|
|
|
|
sidebar:
|
2023-03-23 18:18:24 +03:00
|
|
|
|
parent: "usage"
|
2017-11-27 00:44:32 +03:00
|
|
|
|
name: "Webhooks"
|
2023-07-26 05:00:14 +03:00
|
|
|
|
sidebar_position: 30
|
2017-11-27 00:44:32 +03:00
|
|
|
|
identifier: "webhooks"
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Webhooks
|
|
|
|
|
|
2023-05-25 07:33:38 +03:00
|
|
|
|
Gitea支持用于仓库事件的Webhooks。这可以在仓库管理员在设置页面 `/:username/:reponame/settings/hooks` 中进行配置。Webhooks还可以基于组织和整个系统进行配置。
|
|
|
|
|
所有事件推送都是 POST 请求。目前支持:
|
2021-07-23 07:41:27 +03:00
|
|
|
|
|
2023-05-25 07:33:38 +03:00
|
|
|
|
- Gitea (也可以是 GET 请求)
|
2021-07-23 07:41:27 +03:00
|
|
|
|
- Gogs
|
|
|
|
|
- Slack
|
|
|
|
|
- Discord
|
2023-05-25 07:33:38 +03:00
|
|
|
|
- Dingtalk(钉钉)
|
2021-07-23 07:41:27 +03:00
|
|
|
|
- Telegram
|
|
|
|
|
- Microsoft Teams
|
|
|
|
|
- Feishu
|
2023-05-25 07:33:38 +03:00
|
|
|
|
- Wechatwork(企业微信)
|
2022-01-23 16:46:30 +03:00
|
|
|
|
- Packagist
|
2021-07-23 07:41:27 +03:00
|
|
|
|
|
2023-05-25 07:33:38 +03:00
|
|
|
|
### 事件信息
|
|
|
|
|
|
|
|
|
|
**警告**:自 Gitea 1.13.0 版起,payload 中的 `secret` 字段已被弃用,并将在 1.14.0 版中移除:https://github.com/go-gitea/gitea/issues/11755
|
|
|
|
|
|
|
|
|
|
以下是 Gitea 将发送给 payload URL的事件信息示例:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
X-GitHub-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
|
|
|
|
|
X-GitHub-Event: push
|
|
|
|
|
X-Gogs-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
|
|
|
|
|
X-Gogs-Event: push
|
|
|
|
|
X-Gitea-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
|
|
|
|
|
X-Gitea-Event: push
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"secret": "3gEsCfjlV2ugRwgpU#w1*WaW*wa4NXgGmpCfkbG3",
|
|
|
|
|
"ref": "refs/heads/develop",
|
|
|
|
|
"before": "28e1879d029cb852e4844d9c718537df08844e03",
|
|
|
|
|
"after": "bffeb74224043ba2feb48d137756c8a9331c449a",
|
|
|
|
|
"compare_url": "http://localhost:3000/gitea/webhooks/compare/28e1879d029cb852e4844d9c718537df08844e03...bffeb74224043ba2feb48d137756c8a9331c449a",
|
|
|
|
|
"commits": [
|
|
|
|
|
{
|
|
|
|
|
"id": "bffeb74224043ba2feb48d137756c8a9331c449a",
|
|
|
|
|
"message": "Webhooks Yay!",
|
|
|
|
|
"url": "http://localhost:3000/gitea/webhooks/commit/bffeb74224043ba2feb48d137756c8a9331c449a",
|
|
|
|
|
"author": {
|
|
|
|
|
"name": "Gitea",
|
|
|
|
|
"email": "someone@gitea.io",
|
|
|
|
|
"username": "gitea"
|
|
|
|
|
},
|
|
|
|
|
"committer": {
|
|
|
|
|
"name": "Gitea",
|
|
|
|
|
"email": "someone@gitea.io",
|
|
|
|
|
"username": "gitea"
|
|
|
|
|
},
|
|
|
|
|
"timestamp": "2017-03-13T13:52:11-04:00"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"repository": {
|
|
|
|
|
"id": 140,
|
|
|
|
|
"owner": {
|
|
|
|
|
"id": 1,
|
|
|
|
|
"login": "gitea",
|
|
|
|
|
"full_name": "Gitea",
|
|
|
|
|
"email": "someone@gitea.io",
|
|
|
|
|
"avatar_url": "https://localhost:3000/avatars/1",
|
|
|
|
|
"username": "gitea"
|
|
|
|
|
},
|
|
|
|
|
"name": "webhooks",
|
|
|
|
|
"full_name": "gitea/webhooks",
|
|
|
|
|
"description": "",
|
|
|
|
|
"private": false,
|
|
|
|
|
"fork": false,
|
|
|
|
|
"html_url": "http://localhost:3000/gitea/webhooks",
|
|
|
|
|
"ssh_url": "ssh://gitea@localhost:2222/gitea/webhooks.git",
|
|
|
|
|
"clone_url": "http://localhost:3000/gitea/webhooks.git",
|
|
|
|
|
"website": "",
|
|
|
|
|
"stars_count": 0,
|
|
|
|
|
"forks_count": 1,
|
|
|
|
|
"watchers_count": 1,
|
|
|
|
|
"open_issues_count": 7,
|
|
|
|
|
"default_branch": "master",
|
|
|
|
|
"created_at": "2017-02-26T04:29:06-05:00",
|
|
|
|
|
"updated_at": "2017-03-13T13:51:58-04:00"
|
|
|
|
|
},
|
|
|
|
|
"pusher": {
|
|
|
|
|
"id": 1,
|
|
|
|
|
"login": "gitea",
|
|
|
|
|
"full_name": "Gitea",
|
|
|
|
|
"email": "someone@gitea.io",
|
|
|
|
|
"avatar_url": "https://localhost:3000/avatars/1",
|
|
|
|
|
"username": "gitea"
|
|
|
|
|
},
|
|
|
|
|
"sender": {
|
|
|
|
|
"id": 1,
|
|
|
|
|
"login": "gitea",
|
|
|
|
|
"full_name": "Gitea",
|
|
|
|
|
"email": "someone@gitea.io",
|
|
|
|
|
"avatar_url": "https://localhost:3000/avatars/1",
|
|
|
|
|
"username": "gitea"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 示例
|
|
|
|
|
|
|
|
|
|
这是一个示例,演示如何使用 Webhooks 在推送请求到达仓库时运行一个 php 脚本。
|
|
|
|
|
在你的仓库设置中,在 Webhooks 下,设置一个如下的 Gitea webhook:
|
|
|
|
|
|
|
|
|
|
- 目标 URL:http://mydomain.com/webhook.php
|
|
|
|
|
- HTTP 方法:POST
|
|
|
|
|
- POST Content Type:application/json
|
|
|
|
|
- Secret:123
|
|
|
|
|
- 触发条件:推送事件
|
|
|
|
|
- 激活:勾选
|
|
|
|
|
|
|
|
|
|
现在在你的服务器上创建 php 文件 webhook.php。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
$secret_key = '123';
|
|
|
|
|
|
|
|
|
|
// check for POST request
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
|
|
|
|
|
error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get content type
|
|
|
|
|
$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
|
|
|
|
|
|
|
|
|
|
if ($content_type != 'application/json') {
|
|
|
|
|
error_log('FAILED - not application/json - '. $content_type);
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get payload
|
|
|
|
|
$payload = trim(file_get_contents("php://input"));
|
|
|
|
|
|
|
|
|
|
if (empty($payload)) {
|
|
|
|
|
error_log('FAILED - no payload');
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get header signature
|
|
|
|
|
$header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
|
|
|
|
|
|
|
|
|
|
if (empty($header_signature)) {
|
|
|
|
|
error_log('FAILED - header signature missing');
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calculate payload signature
|
|
|
|
|
$payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
|
|
|
|
|
|
|
|
|
|
// check payload signature against header signature
|
|
|
|
|
if ($header_signature !== $payload_signature) {
|
|
|
|
|
error_log('FAILED - payload signature');
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert json to array
|
|
|
|
|
$decoded = json_decode($payload, true);
|
|
|
|
|
|
|
|
|
|
// check for json decode errors
|
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
|
error_log('FAILED - json decode - '. json_last_error());
|
|
|
|
|
exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// success, do something
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在 Webhook 设置中有一个“测试推送(Test Delivery)”按钮,可以测试配置,还有一个“最近推送记录(Recent Deliveries)”的列表。
|
|
|
|
|
|
|
|
|
|
### 授权头(Authorization header)
|
|
|
|
|
|
|
|
|
|
**从1.19版本开始**,Gitea 的 Webhook 可以配置为向 Webhook 目标发送一个 [授权头(authorization header)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization)。
|