github: fix notices
[pharext/pharext.org] / app / Github / API.php
index c7a2b05075f18387fcc7878688aada799475642b..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) {
@@ -133,7 +140,7 @@ class API
                        "state" => $state,
                        "client_id" => $this->config->client->id,
                        "scope" => $this->config->client->scope,
-                       "redirect_uri" => $callback_url,
+                       "redirect_uri" => (string) $callback_url,
                ];
                return new Url("https://github.com/login/oauth/authorize", [
                        "query" => new QueryString($param)
@@ -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;
@@ -184,13 +191,13 @@ class API
        }
 
        /**
-        * Check if the pharext webhook is set for the repo and return it
-        * @param object $repo
+        * Check if the pharext webhook is set and return it
+        * @param array $hooks
         * @return stdClass hook
         */
-       function checkRepoHook($repo) {
-               if (!empty($repo->hooks)) {
-                       foreach ($repo->hooks as $hook) {
+       function checkHook($hooks) {
+               if (!empty($hooks)) {
+                       foreach ($hooks as $hook) {
                                if ($hook->name === "web" && $hook->config->url === $this->config->hook->url) {
                                        return $hook;
                                }
@@ -199,6 +206,18 @@ class API
                return null;
        }
 
+       /**
+        * Check if the pharext webhook is set for the repo and return it
+        * @param object $repo
+        * @return stdClass hook
+        */
+       function checkRepoHook($repo) {
+               if (!empty($repo->hooks)) {
+                       return $this->checkHook($repo->hooks);
+               }
+               return null;
+       }
+
        function listHooks($repo) {
                return $this->queue(new API\Hooks\ListHooks($this, compact("repo")));
        }
@@ -243,12 +262,16 @@ class API
                return $this->queue(new API\Releases\ListReleaseAssets($this, compact("repo", "id")));
        }
 
-       function uploadAssetForRelease($repo, $release, $config) {
+       function uploadAssetForRelease($repo, $release, $config = null) {
                return $this->listHooks($repo->full_name)->then(function($result) use($release, $repo, $config) {
                        list($repo->hooks) = $result;
-                       $hook = $this->checkRepoHook($repo);
-                       $phar = new Pharext\Package($repo->clone_url, $release->tag_name, $repo->name, $config ?: $hook->config);
-                       $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
+                       $phar = new Pharext\Package(
+                               $repo->clone_url, 
+                               $release->tag_name, 
+                               $repo->name, 
+                               $config ?: (array) $this->checkRepoHook($repo)->config
+                       );
+                       $name = $phar->build();
                        $url = uri_template($release->upload_url, compact("name"));
                        $promise = $this->createReleaseAsset($url, $phar, "application/phar");
                        if ($release->draft) {
@@ -260,7 +283,7 @@ class API
                });
        }
 
-       function createReleaseFromTag($repo, $tag_name, $config) {
+       function createReleaseFromTag($repo, $tag_name, $config = null) {
                return $this->createRelease($repo->full_name, $tag_name)->then(function($result) use($repo, $config) {
                        list($release) = $result;
                        return $this->uploadAssetForRelease($repo, $release, $config);