Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

webhooks.en-us.md 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ---
  2. date: "2016-12-01T16:00:00+02:00"
  3. title: "Webhooks"
  4. slug: "webhooks"
  5. weight: 10
  6. toc: true
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "features"
  11. name: "Webhooks"
  12. weight: 30
  13. identifier: "webhooks"
  14. ---
  15. # Webhooks
  16. Gitea supports web hooks for repository events. This can be configured in the settings
  17. page `/:username/:reponame/settings/hooks` by a repository admin. Webhooks can also be configured on a per-organization and whole system basis.
  18. All event pushes are POST requests. The methods currently supported are:
  19. - Gitea (can also be a GET request)
  20. - Gogs
  21. - Slack
  22. - Discord
  23. - Dingtalk
  24. - Telegram
  25. - Microsoft Teams
  26. - Feishu
  27. ### Event information
  28. **WARNING**: The `secret` field in the payload is deprecated as of Gitea 1.13.0 and will be removed in 1.14.0: https://github.com/go-gitea/gitea/issues/11755
  29. The following is an example of event information that will be sent by Gitea to
  30. a Payload URL:
  31. ```
  32. X-GitHub-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  33. X-GitHub-Event: push
  34. X-Gogs-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  35. X-Gogs-Event: push
  36. X-Gitea-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  37. X-Gitea-Event: push
  38. ```
  39. ```json
  40. {
  41. "secret": "3gEsCfjlV2ugRwgpU#w1*WaW*wa4NXgGmpCfkbG3",
  42. "ref": "refs/heads/develop",
  43. "before": "28e1879d029cb852e4844d9c718537df08844e03",
  44. "after": "bffeb74224043ba2feb48d137756c8a9331c449a",
  45. "compare_url": "http://localhost:3000/gitea/webhooks/compare/28e1879d029cb852e4844d9c718537df08844e03...bffeb74224043ba2feb48d137756c8a9331c449a",
  46. "commits": [
  47. {
  48. "id": "bffeb74224043ba2feb48d137756c8a9331c449a",
  49. "message": "Webhooks Yay!",
  50. "url": "http://localhost:3000/gitea/webhooks/commit/bffeb74224043ba2feb48d137756c8a9331c449a",
  51. "author": {
  52. "name": "Gitea",
  53. "email": "someone@gitea.io",
  54. "username": "gitea"
  55. },
  56. "committer": {
  57. "name": "Gitea",
  58. "email": "someone@gitea.io",
  59. "username": "gitea"
  60. },
  61. "timestamp": "2017-03-13T13:52:11-04:00"
  62. }
  63. ],
  64. "repository": {
  65. "id": 140,
  66. "owner": {
  67. "id": 1,
  68. "login": "gitea",
  69. "full_name": "Gitea",
  70. "email": "someone@gitea.io",
  71. "avatar_url": "https://localhost:3000/avatars/1",
  72. "username": "gitea"
  73. },
  74. "name": "webhooks",
  75. "full_name": "gitea/webhooks",
  76. "description": "",
  77. "private": false,
  78. "fork": false,
  79. "html_url": "http://localhost:3000/gitea/webhooks",
  80. "ssh_url": "ssh://gitea@localhost:2222/gitea/webhooks.git",
  81. "clone_url": "http://localhost:3000/gitea/webhooks.git",
  82. "website": "",
  83. "stars_count": 0,
  84. "forks_count": 1,
  85. "watchers_count": 1,
  86. "open_issues_count": 7,
  87. "default_branch": "master",
  88. "created_at": "2017-02-26T04:29:06-05:00",
  89. "updated_at": "2017-03-13T13:51:58-04:00"
  90. },
  91. "pusher": {
  92. "id": 1,
  93. "login": "gitea",
  94. "full_name": "Gitea",
  95. "email": "someone@gitea.io",
  96. "avatar_url": "https://localhost:3000/avatars/1",
  97. "username": "gitea"
  98. },
  99. "sender": {
  100. "id": 1,
  101. "login": "gitea",
  102. "full_name": "Gitea",
  103. "email": "someone@gitea.io",
  104. "avatar_url": "https://localhost:3000/avatars/1",
  105. "username": "gitea"
  106. }
  107. }
  108. ```
  109. ### Example
  110. This is an example of how to use webhooks to run a php script upon push requests to the repository.
  111. In your repository Settings, under Webhooks, Setup a Gitea webhook as follows:
  112. - Target URL: http://mydomain.com/webhook.php
  113. - HTTP Method: POST
  114. - POST Content Type: application/json
  115. - Secret: 123
  116. - Trigger On: Push Events
  117. - Active: Checked
  118. Now on your server create the php file webhook.php
  119. ```
  120. <?php
  121. $secret_key = '123';
  122. // check for POST request
  123. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  124. error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
  125. exit();
  126. }
  127. // get content type
  128. $content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
  129. if ($content_type != 'application/json') {
  130. error_log('FAILED - not application/json - '. $content_type);
  131. exit();
  132. }
  133. // get payload
  134. $payload = trim(file_get_contents("php://input"));
  135. if (empty($payload)) {
  136. error_log('FAILED - no payload');
  137. exit();
  138. }
  139. // get header signature
  140. $header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
  141. if (empty($header_signature)) {
  142. error_log('FAILED - header signature missing');
  143. exit();
  144. }
  145. // calculate payload signature
  146. $payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
  147. // check payload signature against header signature
  148. if ($header_signature != $payload_signature) {
  149. error_log('FAILED - payload signature');
  150. exit();
  151. }
  152. // convert json to array
  153. $decoded = json_decode($payload, true);
  154. // check for json decode errors
  155. if (json_last_error() !== JSON_ERROR_NONE) {
  156. error_log('FAILED - json decode - '. json_last_error());
  157. exit();
  158. }
  159. // success, do something
  160. ```
  161. There is a Test Delivery button in the webhook settings that allows to test the configuration as well as a list of the most Recent Deliveries.