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

webhooks.en-us.md 4.8 kB

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