start refactoring to promises
[pharext/pharext.org] / app / Github / API.php
index e56ada1ecb3941079ddf9a176a07130cc08ccabd..8e51489872e106c0ce83e6eb290bcca82a93c72f 100644 (file)
@@ -5,6 +5,7 @@ namespace app\Github;
 use app\Github\API;
 use app\Github\Storage;
 use app\Github\Exception;
+use app\Pharext;
 
 use merry\Config;
 
@@ -133,7 +134,7 @@ class API
                ], 0);
        }
 
-       function fetchToken($code, $state, callable $callback) {
+       function fetchToken($code, $state) {
                if (!$this->tokens->get("state", $orig_state, true)) {
                        if (isset($orig_state)) {
                                $this->logger->notice("State expired", $orig_state);
@@ -151,12 +152,12 @@ class API
                        "client_id" => $this->config->client->id,
                        "client_secret" => $this->config->client->secret,
                ]);
-               return $call($callback);
+               return $call();
        }
        
-       function readAuthUser(callable $callback) {
+       function readAuthUser() {
                $call = new API\Users\ReadAuthUser($this);
-               return $call($callback);
+               return $call();
        }
 
        function listRepos($page, callable $callback) {
@@ -239,4 +240,30 @@ class API
                $call = new API\Releases\ListReleaseAssets($this, compact("repo", "id"));
                return $call($callback);
        }
+
+       function uploadAssetForRelease($repo, $release, $config, callable $callback) {
+               return $this->listHooks($repo->full_name, function($hooks) use($release, $repo, $config, $callback) {
+                       $repo->hooks = $hooks;
+                       $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);
+                       $url = uri_template($release->upload_url, compact("name"));
+                       $this->createReleaseAsset($url, $phar, "application/phar", function($json) use($release, $repo, $callback) {
+                               if ($release->draft) {
+                                       $this->publishRelease($repo->full_name, $release->id, $release->tag_name, function($json) use($callback) {
+                                               $callback($json);
+                                       });
+                               } else {
+                                       $callback($json);
+                               }
+                       });
+               });
+       }
+
+       function createReleaseFromTag($repo, $tag_name, $config, callable $callback) {
+               return $this->createRelease($repo->full_name, $tag_name, function($json) use($repo, $callback) {
+                       $this->uploadAssetForRelease($repo, $json, $config, $callback);
+               });
+       }
+
 }