From: Michael Wallner Date: Tue, 2 Jun 2015 12:03:21 +0000 (+0200) Subject: publish release *after* uploading the asset X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8b207c90c6c49876775ab61908f45510baf9981a;p=pharext%2Fpharext.org publish release *after* uploading the asset this will ensure the asset is vivible in the timeline displaying the release --- diff --git a/app/Controller/Github/Hook/Receive.php b/app/Controller/Github/Hook/Receive.php index eda96fa..1ac9984 100644 --- a/app/Controller/Github/Hook/Receive.php +++ b/app/Controller/Github/Hook/Receive.php @@ -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); + }); } } diff --git a/app/Github/API.php b/app/Github/API.php index 522cf14..e56ada1 100644 --- a/app/Github/API.php +++ b/app/Github/API.php @@ -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); diff --git a/app/Github/API/Hooks/CreateHook.php b/app/Github/API/Hooks/CreateHook.php index 1579b5b..146929b 100644 --- a/app/Github/API/Hooks/CreateHook.php +++ b/app/Github/API/Hooks/CreateHook.php @@ -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, diff --git a/app/Github/API/Hooks/UpdateHook.php b/app/Github/API/Hooks/UpdateHook.php index a891b13..2d994b5 100644 --- a/app/Github/API/Hooks/UpdateHook.php +++ b/app/Github/API/Hooks/UpdateHook.php @@ -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, diff --git a/app/Github/API/Releases/CreateRelease.php b/app/Github/API/Releases/CreateRelease.php index 68854f7..1cb2222 100644 --- a/app/Github/API/Releases/CreateRelease.php +++ b/app/Github/API/Releases/CreateRelease.php @@ -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 index 0000000..39c5169 --- /dev/null +++ b/app/Github/API/Releases/PublishRelease.php @@ -0,0 +1,31 @@ +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