From: Michael Wallner Date: Fri, 16 Dec 2016 09:16:14 +0000 (+0100) Subject: github: fix notices X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=303950054385d4c55cfef9a8828ee79420b7366e;p=pharext%2Fpharext.org github: fix notices --- diff --git a/app/Controller/Github/Index.php b/app/Controller/Github/Index.php index 8b6da03..343c16f 100644 --- a/app/Controller/Github/Index.php +++ b/app/Controller/Github/Index.php @@ -8,17 +8,18 @@ use app\Github\API\Repos\ReposCallback; class Index extends Github { function __invoke(array $args = null) { - if ($this->checkToken()) { - $this->github->listRepos( - $this->app->getRequest()->getQuery("page") - )->then( - new ReposCallback($this->github) - )->done(function($results) { - list(list($repos, $links)) = $results; - $this->app->display("github/index", compact("repos", "links")); - }); - - $this->github->drain(); + if (!$this->checkToken()) { + return; } + $this->github->listRepos( + $this->app->getRequest()->getQuery("page") + )->then( + new ReposCallback($this->github) + )->done(function($results) { + list(list($repos, $links)) = $results; + $this->app->display("github/index", compact("repos", "links")); + }); + + $this->github->drain(); } } diff --git a/app/Controller/Github/Release.php b/app/Controller/Github/Release.php index e94e4ee..6743b19 100644 --- a/app/Controller/Github/Release.php +++ b/app/Controller/Github/Release.php @@ -8,36 +8,38 @@ use app\Github\API\Repos\RepoCallback; class Release extends Github { function __invoke(array $args = null) { + if (!$this->checkToken()) { + return; + } + extract($args); - if ($this->checkToken()) { - $this->github->readRepo("$owner/$name")->then( - new RepoCallback($this->github) - )->then(function($result) use(&$repo) { - list($repo,,,$releases) = $result; - $config = $this->app->getRequest()->getForm(); - - foreach ($releases as $release) { - if ($release->tag_name === $config["tag"]) { - return $this->github->uploadAssetForRelease($repo, $release, $config); - } + $this->github->readRepo("$owner/$name")->then( + new RepoCallback($this->github) + )->then(function($result) use(&$repo) { + list($repo,,,$releases) = $result; + $config = $this->app->getRequest()->getForm(); + + foreach ($releases as $release) { + if ($release->tag_name === $config["tag"]) { + return $this->github->uploadAssetForRelease($repo, $release, $config); } - - return $this->github->createReleaseFromTag($repo, $config["tag"], $config); - })->done(function() use(&$repo) { - $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name)); - }); - - $this->github->drain(); - - $hook = $this->github->checkRepoHook($repo); - $this->app->getView()->addData(compact("owner", "name", "repo", "hook")); - - if (($modal = $this->app->getRequest()->getQuery("modal"))) { - $action = $this->app->getRequest()->getQuery($modal); - $this->app->getView()->addData(compact("modal", "action")); } - $this->app->display("github/repo"); + return $this->github->createReleaseFromTag($repo, $config["tag"], $config); + })->done(function() use(&$repo) { + $this->app->redirect($this->app->getBaseUrl()->mod(":./github/repo/" . $repo->full_name)); + }); + + $this->github->drain(); + + $hook = $this->github->checkRepoHook($repo); + $this->app->getView()->addData(compact("owner", "name", "repo", "hook")); + + if (($modal = $this->app->getRequest()->getQuery("modal"))) { + $action = $this->app->getRequest()->getQuery($modal); + $this->app->getView()->addData(compact("modal", "action")); } + + $this->app->display("github/repo"); } } diff --git a/app/Controller/Github/Repo.php b/app/Controller/Github/Repo.php index bf61387..3c166ad 100644 --- a/app/Controller/Github/Repo.php +++ b/app/Controller/Github/Repo.php @@ -8,20 +8,22 @@ use app\Github\API\Repos\RepoCallback; class Repo extends Github { function __invoke(array $args = null) { - extract($args); - if ($this->checkToken()) { - $this->github->readRepo("$owner/$name")->then( - new RepoCallback($this->github) - )->done(function($result) use($owner, $name) { - list($repo) = $result; - $hook = $this->github->checkRepoHook($repo); - - $this->app->getView()->addData(compact("owner", "name", "repo", "hook")); - }); - - $this->github->drain(); + if (!$this->checkToken()) { + return; } + extract($args); + $this->github->readRepo("$owner/$name")->then( + new RepoCallback($this->github) + )->done(function($result) use($owner, $name) { + list($repo) = $result; + $hook = $this->github->checkRepoHook($repo); + + $this->app->getView()->addData(compact("owner", "name", "repo", "hook")); + }); + + $this->github->drain(); + if (($modal = $this->app->getRequest()->getQuery("modal"))) { $action = $this->app->getRequest()->getQuery($modal); $this->app->getView()->addData(compact("modal", "action")); diff --git a/app/Controller/Github/RepoHook.php b/app/Controller/Github/RepoHook.php index 96240f6..9d9ad2c 100644 --- a/app/Controller/Github/RepoHook.php +++ b/app/Controller/Github/RepoHook.php @@ -8,19 +8,20 @@ use app\Github\API\Hooks\ListHooks; 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 { - $this->changeHook($args)->done(function() use($args) { - $this->redirectBack($args["owner"]."/".$args["name"]); - }); - $this->github->drain(); - } + if (!$this->checkToken()) { + return; + } + 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 { + $this->changeHook($args)->done(function() use($args) { + $this->redirectBack($args["owner"]."/".$args["name"]); + }); + $this->github->drain(); } } @@ -79,7 +80,7 @@ class RepoHook extends Github 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)); + $this->app->redirect($this->app->getBaseUrl()->mod(":./github/repo/" . $repo)); } } } diff --git a/app/Github/API.php b/app/Github/API.php index bddd167..8cff984 100644 --- a/app/Github/API.php +++ b/app/Github/API.php @@ -101,7 +101,14 @@ class API } function hasToken() { - return $this->tokens->get("access_token"); + if ($this->tokens->get("access_token", $token)) { + $access_token = $token->getValue(); + if (isset($access_token)) { + return true; + } + $this->dropToken(); + } + return false; } function setToken($token) { @@ -143,7 +150,7 @@ class API function fetchToken($code, $state) { if (!$this->tokens->get("state", $orig_state, true)) { if (isset($orig_state)) { - $this->logger->notice("State expired", $orig_state); + $this->logger->notice("State expired", compact("state", "orig_state")); throw new Exception\StateExpired($orig_state->getLTL()); } throw new Exception\StateNotSet; diff --git a/app/Github/API/Call.php b/app/Github/API/Call.php index 4e0513d..21fd45f 100644 --- a/app/Github/API/Call.php +++ b/app/Github/API/Call.php @@ -82,7 +82,10 @@ abstract class Call $this->request(), function($response) { try { - $this->deferred->resolve($this->response($response)); + $result = $this->response($response); + if (!($result instanceof Promise\PromiseInterface)) { + $this->deferred->resolve($result); + } } catch (\Exception $e) { $this->deferred->reject($e); } @@ -136,9 +139,17 @@ abstract class Call return false; } if (!$cache->get($key, $cached)) { + if ($cached) { + $this->api->getLogger()->debug( + sprintf("Cache-Stale: $this [TTL=%d]", $cached->getTTL()), + $this->args); + } else { + $this->api->getLogger()->debug("Cache-Miss: $this", $this->args); + } return false; } if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) { + $this->api->getLogger()->debug("Cache-Refresh: $this", $this->args); return false; } $this->api->getLogger()->debug("Cache-Hit: $this", $this->args); @@ -156,6 +167,7 @@ abstract class Call $key = $this->getCacheKey(); $cache->set($key, new Item($fresh, $ttl)); + $this->api->getLogger()->debug("Cache-Push: $this", $this->args); } } @@ -163,6 +175,7 @@ abstract class Call if (($cache = $this->api->getCacheStorage())) { $key = $this->getCacheKey(); $cache->del($key); + $this->api->getLogger()->debug("Cache-Drop: $this", $this->args); } } } diff --git a/app/Github/API/Hooks/CreateHook.php b/app/Github/API/Hooks/CreateHook.php index 0d36ad6..1367b70 100644 --- a/app/Github/API/Hooks/CreateHook.php +++ b/app/Github/API/Hooks/CreateHook.php @@ -10,7 +10,7 @@ use http\Client\Response; class CreateHook extends Call { function request() { - $url = $this->url->mod("./repos/". $this->args["repo"] ."/hooks"); + $url = $this->url->mod(":./repos/". $this->args["repo"] ."/hooks"); $request = new Request("POST", $url, [ "Authorization" => "token " . $this->api->getToken(), "Accept" => $this->config->api->accept,