github: fix notices
authorMichael Wallner <mike@php.net>
Fri, 16 Dec 2016 09:16:14 +0000 (10:16 +0100)
committerMichael Wallner <mike@php.net>
Fri, 20 Dec 2019 12:21:59 +0000 (13:21 +0100)
app/Controller/Github/Index.php
app/Controller/Github/Release.php
app/Controller/Github/Repo.php
app/Controller/Github/RepoHook.php
app/Github/API.php
app/Github/API/Call.php
app/Github/API/Hooks/CreateHook.php

index 8b6da0328abe80908c0c2e9dd99c7117854cb553..343c16f7e922b15be2942e3838ade6a726b29ec8 100644 (file)
@@ -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();
        }
 }
index e94e4ee9d8ed0590d46dc35d424b1ad96b342a5e..6743b19e7b480df968c21b7eeeb92db9e7fcc5a0 100644 (file)
@@ -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");
        }
 }
index bf61387330b295114da2172c854dd1359c9c6fe9..3c166ad51c5c8812fbf90e87a6fccd7820707868 100644 (file)
@@ -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")); 
index 96240f678015f891be45df43c623fcafe4fc9757..9d9ad2cbcabea571cc81ef2e068f2d06d6765479 100644 (file)
@@ -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));
                }
        }
 }
index bddd1672789b7d9b467dc0d6598ddea6982afa1b..8cff984c4707bb2645c7ecc5bcf06069a83f9442 100644 (file)
@@ -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;
index 4e0513db60b73a66212f9ac5a7e24c1df871fa5b..21fd45f016fb808802531baa5113baa37dc3ef40 100644 (file)
@@ -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);
                }
        }
 }
index 0d36ad66a621c32475b264bc2736f679e5dd8cd1..1367b70eaef85cfbac32f899a426bc1840e8d1e0 100644 (file)
@@ -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,