if ($repo->hooks) {
foreach ($repo->hooks as $hook) {
if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
- return $hook->id;
+ return $hook;
}
}
}
+++ /dev/null
-<?php
-
-namespace app\Controller\Github;
-
-use app\Controller;
-use app\Github\API;
-use app\Model\Accounts;
-use app\Web;
-use http\Params;
-use pharext\Task;
-use pharext\SourceDir\Git;
-
-require_once __DIR__."/../../../vendor/m6w6/pharext/src/pharext/Version.php";
-
-class Hook implements Controller
-{
- private $app;
- private $github;
- private $accounts;
-
- function __construct(Web $app, API $github, Accounts $accounts) {
- $this->app = $app;
- $this->github = $github;
- $this->accounts = $accounts;
- }
-
- function __invoke(array $args = []) {
- $request = $this->app->getRequest();
- $response = $this->app->getResponse();
-
- if (!($sig = $request->getHeader("X-Hub-Signature")) || !($evt = $request->getHeader("X-Github-Event"))) {
- $response->setResponseCode(400);
- $response->setContentType("message/http");
- $response->getBody()->append($request);
- } else {
- $key = $this->github->getConfig()->client->secret;
- foreach ((new Params($sig))->params as $algo => $mac) {
- if ($mac["value"] !== hash_hmac($algo, $request->getBody(), $key)) {
- $response->setResponseCode(403);
- $response->getBody()->append("Invalid signature");
- return;
- }
- }
- }
-
- switch ($evt) {
- default:
- $response->setResponseCode(202);
- $response->getBody()->append("Not a configured event");
- break;
- case "ping";
- $response->setResponseCode(204);
- $response->setResponseStatus("PONG");
- break;
- case "create":
- case "release":
- if (($json = json_decode($request->getBody()))) {
- $this->$evt($json);
- } else {
- $response->setResponseCode(415);
- $response->setContentType($request->getHeader("Content-Type"));
- $response->getBody()->append($request->getBody());
- }
- break;
- }
- }
-
- function release($release) {
- if ($release->action !== "published") {
- $response = $this->app->getResponse();
-
- $response->setResponseCode(202);
- $response->getBody()->append("Not published");
- return;
- }
-
- $this->uploadAssetForRelease($release->release, $release->repository);
- }
-
- private function uploadAssetForRelease($release, $repo) {
- $this->setTokenForUser($repo->owner->login);
- $asset = $this->createReleaseAsset($release, $repo);
- // FIXME: use uri_template extension
- $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
- $url = str_replace("{?name}", "?name=$name", $release->upload_url);
- $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) {
- $response = $this->app->getResponse();
- $response->setResponseCode(201);
- $response->setHeader("Location", $json->url);
- })->send();
- }
-
- private function createReleaseAsset($release, $repo) {
- define("STDERR", fopen("/var/log/apache2/php_errors.log", "a"));
- $source = (new Task\GitClone($repo->clone_url, $release->tag_name))->run();
- $iterator = new Git($source);
- $meta = [
- "header" => sprintf("pharext v%s (c) Michael Wallner <mike@php.net>", \pharext\VERSION),
- "version" => \pharext\VERSION,
- "date" => date("Y-m-d"),
- "name" => $repo->name,
- "release" => $release->tag_name,
- "license" => @file_get_contents(current(glob($iterator->getBaseDir()."/LICENSE*"))),
- "stub" => "pharext_installer.php",
- "type" => false ? "zend_extension" : "extension",
- ];
- $file = (new Task\PharBuild($iterator, $meta))->run();
- return $file;
- }
-
- function create($create) {
- if ($create->ref_type !== "tag") {
- $response = $this->app->getResponse();
-
- $response->setResponseCode(202);
- $response->getBody()->append("Not a tag");
- return;
- }
-
- $this->createReleaseFromTag($create->ref, $create->repository);
- }
-
- private function setTokenForUser($login) {
- $relations = [
- $this->accounts->getTokens()->getRelation("accounts"),
- $this->accounts->getOwners()->getRelation("accounts")
- ];
- $tokens = $this->accounts->getTokens()->with($relations, [
- "login=" => $login,
- "tokens.authority=" => "github",
- ]);
-
- if (count($tokens)) {
- $this->github->setToken($tokens->current()->token->get());
- }
- }
-
- private function createReleaseFromTag($tag, $repo) {
- $this->setTokenForUser($repo->owner->login);
- $this->github->createRelease($repo->full_name, $tag, function($json) {
- $response = $this->app->getResponse();
- $response->setResponseCode(201);
- $response->setHeader("Location", $json->url);
- })->send();
- }
-}
--- /dev/null
+<?php
+
+namespace app\Controller\Github\Hook;
+
+use app\Controller;
+use app\Github\API;
+use app\Model\Accounts;
+use app\Web;
+use http\Params;
+use pharext\Task;
+use pharext\SourceDir\Git;
+
+require_once __DIR__."/../../../../vendor/m6w6/pharext/src/pharext/Version.php";
+
+class Receive implements Controller
+{
+ private $app;
+ private $github;
+ private $accounts;
+
+ function __construct(Web $app, API $github, Accounts $accounts) {
+ $this->app = $app;
+ $this->github = $github;
+ $this->accounts = $accounts;
+ }
+
+ function __invoke(array $args = []) {
+ $request = $this->app->getRequest();
+ $response = $this->app->getResponse();
+
+ if (!($sig = $request->getHeader("X-Hub-Signature")) || !($evt = $request->getHeader("X-Github-Event"))) {
+ $response->setResponseCode(400);
+ $response->setContentType("message/http");
+ $response->getBody()->append($request);
+ } else {
+ $key = $this->github->getConfig()->client->secret;
+ foreach ((new Params($sig))->params as $algo => $mac) {
+ if ($mac["value"] !== hash_hmac($algo, $request->getBody(), $key)) {
+ $response->setResponseCode(403);
+ $response->getBody()->append("Invalid signature");
+ return;
+ }
+ }
+ }
+
+ switch ($evt) {
+ default:
+ $response->setResponseCode(202);
+ $response->getBody()->append("Not a configured event");
+ break;
+ case "ping";
+ $response->setResponseCode(204);
+ $response->setResponseStatus("PONG");
+ break;
+ case "create":
+ case "release":
+ if (($json = json_decode($request->getBody()))) {
+ $this->$evt($json);
+ } else {
+ $response->setResponseCode(415);
+ $response->setContentType($request->getHeader("Content-Type"));
+ $response->getBody()->append($request->getBody());
+ }
+ break;
+ }
+ }
+
+ function release($release) {
+ if ($release->action !== "published") {
+ $response = $this->app->getResponse();
+
+ $response->setResponseCode(202);
+ $response->getBody()->append("Not published");
+ return;
+ }
+
+ $this->uploadAssetForRelease($release->release, $release->repository);
+ }
+
+ private function uploadAssetForRelease($release, $repo) {
+ $this->setTokenForUser($repo->owner->login);
+ $asset = $this->createReleaseAsset($release, $repo);
+ // FIXME: use uri_template extension
+ $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
+ $url = str_replace("{?name}", "?name=$name", $release->upload_url);
+ $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) {
+ $response = $this->app->getResponse();
+ $response->setResponseCode(201);
+ $response->setHeader("Location", $json->url);
+ })->send();
+ }
+
+ private function createReleaseAsset($release, $repo) {
+ define("STDERR", fopen("/var/log/apache2/php_errors.log", "a"));
+ $source = (new Task\GitClone($repo->clone_url, $release->tag_name))->run();
+ $iterator = new Git($source);
+ $meta = [
+ "header" => sprintf("pharext v%s (c) Michael Wallner <mike@php.net>", \pharext\VERSION),
+ "version" => \pharext\VERSION,
+ "date" => date("Y-m-d"),
+ "name" => $repo->name,
+ "release" => $release->tag_name,
+ "license" => @file_get_contents(current(glob($iterator->getBaseDir()."/LICENSE*"))),
+ "stub" => "pharext_installer.php",
+ "type" => false ? "zend_extension" : "extension",
+ ];
+ $file = (new Task\PharBuild($iterator, $meta))->run();
+ return $file;
+ }
+
+ function create($create) {
+ if ($create->ref_type !== "tag") {
+ $response = $this->app->getResponse();
+
+ $response->setResponseCode(202);
+ $response->getBody()->append("Not a tag");
+ return;
+ }
+
+ $this->createReleaseFromTag($create->ref, $create->repository);
+ }
+
+ private function setTokenForUser($login) {
+ $relations = [
+ $this->accounts->getTokens()->getRelation("accounts"),
+ $this->accounts->getOwners()->getRelation("accounts")
+ ];
+ $tokens = $this->accounts->getTokens()->with($relations, [
+ "login=" => $login,
+ "tokens.authority=" => "github",
+ ]);
+
+ if (count($tokens)) {
+ $this->github->setToken($tokens->current()->token->get());
+ }
+ }
+
+ private function createReleaseFromTag($tag, $repo) {
+ $this->setTokenForUser($repo->owner->login);
+ $this->github->createRelease($repo->full_name, $tag, function($json) {
+ $response = $this->app->getResponse();
+ $response->setResponseCode(201);
+ $response->setHeader("Location", $json->url);
+ })->send();
+ }
+}
+++ /dev/null
-<?php
-
-namespace app\Controller\Github\Repo;
-
-use app\Controller\Github;
-
-class Hook extends Github
-{
- function __invoke(array $args = null) {
- if ($this->checkToken()) {
- if ($this->app->getRequest()->getRequestMethod() != "POST") {
- // user had to re-authenticate, and was redirected here
- $this->app->redirect($this->app->getBaseUrl()->mod([
- "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
- "query" => "modal=hook&hook=" . $args["action"]
- ]));
- } else switch ($args["action"]) {
- case "upd":
- $this->updateHook($args["owner"], $args["name"]);
- break;
-
- case "add":
- $this->addHook($args["owner"], $args["name"]);
- break;
-
- case "del":
- $this->delHook($args["owner"], $args["name"]);
- break;
- }
- }
- }
-
- function addHook($owner, $repo) {
- $hook_conf = $this->app->getRequest()->getForm();
- $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo) {
- if (($cache = $this->github->getCacheStorage())) {
- $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
- }
- if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
- $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
- } else {
- $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
- }
- })->send();
- }
-
- function delHook($owner, $repo) {
- $this->github->fetchRepo("$owner/$repo", function($repo) {
- $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
- $repo->hooks = $hooks;
- if (($id = $this->checkRepoHook($repo))) {
- $this->github->deleteRepoHook($repo->full_name, $id, function() use($repo) {
- if (($cache = $this->github->getCacheStorage())) {
- $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
- }
- if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
- $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
- } else {
- $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
- }
- });
- }
- });
- })->send();
- }
-}
--- /dev/null
+<?php
+
+namespace app\Controller\Github;
+
+use app\Controller\Github;
+
+class RepoHook extends Github
+{
+ function __invoke(array $args = null) {
+ if ($this->checkToken()) {
+ if ($this->app->getRequest()->getRequestMethod() != "POST") {
+ // user had to re-authenticate, and was redirected here
+ $this->app->redirect($this->app->getBaseUrl()->mod([
+ "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
+ "query" => "modal=hook&hook=" . $args["action"]
+ ]));
+ } else {
+ switch ($args["action"]) {
+ case "upd":
+ $this->updateHook($args["owner"], $args["name"]);
+ break;
+
+ case "add":
+ $this->addHook($args["owner"], $args["name"]);
+ break;
+
+ case "del":
+ $this->delHook($args["owner"], $args["name"]);
+ break;
+ }
+ }
+ }
+ }
+
+ function addHook($owner, $repo) {
+ $hook_conf = $this->app->getRequest()->getForm();
+ $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo) {
+ if (($cache = $this->github->getCacheStorage())) {
+ $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
+ }
+ if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
+ $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
+ } else {
+ $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
+ }
+ })->send();
+ }
+
+ function delHook($owner, $repo) {
+ $this->github->fetchRepo("$owner/$repo", function($repo) {
+ $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
+ $repo->hooks = $hooks;
+ if (($hook = $this->checkRepoHook($repo))) {
+ $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo) {
+ if (($cache = $this->github->getCacheStorage())) {
+ $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
+ }
+ if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
+ $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
+ } else {
+ $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
+ }
+ });
+ }
+ });
+ })->send();
+ }
+}
new QueryString($this->args));
}
- function readFromCache(&$cached = null, &$ttl = null) {
+ function readFromCache(array &$cached = null, &$ttl = null) {
if (empty($this->args["fresh"]) && ($cache = $this->api->getCacheStorage())) {
$key = $this->getCacheKey();
return $cache->get($key, $cached, $ttl);
return false;
}
- function saveToCache($fresh) {
+ function saveToCache(array $fresh) {
if (($cache = $this->api->getCacheStorage())) {
if (isset($this->config->storage->cache->{$this}->ttl)) {
$ttl = $this->config->storage->cache->{$this}->ttl;
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache($json);
+ $this->saveToCache([$json]);
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache($json);
+ $this->saveToCache([$json]);
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache($json);
+ $this->saveToCache([$json]);
$callback($json);
return true;
});
<div class="col-md-12">
<div class="checkbox">
<label for="hook-tag">
- <input id="hook-tag" type="checkbox" name="tag" value="1">
+ <input id="hook-tag" type="checkbox" name="tag" value="1" <?= ($hook = $this->check($repo)) && in_array("create", $hook->events) ? "checked":"" ?>>
Automatically create a release when I push a tag.
</label>
</div>
<div class="checkbox">
<label for="hook-release">
- <input id="hook-release" type="checkbox" name="release" value="1">
+ <input id="hook-release" type="checkbox" name="release" value="1" <?= ($hook = $this->check($repo)) && in_array("release", $hook->events) ? "checked":"" ?>>
Automatically upload a PHARext package as an asset to a release.
</label>
</div>
[Github\Callback]
GET[] = /github/callback
-[Github\Hook]
+[Github\Hook\Receive]
POST[] = /github/hook
[Github\Index]
GET[] = /github/repo/{owner}/{name}
GET[] = /github/repo/{owner}/{name}/{page}
-[Github\Repo\Hook]
+[Github\RepoHook]
POST[] = /github/repo/{owner}/{name}/hook/{action}
; if the user has to re-authenticate, we'll receive a GET because of a redirect
GET[] = /github/repo/{owner}/{name}/hook/{action}