|
17 | 17 |
|
18 | 18 | Gitea supports web hooks for repository events. This can be found in the settings
|
19 | 19 | page `/:username/:reponame/settings/hooks`. All event pushes are POST requests.
|
20 |
| -The two methods currently supported are Gitea and Slack. |
| 20 | +The methods currently supported are: |
| 21 | + |
| 22 | +- Gitea |
| 23 | +- Gogs |
| 24 | +- Slack |
| 25 | +- Discord |
| 26 | +- Dingtalk |
| 27 | +- Telegram |
| 28 | +- Microsoft Teams |
21 | 29 |
|
22 | 30 | ### Event information
|
23 | 31 |
|
@@ -104,3 +112,75 @@ X-Gitea-Event: push
|
104 | 112 | }
|
105 | 113 | }
|
106 | 114 | ```
|
| 115 | + |
| 116 | +### Example |
| 117 | + |
| 118 | +This is an example of how to use webhooks to run a php script upon push requests to the repository. |
| 119 | +In your repository Settings, under Webhooks, Setup a Gitea webhook as follows: |
| 120 | + |
| 121 | +- Target URL: http://mydomain.com/webhook.php |
| 122 | +- HTTP Method: POST |
| 123 | +- POST Content Type: application/json |
| 124 | +- Secret: 123 |
| 125 | +- Trigger On: Push Events |
| 126 | +- Active: Checked |
| 127 | + |
| 128 | +Now on your server create the php file webhook.php |
| 129 | + |
| 130 | +``` |
| 131 | +<?php |
| 132 | +
|
| 133 | +$secret_key = '123'; |
| 134 | +
|
| 135 | +// check for POST request |
| 136 | +if ($_SERVER['REQUEST_METHOD'] != 'POST') { |
| 137 | + error_log('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']); |
| 138 | + exit(); |
| 139 | +} |
| 140 | +
|
| 141 | +// get content type |
| 142 | +$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : ''; |
| 143 | +
|
| 144 | +if ($content_type != 'application/json') { |
| 145 | + error_log('FAILED - not application/json - '. $content_type); |
| 146 | + exit(); |
| 147 | +} |
| 148 | +
|
| 149 | +// get payload |
| 150 | +$payload = trim(file_get_contents("php://input")); |
| 151 | +
|
| 152 | +if (empty($payload)) { |
| 153 | + error_log('FAILED - no payload'); |
| 154 | + exit(); |
| 155 | +} |
| 156 | +
|
| 157 | +// get header signature |
| 158 | +$header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : ''; |
| 159 | +
|
| 160 | +if (empty($header_signature)) { |
| 161 | + error_log('FAILED - header signature missing'); |
| 162 | + exit(); |
| 163 | +} |
| 164 | +
|
| 165 | +// calculate payload signature |
| 166 | +$payload_signature = hash_hmac('sha256', $payload, $secret_key, false); |
| 167 | +
|
| 168 | +// check payload signature against header signature |
| 169 | +if ($header_signature != $payload_signature) { |
| 170 | + error_log('FAILED - payload signature'); |
| 171 | + exit(); |
| 172 | +} |
| 173 | +
|
| 174 | +// convert json to array |
| 175 | +$decoded = json_decode($payload, true); |
| 176 | +
|
| 177 | +// check for json decode errors |
| 178 | +if (json_last_error() !== JSON_ERROR_NONE) { |
| 179 | + error_log('FAILED - json decode - '. json_last_error()); |
| 180 | + exit(); |
| 181 | +} |
| 182 | +
|
| 183 | +// success, do something |
| 184 | +``` |
| 185 | + |
| 186 | +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. |
0 commit comments