You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhooks.en-us.md 4.8KB

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. The following is an example of event information that will be sent by Gitea to
  29. a Payload URL:
  30. ```
  31. X-GitHub-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  32. X-GitHub-Event: push
  33. X-Gogs-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  34. X-Gogs-Event: push
  35. X-Gitea-Delivery: f6266f16-1bf3-46a5-9ea4-602e06ead473
  36. X-Gitea-Event: push
  37. ```
  38. ```json
  39. {
  40. "secret": "3gEsCfjlV2ugRwgpU#w1*WaW*wa4NXgGmpCfkbG3",
  41. "ref": "refs/heads/develop",
  42. "before": "28e1879d029cb852e4844d9c718537df08844e03",
  43. "after": "bffeb74224043ba2feb48d137756c8a9331c449a",
  44. "compare_url": "http://localhost:3000/gitea/webhooks/compare/28e1879d029cb852e4844d9c718537df08844e03...bffeb74224043ba2feb48d137756c8a9331c449a",
  45. "commits": [
  46. {
  47. "id": "bffeb74224043ba2feb48d137756c8a9331c449a",
  48. "message": "Webhooks Yay!",
  49. "url": "http://localhost:3000/gitea/webhooks/commit/bffeb74224043ba2feb48d137756c8a9331c449a",
  50. "author": {
  51. "name": "Gitea",
  52. "email": "someone@gitea.io",
  53. "username": "gitea"
  54. },
  55. "committer": {
  56. "name": "Gitea",
  57. "email": "someone@gitea.io",
  58. "username": "gitea"
  59. },
  60. "timestamp": "2017-03-13T13:52:11-04:00"
  61. }
  62. ],
  63. "repository": {
  64. "id": 140,
  65. "owner": {
  66. "id": 1,
  67. "login": "gitea",
  68. "full_name": "Gitea",
  69. "email": "someone@gitea.io",
  70. "avatar_url": "https://localhost:3000/avatars/1",
  71. "username": "gitea"
  72. },
  73. "name": "webhooks",
  74. "full_name": "gitea/webhooks",
  75. "description": "",
  76. "private": false,
  77. "fork": false,
  78. "html_url": "http://localhost:3000/gitea/webhooks",
  79. "ssh_url": "ssh://gitea@localhost:2222/gitea/webhooks.git",
  80. "clone_url": "http://localhost:3000/gitea/webhooks.git",
  81. "website": "",
  82. "stars_count": 0,
  83. "forks_count": 1,
  84. "watchers_count": 1,
  85. "open_issues_count": 7,
  86. "default_branch": "master",
  87. "created_at": "2017-02-26T04:29:06-05:00",
  88. "updated_at": "2017-03-13T13:51:58-04:00"
  89. },
  90. "pusher": {
  91. "id": 1,
  92. "login": "gitea",
  93. "full_name": "Gitea",
  94. "email": "someone@gitea.io",
  95. "avatar_url": "https://localhost:3000/avatars/1",
  96. "username": "gitea"
  97. },
  98. "sender": {
  99. "id": 1,
  100. "login": "gitea",
  101. "full_name": "Gitea",
  102. "email": "someone@gitea.io",
  103. "avatar_url": "https://localhost:3000/avatars/1",
  104. "username": "gitea"
  105. }
  106. }
  107. ```
  108. ### Example
  109. This is an example of how to use webhooks to run a php script upon push requests to the repository.
  110. In your repository Settings, under Webhooks, Setup a Gitea webhook as follows:
  111. - Target URL: http://mydomain.com/webhook.php
  112. - HTTP Method: POST
  113. - POST Content Type: application/json
  114. - Secret: 123
  115. - Trigger On: Push Events
  116. - Active: Checked
  117. Now on your server create the php file webhook.php
  118. ```
  119. <?php
  120. $secret_key = '123';
  121. // check for POST request
  122. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  123. error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
  124. exit();
  125. }
  126. // get content type
  127. $content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
  128. if ($content_type != 'application/json') {
  129. error_log('FAILED - not application/json - '. $content_type);
  130. exit();
  131. }
  132. // get payload
  133. $payload = trim(file_get_contents("php://input"));
  134. if (empty($payload)) {
  135. error_log('FAILED - no payload');
  136. exit();
  137. }
  138. // get header signature
  139. $header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
  140. if (empty($header_signature)) {
  141. error_log('FAILED - header signature missing');
  142. exit();
  143. }
  144. // calculate payload signature
  145. $payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
  146. // check payload signature against header signature
  147. if ($header_signature != $payload_signature) {
  148. error_log('FAILED - payload signature');
  149. exit();
  150. }
  151. // convert json to array
  152. $decoded = json_decode($payload, true);
  153. // check for json decode errors
  154. if (json_last_error() !== JSON_ERROR_NONE) {
  155. error_log('FAILED - json decode - '. json_last_error());
  156. exit();
  157. }
  158. // success, do something
  159. ```
  160. 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.