publish release *after* uploading the asset
[pharext/pharext.org] / app / Github / API / Hooks / CreateHook.php
1 <?php
2
3 namespace app\Github\API\Hooks;
4
5 use app\Github\API\Call;
6 use app\Github\Exception\RequestException;
7 use http\Client\Request;
8
9 class CreateHook extends Call
10 {
11 function enqueue(callable $callback) {
12 $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks");
13 $request = new Request("POST", $url, [
14 "Authorization" => "token " . $this->api->getToken(),
15 "Accept" => $this->config->api->accept,
16 "Content-Type" => "application/json",
17 ]);
18
19 $events = [];
20 if (!empty($this->args["conf"]["tag"])) {
21 $events[] = "create";
22 }
23 if (!empty($this->args["conf"]["release"])) {
24 $events[] = "release";
25 }
26
27 $request->getBody()->append(json_encode([
28 "name" => "web",
29 "events" => $events,
30 "config" => [
31 "zend" => (int)!empty($this->args["conf"]["zend"]),
32 "pecl" => (int)!empty($this->args["conf"]["pecl"]),
33 "url" => $this->config->hook->url,
34 "content_type" => $this->config->hook->content_type,
35 "insecure_ssl" => $this->config->hook->insecure_ssl,
36 "secret" => $this->config->client->secret, // FIXME: bad idea?
37 ]
38 ]));
39
40 $this->api->getClient()->enqueue($request, function($response) use($callback) {
41 if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
42 throw new RequestException($response);
43 }
44 $this->result = [$json];
45 $callback($json);
46 return true;
47 });
48 }
49 }