publish release *after* uploading the asset
authorMichael Wallner <mike@php.net>
Tue, 2 Jun 2015 12:03:21 +0000 (14:03 +0200)
committerMichael Wallner <mike@php.net>
Tue, 2 Jun 2015 12:03:21 +0000 (14:03 +0200)
this will ensure the asset is vivible in the timeline displaying the
release

app/Controller/Github/Hook/Receive.php
app/Github/API.php
app/Github/API/Hooks/CreateHook.php
app/Github/API/Hooks/UpdateHook.php
app/Github/API/Releases/CreateRelease.php
app/Github/API/Releases/PublishRelease.php [new file with mode: 0644]

index eda96fac3e5eff07fbd35c1c651b4f65db0768c0..1ac998477d3493898fa98716caf903653358f271 100644 (file)
@@ -72,23 +72,39 @@ class Receive implements Controller
                        $response->getBody()->append("Not published");
                        return;
                }
+               if (!empty($release->release->assets)) {
+                       foreach ($release->release->assets as $asset) {
+                               if ($asset->content_type === "application/phar") {
+                                       /* we've already uploaded the asset when we created the release */
+                                       return;
+                               }
+                       }
+               }
                
-               $this->uploadAssetForRelease($release->release, $release->repository);
+               $this->uploadAssetForRelease($release->release, $release->repository)->send();
        }
        
        private function uploadAssetForRelease($release, $repo) {
                $this->setTokenForUser($repo->owner->login);
-               $this->github->listHooks($repo->full_name, function($hooks) use($release, $repo) {
+               return $this->github->listHooks($repo->full_name, function($hooks) use($release, $repo) {
                        $repo->hooks = $hooks;
                        $asset = $this->createReleaseAsset($release, $repo);
                        $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
                        $url = uri_template($release->upload_url, compact("name"));
-                       $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) {
-                               $response = $this->app->getResponse();
-                               $response->setResponseCode(201);
-                               $response->setHeader("Location", $json->url);
+                       $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) use($release, $repo) {
+                               if ($release->draft) {
+                                       $this->github->publishRelease($repo->full_name, $release->id, $release->tag_name, function($json) {
+                                               $response = $this->app->getResponse();
+                                               $response->setResponseCode(201);
+                                               $response->setHeader("Location", $json->url);
+                                       });
+                               } else {
+                                       $response = $this->app->getResponse();
+                                       $response->setResponseCode(201);
+                                       $response->setHeader("Location", $json->url);
+                               }
                        });
-               })->send();
+               });
        }
        
        private function createReleaseAsset($release, $repo) {
@@ -119,7 +135,7 @@ class Receive implements Controller
                        return;
                }
                
-               $this->createReleaseFromTag($create->ref, $create->repository);
+               $this->createReleaseFromTag($create->ref, $create->repository)->send();
        }
        
        private function setTokenForUser($login) {
@@ -139,10 +155,8 @@ class Receive implements Controller
        
        private function createReleaseFromTag($tag, $repo) {
                $this->setTokenForUser($repo->owner->login);
-               $this->github->createRelease($repo->full_name, $tag, function($json) {
-                       $response = $this->app->getResponse();
-                       $response->setResponseCode(201);
-                       $response->setHeader("Location", $json->url);
-               })->send();
+               return $this->github->createRelease($repo->full_name, $tag, function($json) use($repo) {
+                       $this->uploadAssetForRelease($json, $repo);
+               });
        }
 }
index 522cf146c6b6090872917897c904d52270b1101e..e56ada1ecb3941079ddf9a176a07130cc08ccabd 100644 (file)
@@ -224,7 +224,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);
index 1579b5b4b73a9840cae9b1c4d739a94993b0b3ea..146929b6f3372f22953fb36a8fdef37a7873c7df 100644 (file)
@@ -28,8 +28,8 @@ class CreateHook extends Call
                        "name" => "web",
                        "events" => $events,
                        "config" => [
-                               "zend" => !empty($this->args["conf"]["zend"]),
-                               "pecl" => !empty($this->args["conf"]["pecl"]),
+                               "zend" => (int)!empty($this->args["conf"]["zend"]),
+                               "pecl" => (int)!empty($this->args["conf"]["pecl"]),
                                "url" => $this->config->hook->url,
                                "content_type" => $this->config->hook->content_type,
                                "insecure_ssl" => $this->config->hook->insecure_ssl,
index a891b13351c1d2382be41e720c6da99df2bff134..2d994b592ec88703feebc4b3b16aa5ff2c9d4afe 100644 (file)
@@ -20,8 +20,8 @@ class UpdateHook extends \app\Github\API\Call
                        $events[] = "release";
                }
                $config = [
-                       "zend" => !empty($this->args["conf"]["zend"]),
-                       "pecl" => !empty($this->args["conf"]["pecl"]),
+                       "zend" => (int)!empty($this->args["conf"]["zend"]),
+                       "pecl" => (int)!empty($this->args["conf"]["pecl"]),
                        "url" => $this->config->hook->url,
                        "content_type" => $this->config->hook->content_type,
                        "insecure_ssl" => $this->config->hook->insecure_ssl,
index 68854f7697d8c730f4060165c2dad2ba9fda2e86..1cb22221fb1df584989573acbb1d5d83820e985f 100644 (file)
@@ -17,6 +17,7 @@ class CreateRelease extends Call
                ]);
                $request->getBody()->append(json_encode([
                        "tag_name" => $this->args["tag"],
+                       "draft" => true,
                ]));
                $this->api->getClient()->enqueue($request, function($response) use($callback) {
                        if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
diff --git a/app/Github/API/Releases/PublishRelease.php b/app/Github/API/Releases/PublishRelease.php
new file mode 100644 (file)
index 0000000..39c5169
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+namespace app\Github\API\Releases;
+
+use app\Github\API\Call;
+use app\Github\Exception\RequestException;
+use http\Client\Request;
+
+class PublishRelease extends Call
+{
+       function enqueue(callable $callback) {
+               $url = $this->url->mod(uri_template("./repos/{+repo}/releases{/id}", $this->args));
+               $request = new Request("PATCH", $url, [
+                       "Authorization" => "token ". $this->api->getToken(),
+                       "Accept" => $this->config->api->accept,
+                       "Content-Type" => "application/json",
+               ]);
+               $request->getBody()->append(json_encode([
+                       "draft" => false,
+                       "tag_name" => $this->args["tag"],
+               ]));
+               $this->api->getClient()->enqueue($request, function($response) use($callback) {
+                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                               throw new RequestException($response);
+                       }
+                       $this->result = [$json];
+                       $callback($json);
+                       return true;
+               });
+       }
+}
\ No newline at end of file