start refactoring to promises
[pharext/pharext.org] / app / Github / API.php
index 1c9ff7834e772a92cc8fb758bb8701bfaae6bf88..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;
 
@@ -49,7 +50,8 @@ class API
        function __construct(Config $config, LoggerInterface $logger, Storage $tokens = null, Storage $cache = null) {
                $this->logger = $logger;
                $this->config = $config;
-               $this->client = new Client;
+               $this->client = new Client("curl", "github");
+               $this->client->configure($config->http->configure->toArray());
                $this->client->attach(new ClientObserver($logger));
                $this->tokens = $tokens ?: new Storage\Session;
                $this->cache = $cache;
@@ -132,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);
@@ -150,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) {
@@ -223,7 +225,12 @@ class API
                $call = new API\Releases\CreateRelease($this, compact("repo", "tag"));
                return $call($callback);
        }
-       
+
+       function publishRelease($repo, $id, $tag, callable $callback) {
+               $call = new API\Releases\PublishRelease($this, compact("repo", "id", "tag"));
+               return $call($callback);
+       }
+
        function createReleaseAsset($url, $asset, $type, callable $callback) {
                $call = new API\Releases\CreateReleaseAsset($this, compact("url", "asset", "type"));
                return $call($callback);
@@ -233,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);
+               });
+       }
+
 }