From: Michael Wallner Date: Mon, 11 May 2015 12:30:04 +0000 (+0200) Subject: fix hooks X-Git-Url: https://git.m6w6.name/?p=pharext%2Fpharext.org;a=commitdiff_plain;h=a2af03951ac7fbd032609d0044c0a85704b77771 fix hooks --- diff --git a/app/Controller/Github/RepoHook.php b/app/Controller/Github/RepoHook.php index 8ab1eed..6733b3f 100644 --- a/app/Controller/Github/RepoHook.php +++ b/app/Controller/Github/RepoHook.php @@ -34,35 +34,50 @@ class RepoHook extends Github 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")); - } + $call = $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo, &$call) { + $call->dropFromCache(); + $this->redirectBack("$owner/$repo"); + }); + $call->send(); + } + + function updateHook($owner, $repo) { + $this->github->fetchRepo("$owner/$repo", function($repo) { + $call = $this->github->fetchHooks($repo->full_name, function($hooks, $links) use($repo, &$call) { + $repo->hooks = $hooks; + if (($hook = $this->checkRepoHook($repo))) { + $hook_conf = $this->app->getRequest()->getForm(); + $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, $call) { + foreach ($changed_hook as $key => $val) { + $hook->$key = $val; + } + $call->saveToCache([$hooks, $links]); + $this->redirectBack($repo->full_name); + }); + } + }); })->send(); } function delHook($owner, $repo) { $this->github->fetchRepo("$owner/$repo", function($repo) { - $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) { + $call = $this->github->fetchHooks($repo->full_name, function($hooks) use($repo, &$call) { $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)); - } + $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, $call) { + $call->dropFromCache(); + $this->redirectBack($repo->full_name); }); } }); })->send(); } + + function redirectBack($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/" . $repo)); + } + } } diff --git a/app/Github/API/Call.php b/app/Github/API/Call.php index 3615851..ea9b59a 100644 --- a/app/Github/API/Call.php +++ b/app/Github/API/Call.php @@ -67,7 +67,7 @@ abstract class Call } $this->enqueue($callback); - return $this->api->getClient(); + return $this; } /** @@ -79,6 +79,13 @@ abstract class Call return strtolower(end($parts)); } + /** + * Call Client::send() + */ + function send() { + return $this->api->getClient()->send(); + } + /** * Get associated cache storage * @param int $ttl out param of configure ttl @@ -92,8 +99,11 @@ abstract class Call } function getCacheKey() { + $args = $this->args; + unset($args["fresh"]); + ksort($args); return sprintf("github:%s:%s:%s", $this->api->getToken(), $this, - new QueryString($this->args)); + new QueryString($args)); } function readFromCache(array &$cached = null, &$ttl = null) { @@ -116,4 +126,11 @@ abstract class Call $cache->set($key, $fresh, $ttl); } } + + function dropFromCache() { + if (($cache = $this->api->getCacheStorage())) { + $key = $this->getCacheKey(); + $cache->del($key); + } + } } diff --git a/app/Github/API/Hooks/CreateHook.php b/app/Github/API/Hooks/CreateHook.php index 325dca7..866e939 100644 --- a/app/Github/API/Hooks/CreateHook.php +++ b/app/Github/API/Hooks/CreateHook.php @@ -15,7 +15,8 @@ class CreateHook extends Call "Accept" => $this->config->api->accept, "Content-Type" => "application/json", ]); - + + $events = []; if (!empty($this->args["conf"]["tag"])) { $events[] = "create"; } @@ -35,7 +36,7 @@ class CreateHook extends Call ])); $this->api->getClient()->enqueue($request, function($response) use($callback) { - if ($response->getReesponseCode() != 400 || null === ($json = json_decode($response->getBody()))) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { throw new RequestException($response); } $callback($json); diff --git a/app/Github/API/Hooks/UpdateHook.php b/app/Github/API/Hooks/UpdateHook.php new file mode 100644 index 0000000..cd249ba --- /dev/null +++ b/app/Github/API/Hooks/UpdateHook.php @@ -0,0 +1,32 @@ +url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args)); + $request = new \http\Client\Request("PATCH", $url, [ + "Authorization" => "token ". $this->api->getToken(), + "Accept" => $this->config->api->accept, + "Content-Type" => "application/json", + ]); + + $events = []; + if (!empty($this->args["conf"]["tag"])) { + $events[] = "create"; + } + if (!empty($this->args["conf"]["release"])) { + $events[] = "release"; + } + + $request->getBody()->append(json_encode(compact("events"))); + $this->api->getClient()->enqueue($request, function($response) use($callback) { + if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) { + throw new \app\Github\Exception\RequestException($response); + } + $callback($json); + return true; + }); + } +}