add action to post-release a pharext package
authorMichael Wallner <mike@php.net>
Mon, 8 Jun 2015 17:49:11 +0000 (19:49 +0200)
committerMichael Wallner <mike@php.net>
Mon, 8 Jun 2015 17:49:11 +0000 (19:49 +0200)
app/Controller/Github/Hook/Receive.php
app/Controller/Github/Release.php [new file with mode: 0644]
app/Github/API.php
app/Pharext/Package.php [new file with mode: 0644]
app/views/github/repo.phtml
config/app.ini
config/routes.ini

index 4af889993cb58d8a45ce66131519b437573cd7e1..fa631e2d97f484d71b3fe5a9f57f13205d7fa5b5 100644 (file)
@@ -64,18 +64,31 @@ class Receive implements Controller
                }
        }
        
-       function release($release) {
+       private function setTokenForUser($login) {
+               $relations = [
+                       $this->accounts->getTokens()->getRelation("accounts"),
+                       $this->accounts->getOwners()->getRelation("accounts")
+               ];
+               $tokens = $this->accounts->getTokens()->with($relations, [
+                       "login=" => $login,
+                       "tokens.authority=" => "github",
+               ]);
+
+               if (count($tokens)) {
+                       $this->github->setToken($tokens->current()->token->get());
+               }
+       }
+
+       private function release($release) {
+               $response = $this->app->getResponse();
+
                if ($release->action !== "published") {
-                       $response = $this->app->getResponse();
                        $response->setResponseCode(202);
                        $response->getBody()->append("Not published");
-                       return;
-               }
-               if (!empty($release->release->assets)) {
+               } elseif (!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 */
-                                       $response = $this->app->getResponse();
                                        $response->setResponseCode(202);
                                        $response->getBody()->append("Already published");
                                        return;
@@ -83,34 +96,34 @@ class Receive implements Controller
                        }
                }
                
-               $this->uploadAssetForRelease($release->release, $release->repository)->send();
+               $this->setTokenForUser($release->repository->owner->login);
+               $this->github->uploadAssetForRelease($release->release, $release->repository, function($json) use($response) {
+                       $response->setResponseCode(201);
+                       $response->setHeader("Location", $json->url);
+               })->send();
        }
        
-       private function uploadAssetForRelease($release, $repo) {
-               $this->setTokenForUser($repo->owner->login);
-               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) 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);
-                               }
-                       });
-               });
+       private function create($create) {
+               $response = $this->app->getResponse();
+
+               if ($create->ref_type !== "tag") {
+                       $response->setResponseCode(202);
+                       $response->getBody()->append("Not a tag");
+                       return;
+               }
+               
+               $this->setTokenForUser($create->repository->owner->login);
+               $this->github->createReleaseFromTag($create->repository, $create->ref, function($json) use($response) {
+                       $response->setResponseCode(201);
+                       $response->setHeader("Location", $json->url);
+               })->send();
        }
        
        private function createReleaseAsset($release, $repo) {
                $hook = $this->github->checkRepoHook($repo);
+               $phar = new Pharext\Package($repo->clone_url, $release->tag_name, $repo->name, $hook->config);
+               return $phar->getFile();
+
                $dir = (new Task\GitClone($repo->clone_url, $release->tag_name))->run();
                if (!empty($hook->config->pecl)) {
                        $src = new SoureDir\Pecl($dir);
@@ -127,38 +140,5 @@ class Receive implements Controller
                $file = (new Task\PharBuild($src, $meta))->run();
                return $file;
        }
-       
-       function create($create) {
-               if ($create->ref_type !== "tag") {
-                       $response = $this->app->getResponse();
-                       
-                       $response->setResponseCode(202);
-                       $response->getBody()->append("Not a tag");
-                       return;
-               }
-               
-               $this->createReleaseFromTag($create->ref, $create->repository)->send();
-       }
-       
-       private function setTokenForUser($login) {
-               $relations = [
-                       $this->accounts->getTokens()->getRelation("accounts"),
-                       $this->accounts->getOwners()->getRelation("accounts")
-               ];
-               $tokens = $this->accounts->getTokens()->with($relations, [
-                       "login=" => $login,
-                       "tokens.authority=" => "github",
-               ]);
-               
-               if (count($tokens)) {
-                       $this->github->setToken($tokens->current()->token->get());
-               }
-       }
-       
-       private function createReleaseFromTag($tag, $repo) {
-               $this->setTokenForUser($repo->owner->login);
-               return $this->github->createRelease($repo->full_name, $tag, function($json) use($repo) {
-                       $this->uploadAssetForRelease($json, $repo);
-               });
-       }
+
 }
diff --git a/app/Controller/Github/Release.php b/app/Controller/Github/Release.php
new file mode 100644 (file)
index 0000000..fd35505
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+namespace app\Controller\Github;
+
+use app\Controller\Github;
+use app\Github\API\Repos\RepoCallback;
+
+
+class Release extends Github
+{
+       function __invoke(array $args = null) {
+               extract($args);
+               if ($this->checkToken()) {
+                       list($repo) = $this->github->readRepo("$owner/$name", function($repo, $links = null) {
+                               call_user_func(new RepoCallback($this->github), $repo, $links);
+
+                               $this->github->listReleases($repo->full_name, null, function($releases) use($repo) {
+                                       $tag = $this->app->getRequest()->getForm("tag");
+                                       foreach ($releases as $r) {
+                                               if ($r->tag_name === $tag) {
+                                                       $this->github->uploadAssetForRelease($repo, $r, function() use($repo) {
+                                                               $this->app->redirect($this->app->getBaseUrl()->mod("./github/" . $repo->full_name));
+                                                       });
+                                                       return;
+                                               }
+                                       }
+                                       
+                                       $this->github->createReleaseFromTag($repo, $tag, function() use($repo) {
+                                               $this->app->redirect($this->app->getBaseUrl()->mod("./github/" . $repo->full_name));
+                                       });
+                               });
+                       })->send();
+                       
+                       $hook = $this->github->checkRepoHook($repo);
+
+                       $this->app->getView()->addData(compact("owner", "name", "repo", "hook"));
+
+                       if (($modal = $this->app->getRequest()->getQuery("modal"))) {
+                               $action = $this->app->getRequest()->getQuery($modal);
+                               $this->app->getView()->addData(compact("modal", "action"));
+                       }
+
+                       $this->app->display("github/repo");
+               }
+       }
+}
index e56ada1ecb3941079ddf9a176a07130cc08ccabd..bd28f3ababa56d1bb711a4c32d18e8c69791cc35 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;
 
@@ -239,4 +240,30 @@ class API
                $call = new API\Releases\ListReleaseAssets($this, compact("repo", "id"));
                return $call($callback);
        }
+
+       function uploadAssetForRelease($repo, $release, callable $callback) {
+               return $this->listHooks($repo->full_name, function($hooks) use($release, $repo, $callback) {
+                       $repo->hooks = $hooks;
+                       $hook = $this->checkRepoHook($repo);
+                       $phar = new Pharext\Package($repo->clone_url, $release->tag_name, $repo->name, $hook ? $hook->config : null);
+                       $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, callable $callback) {
+               return $this->createRelease($repo->full_name, $tag_name, function($json) use($repo, $callback) {
+                       $this->uploadAssetForRelease($repo, $json, $callback);
+               });
+       }
+
 }
diff --git a/app/Pharext/Package.php b/app/Pharext/Package.php
new file mode 100644 (file)
index 0000000..bb0d92c
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace app\Pharext;
+
+use pharext\Metadata;
+use pharext\SourceDir;
+use pharext\Task;
+
+class Package
+{
+       private $file;
+
+       function __construct($git_url, $tag_name, $pkg_name, $options) {
+               $dir = (new Task\GitClone($git_url, $tag_name))->run();
+               $src = !empty($options->pecl)
+                       ? new SourceDir\Pecl($dir)
+                       : new SourceDir\Git($dir);
+               $meta = Metadata::all() + [
+                       "name" => $pkg_name,
+                       "release" => $tag_name,
+                       "license" => $src->getLicense(),
+                       "stub" => "pharext_installer.php",
+                       "type" => !empty($options->zend) ? "zend_extension" : "extension",
+               ];
+               $this->file = (new Task\PharBuild($src, $meta))->run();
+       }
+
+       function __toString() {
+               return (string) $this->file;
+       }
+
+       function getFile() {
+               return $this->file;
+       }
+}
index 2245aa9d777e57554d8b14d21c6fed34e24468f4..709908ce64255fbfffddca29ef8b9a97fff1d55a 100644 (file)
                </ul>
                        </div>
                        <div class="col-sm-4">
-               <?php if (!empty($v->release->assets)) : ?>
                        <ul class="list-inline pull-right">
+                       <?php if (!empty($v->release->assets)) : ?>
                                <?php foreach ($v->release->assets as $asset) : ?>
                                <?php if (fnmatch("*.ext.phar", $asset->name)) : ?>
                                <li>
                                </li>
                                <?php endif; ?>
                                <?php endforeach; ?>
+                       <?php else: ?>
+                               <form method="post" action="<?= $baseUrl->mod("./github/repo/". $repo->full_name ."/release") ?>">
+                                       <input type="hidden" name="tag" value="<?= $this->e($v->tag->name) ?>">
+                                       <button class="btn btn-warning">
+                                               <span class="octicon octicon-package"></span>
+                                               Release PHARext package
+                                       </button>
+                               </form>
+                       <?php endif; ?>
                        </ul>
-               <?php endif; ?>
                        </div>
                </div>
        </div>
index 7b9ca3871b302575fd91693e9e0c838157a28876..24178f7926eeae8515f1664a9b976906fc1f2296 100644 (file)
@@ -13,11 +13,12 @@ github.hook.content_type = json
 github.hook.insecure_ssl = 0
 
 github.storage.token.ttl = 3600
-github.storage.cache.repo.ttl = 3600
-github.storage.cache.repos.ttl = 3600
-github.storage.cache.hooks.ttl = 3600
-github.storage.cache.tags.ttl = 3600
-github.storage.cache.releases.ttl = 3600
+github.storage.cache.readrepo.ttl = 3600
+github.storage.cache.listrepos.ttl = 3600
+github.storage.cache.listhooks.ttl = 3600
+github.storage.cache.listtags.ttl = 3600
+github.storage.cache.listreleases.ttl = 3600
+github.storage.cache.listreleaseassets.ttl = 3600
 
 github.log = github
 
index cb28b1e9846b285a1e915178685111eccdb05006..9adef8923004b4fe9572c1dc65c0c3c09caf0d2a 100644 (file)
@@ -22,6 +22,11 @@ POST[] = /github/repo/{owner}/{name}/hook/{action}
 ; if the user has to re-authenticate, we'll receive a GET because of a redirect
 GET[] = /github/repo/{owner}/{name}/hook/{action}
 
+[Github\Release]
+POST[] = /github/repo/{owner}/{name}/release
+; if the user has to re-authenticate, we'll receive a GET because of a redirect
+GET[] = /github/repo/{owner}/{name}/release
+
 [Github\Signin]
 GET[] = /github/signin