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()))) {
54 $this->$evt($json)->done(function($result) use($response) {
55 list($created) = $result;
56 $response->setResponseCode(201);
57 $response->setHeader("Location", $created->url
);
59 $this->github
->drain();
61 $response->setResponseCode(415);
62 $response->setContentType($request->getHeader("Content-Type"));
63 $response->getBody()->append($request->getBody());
69 private function setTokenForUser($login) {
71 $this->accounts
->getTokens()->getRelation("accounts"),
72 $this->accounts
->getOwners()->getRelation("accounts")
74 $tokens = $this->accounts
->getTokens()->with($relations, [
76 "tokens.authority=" => "github",
80 $this->github
->setToken($tokens->current()->token
->get());
84 private function release($release) {
85 $response = $this->app
->getResponse();
87 if ($release->action
!== "published") {
88 $response->setResponseCode(202);
89 $response->getBody()->append("Not published");
90 } elseif (!empty($release->release
->assets
)) {
91 foreach ($release->release
->assets
as $asset) {
92 if ($asset->content_type
=== "application/phar") {
93 /* we've already uploaded the asset when we created the release */
94 $response->setResponseCode(202);
95 $response->getBody()->append("Already published");
101 $this->setTokenForUser($release->repository
->owner
->login
);
102 return $this->github
->uploadAssetForRelease(
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 return $this->github
->createReleaseFromTag(