Webhooks

Gitea支持用于仓库事件的Webhooks。这可以在仓库管理员在设置页面 /:username/:reponame/settings/hooks 中进行配置。Webhooks还可以基于组织和整个系统进行配置。 所有事件推送都是 POST 请求。目前支持:

  • Gitea (也可以是 GET 请求)
  • Gogs
  • Slack
  • Discord
  • Dingtalk(钉钉)
  • Telegram
  • Microsoft Teams
  • Feishu
  • Wechatwork(企业微信)
  • Packagist

事件信息

警告:自 Gitea 1.13.0 版起,payload 中的 secret 字段已被弃用,并将在 1.14.0 版中移除:https://github.com/go-gitea/gitea/issues/11755

以下是 Gitea 将发送给 payload URL的事件信息示例:

  1. X-GitHub-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  2. X-GitHub-Event: push
  3. X-Gogs-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  4. X-Gogs-Event: push
  5. X-Gitea-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  6. X-Gitea-Event: push
  1. {
  2. "secret": "3gEsCfjlV2ugRwgpU#w1*WaW*wa4NXgGmpCfkbG3",
  3. "ref": "refs/heads/develop",
  4. "before": "28e1879d029cb852e4844d9c718537df08844e03",
  5. "after": "bffeb74224043ba2feb48d137756c8a9331c449a",
  6. "compare_url": "http://localhost:3000/gitea/webhooks/compare/28e1879d029cb852e4844d9c718537df08844e03...bffeb74224043ba2feb48d137756c8a9331c449a",
  7. "commits": [
  8. {
  9. "id": "bffeb74224043ba2feb48d137756c8a9331c449a",
  10. "message": "Webhooks Yay!",
  11. "url": "http://localhost:3000/gitea/webhooks/commit/bffeb74224043ba2feb48d137756c8a9331c449a",
  12. "author": {
  13. "name": "Gitea",
  14. "email": "someone@gitea.io",
  15. "username": "gitea"
  16. },
  17. "committer": {
  18. "name": "Gitea",
  19. "email": "someone@gitea.io",
  20. "username": "gitea"
  21. },
  22. "timestamp": "2017-03-13T13:52:11-04:00"
  23. }
  24. ],
  25. "repository": {
  26. "id": 140,
  27. "owner": {
  28. "id": 1,
  29. "login": "gitea",
  30. "full_name": "Gitea",
  31. "email": "someone@gitea.io",
  32. "avatar_url": "https://localhost:3000/avatars/1",
  33. "username": "gitea"
  34. },
  35. "name": "webhooks",
  36. "full_name": "gitea/webhooks",
  37. "description": "",
  38. "private": false,
  39. "fork": false,
  40. "html_url": "http://localhost:3000/gitea/webhooks",
  41. "ssh_url": "ssh://gitea@localhost:2222/gitea/webhooks.git",
  42. "clone_url": "http://localhost:3000/gitea/webhooks.git",
  43. "website": "",
  44. "stars_count": 0,
  45. "forks_count": 1,
  46. "watchers_count": 1,
  47. "open_issues_count": 7,
  48. "default_branch": "master",
  49. "created_at": "2017-02-26T04:29:06-05:00",
  50. "updated_at": "2017-03-13T13:51:58-04:00"
  51. },
  52. "pusher": {
  53. "id": 1,
  54. "login": "gitea",
  55. "full_name": "Gitea",
  56. "email": "someone@gitea.io",
  57. "avatar_url": "https://localhost:3000/avatars/1",
  58. "username": "gitea"
  59. },
  60. "sender": {
  61. "id": 1,
  62. "login": "gitea",
  63. "full_name": "Gitea",
  64. "email": "someone@gitea.io",
  65. "avatar_url": "https://localhost:3000/avatars/1",
  66. "username": "gitea"
  67. }
  68. }

示例

这是一个示例,演示如何使用 Webhooks 在推送请求到达仓库时运行一个 php 脚本。 在你的仓库设置中,在 Webhooks 下,设置一个如下的 Gitea webhook:

现在在你的服务器上创建 php 文件 webhook.php。

  1. <?php
  2. $secret_key = '123';
  3. // check for POST request
  4. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  5. error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
  6. exit();
  7. }
  8. // get content type
  9. $content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
  10. if ($content_type != 'application/json') {
  11. error_log('FAILED - not application/json - '. $content_type);
  12. exit();
  13. }
  14. // get payload
  15. $payload = trim(file_get_contents("php://input"));
  16. if (empty($payload)) {
  17. error_log('FAILED - no payload');
  18. exit();
  19. }
  20. // get header signature
  21. $header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
  22. if (empty($header_signature)) {
  23. error_log('FAILED - header signature missing');
  24. exit();
  25. }
  26. // calculate payload signature
  27. $payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
  28. // check payload signature against header signature
  29. if ($header_signature !== $payload_signature) {
  30. error_log('FAILED - payload signature');
  31. exit();
  32. }
  33. // convert json to array
  34. $decoded = json_decode($payload, true);
  35. // check for json decode errors
  36. if (json_last_error() !== JSON_ERROR_NONE) {
  37. error_log('FAILED - json decode - '. json_last_error());
  38. exit();
  39. }
  40. // success, do something

在 Webhook 设置中有一个“测试推送(Test Delivery)”按钮,可以测试配置,还有一个“最近推送记录(Recent Deliveries)”的列表。

授权头(Authorization header)

从1.19版本开始,Gitea 的 Webhook 可以配置为向 Webhook 目标发送一个 授权头(authorization header)