3 namespace app\Controller\Github\Hook
;
7 use app\Model\Accounts
;
11 class Receive
implements Controller
17 function __construct(Web
$app, API
$github, Accounts
$accounts) {
19 $this->github
= $github;
20 $this->accounts
= $accounts;
23 function __invoke(array $args = []) {
24 $request = $this->app
->getRequest();
25 $response = $this->app
->getResponse();
27 if (!($sig = $request->getHeader("X-Hub-Signature")) ||
!($evt = $request->getHeader("X-Github-Event"))) {
28 $response->setResponseCode(400);
29 $response->setContentType("message/http");
30 $response->getBody()->append($request);
32 $key = $this->github
->getConfig()->client
->secret
;
33 foreach ((new Params($sig))->params
as $algo => $mac) {
34 if ($mac["value"] !== hash_hmac($algo, $request->getBody(), $key)) {
35 $response->setResponseCode(403);
36 $response->getBody()->append("Invalid signature");
44 $response->setResponseCode(202);
45 $response->getBody()->append("Not a configured event");
48 $response->setResponseCode(204);
49 $response->setResponseStatus("PONG");
53 if (($json = json_decode($request->getBody()))) {
56 $response->setResponseCode(415);
57 $response->setContentType($request->getHeader("Content-Type"));
58 $response->getBody()->append($request->getBody());
64 private function setTokenForUser($login) {
66 $this->accounts
->getTokens()->getRelation("accounts"),
67 $this->accounts
->getOwners()->getRelation("accounts")
69 $tokens = $this->accounts
->getTokens()->with($relations, [
71 "tokens.authority=" => "github",
75 $this->github
->setToken($tokens->current()->token
->get());
79 private function release($release) {
80 $response = $this->app
->getResponse();
82 if ($release->action
!== "published") {
83 $response->setResponseCode(202);
84 $response->getBody()->append("Not published");
85 } elseif (!empty($release->release
->assets
)) {
86 foreach ($release->release
->assets
as $asset) {
87 if ($asset->content_type
=== "application/phar") {
88 /* we've already uploaded the asset when we created the release */
89 $response->setResponseCode(202);
90 $response->getBody()->append("Already published");
96 $this->setTokenForUser($release->repository
->owner
->login
);
97 $this->github
->uploadAssetForRelease(
100 )->done(function($result) use($response) {
101 list($created) = $result;
102 $response->setResponseCode(201);
103 $response->setHeader("Location", $created->url
);
105 $this->github
->drain();
108 private function create($create) {
109 $response = $this->app
->getResponse();
111 if ($create->ref_type
!== "tag") {
112 $response->setResponseCode(202);
113 $response->getBody()->append("Not a tag");
117 $this->setTokenForUser($create->repository
->owner
->login
);
118 $this->github
->createReleaseFromTag(
121 )->done(function($result) use($response) {
122 list($created) = $result;
123 $response->setResponseCode(201);
124 $response->setHeader("Location", $created->url
);
126 $this->github
->drain();