Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit e48520d

Browse files
author
Jonathan Beliën
committed
Handle RELEASE event
Close #10
1 parent 36adadf commit e48520d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Create a `config.php` file in `config/application` directory :
2121
'endpoints' => [
2222
[
2323
'repository' => 'jbelien/myrepo',
24-
'branch' => 'master',
24+
'branch' => 'master', // required for PUSH event
2525
'run' => '',
2626
],
2727
],
@@ -42,4 +42,4 @@ You can provide as many endpoints as needed ! For instance, if you need to use t
4242
4. Put the link to the webhook in "Payload URL" : something like `http://YOUR_IP_ADDRESS/webhook` ; don't forget to add the `/webhook` after your IP address or domain name !
4343
5. Choose `application/json` as "Content type"
4444
6. I suggest to add a token in "Secret" (don't forget to define it in your `config.php` file)
45-
7. You only need to send the `push` events.
45+
7. You only need to send the `push` (or `release`) events.

src/Handler/WebhookHandler.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
4848
return $this->push();
4949
break;
5050

51+
case 'release':
52+
return $this->release();
53+
break;
54+
5155
default:
52-
return new TextResponse('ERROR: This webhook only supports PING and PUSH events!', 501);
56+
return new TextResponse('ERROR: This webhook only supports PING, PUSH, and RELEASE events!', 501);
5357
break;
5458
}
5559
}
@@ -103,4 +107,48 @@ private function push()
103107

104108
return new TextResponse(sprintf('WARNING: No endpoint found for "%s" (branch "%s")!', $repository, $branch), 404);
105109
}
110+
111+
private function release()
112+
{
113+
$repository = $this->payload['repository']['full_name'];
114+
115+
$out = 'DELIVERY: '.$this->delivery.PHP_EOL;
116+
$out .= 'BY: '.$this->payload['release']['author']['login'].PHP_EOL;
117+
$out .= '--------------------------------------------------'.PHP_EOL;
118+
119+
foreach ($this->config['endpoints'] as $endpoint) {
120+
if ($endpoint['repository'] === $repository) {
121+
$out .= 'REPOSITORY: '.$repository.PHP_EOL;
122+
$out .= 'RELEASE: '.($this->payload['release']['name'] ?? '').' ('.$this->payload['release']['tag_name'].')'.PHP_EOL;
123+
$out .= '--------------------------------------------------'.PHP_EOL;
124+
125+
if (!is_array($endpoint['run'])) {
126+
$endpoint['run'] = [$endpoint['run']];
127+
}
128+
129+
$status = 200;
130+
foreach ($endpoint['run'] as $i => $run) {
131+
$process = new Process($run);
132+
133+
$out .= '['.($i + 1).'] '.$run.PHP_EOL;
134+
135+
try {
136+
$process->mustRun();
137+
138+
$out .= $process->getOutput().PHP_EOL;
139+
} catch (ProcessFailedException $exception) {
140+
$status = 500;
141+
142+
$out .= $exception->getMessage().PHP_EOL;
143+
}
144+
145+
$out .= '--------------------------------------------------'.PHP_EOL;
146+
}
147+
148+
return new TextResponse($out, $status);
149+
}
150+
}
151+
152+
return new TextResponse(sprintf('WARNING: No endpoint found for "%s"!', $repository, $branch), 404);
153+
}
106154
}

0 commit comments

Comments
 (0)