2nd round
authorMichael Wallner <mike@php.net>
Wed, 10 Jun 2015 07:52:59 +0000 (09:52 +0200)
committerMichael Wallner <mike@php.net>
Wed, 10 Jun 2015 07:52:59 +0000 (09:52 +0200)
app/Controller/Github/Callback.php
app/Controller/Github/Hook/Receive.php
app/Controller/Github/Index.php
app/Controller/Github/Release.php
app/Controller/Github/Repo.php
app/Controller/Github/RepoHook.php
app/Github/API.php

index baa6c4d47389f0d4aba92eede281c180f2e742f0..fcd814f8995388ce93d663126794c1eb41107319 100644 (file)
@@ -45,18 +45,16 @@ class Callback extends Github
                $this->github->fetchToken(
                        $this->app->getRequest()->getQuery("code"),
                        $this->app->getRequest()->getQuery("state")
-               )->then(function($result) {
+               )->then(function($result) use (&$oauth) {
                        list($oauth) = $result;
                        $this->github->setToken($oauth->access_token);
-                       return $this->github->readAuthUser()->then(function($result) use($oauth) {
-                               list($user) = $result;
-                               return $this->persistUser($oauth, $user);
-                       });
-               })->done(function($result) {
-                       $this->login(...$result);
+                       return $this->github->readAuthUser();
+               })->done(function($result) use(&$oauth) {
+                       list($user) = $result;
+                       $this->login(...$this->persistUser($oauth, $user));
                });
 
-               $this->github->getClient()->send();
+               $this->github->drain();
        }
        
        private function persistUser($oauth, $user) {
index 3dc519a36290716b9ad0993a68f90775bd7d3dc6..9bc1323e1a1d4ce08ab65ea3edc86266835982e6 100644 (file)
@@ -51,7 +51,12 @@ class Receive implements Controller
                        case "create":
                        case "release":
                                if (($json = json_decode($request->getBody()))) {
-                                       $this->$evt($json);
+                                       $this->$evt($json)->done(function($result) use($response) {
+                                               list($created) = $result;
+                                               $response->setResponseCode(201);
+                                               $response->setHeader("Location", $created->url);
+                                       });
+                                       $this->github->drain();
                                } else {
                                        $response->setResponseCode(415);
                                        $response->setContentType($request->getHeader("Content-Type"));
@@ -94,15 +99,10 @@ class Receive implements Controller
                }
                
                $this->setTokenForUser($release->repository->owner->login);
-               $this->github->uploadAssetForRelease(
+               return $this->github->uploadAssetForRelease(
                        $release->release,
                        $release->repository
-               )->done(function($result) use($response) {
-                       list($created) = $result;
-                       $response->setResponseCode(201);
-                       $response->setHeader("Location", $created->url);
-               });
-               $this->github->drain();
+               );
        }
        
        private function create($create) {
@@ -115,14 +115,9 @@ class Receive implements Controller
                }
                
                $this->setTokenForUser($create->repository->owner->login);
-               $this->github->createReleaseFromTag(
+               return $this->github->createReleaseFromTag(
                        $create->repository, 
                        $create->ref
-               )->done(function($result) use($response) {
-                       list($created) = $result;
-                       $response->setResponseCode(201);
-                       $response->setHeader("Location", $created->url);
-               });
-               $this->github->drain();
+               );
        }
 }
index 7918e2df620a4152555ea9b0bd00bac740e775c0..8b6da0328abe80908c0c2e9dd99c7117854cb553 100644 (file)
@@ -17,6 +17,7 @@ class Index extends Github
                                list(list($repos, $links)) = $results;
                                $this->app->display("github/index", compact("repos", "links"));
                        });
+                       
                        $this->github->drain();
                }
        }
index 32da9b5dd5f109ea29e4502701ec4fa00ef99951..e94e4ee9d8ed0590d46dc35d424b1ad96b342a5e 100644 (file)
@@ -28,6 +28,7 @@ class Release extends Github
                        });
                        
                        $this->github->drain();
+                       
                        $hook = $this->github->checkRepoHook($repo);
                        $this->app->getView()->addData(compact("owner", "name", "repo", "hook"));
 
index 64fbb9dbb67459bcac988e5d9f5eb31cfaa0809e..bf61387330b295114da2172c854dd1359c9c6fe9 100644 (file)
@@ -18,6 +18,7 @@ class Repo extends Github
 
                                $this->app->getView()->addData(compact("owner", "name", "repo", "hook"));
                        });
+                       
                        $this->github->drain();
                }
 
index 14d3d69da48fa20e0b13b40f613f7e9938532009..6bef009e8ca3ecf414e9586bbed1a817bde338d8 100644 (file)
@@ -16,71 +16,63 @@ class RepoHook extends Github
                                        "query" => "modal=hook&hook=" . $args["action"]
                                ]));
                        } else {
-                               switch ($args["action"]) {
-                               case "upd":
-                                       $this->updateHook($args["owner"], $args["name"]);
-                                       break;
-                               
-                               case "add":
-                                       $this->addHook($args["owner"], $args["name"]);
-                                       break;
-
-                               case "del":
-                                       $this->delHook($args["owner"], $args["name"]);
-                                       break;
-                               }
+                               $this->changeHook($args)->done(function() use($args) {
+                                       $this->redirectBack($args["owner"]."/".$args["repo"]);
+                               });
+                               $this->github->drain();
                        }
                }
        }
+
+       function changeHook($args) {
+               switch ($args["action"]) {
+               case "upd":
+                       return $this->updateHook($args["owner"] ."/". $args["name"]);
+               case "add":
+                       return $this->addHook($args["owner"] ."/". $args["name"]);
+               case "del":
+                       return $this->delHook($args["owner"] ."/". $args["name"]);
+               default:
+                       throw new \Exception("Unknown action ".$args["action"]);
+               }
+       }
        
-       function addHook($owner, $repo) {
+       function addHook($repo_name) {
                $hook_conf = $this->app->getRequest()->getForm();
-               $this->github->createRepoHook("$owner/$repo", $hook_conf)->then(function($hook) use($owner, $repo) {
-                       $call = new ListHooks($this->github, ["repo" => "$owner/$repo", "fresh" => true]);
-                       $this->github->queue($call)->then(function() use($owner, $repo) {
-                               $this->redirectBack("$owner/$repo");
-                       });
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
                });
-               $this->github->drain();
        }
        
-       function updateHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo")->then(function($result) {
-                       list($repo) = $result;
-                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
-                       $this->github->queue($call)->then(function($result) use($repo, $call) {
-                               list($repo->hooks) = $result;
-                               if (($hook = $this->github->checkRepoHook($repo))) {
-                                       $hook_conf = $this->app->getRequest()->getForm();
-                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf)->then(function($hook_result) use($repo, $hook, $result, $call) {
-                                               list($changed_hook) = $hook_result;
-                                               foreach ($changed_hook as $key => $val) {
-                                                       $hook->$key = $val;
-                                               }
-                                               $call->saveToCache($result);
-                                               $this->redirectBack($repo->full_name);
-                                       });
-                               }
-                       });
+       function updateHook($repo_name) {
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
+                       list($hooks) = $result;
+
+                       if (!($hook = $this->github->checkHook($hooks))) {
+                               throw new \Exception("Hook is not set");
+                       }
+
+                       return $this->github->updateRepoHook($repo_name, $hook->id, $this->app->getRequest()->getForm());
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
                });
-               $this->github->drain();
        }
        
-       function delHook($owner, $repo) {
-               $this->github->readRepo("$owner/$repo")->then(function($result) {
-                       list($repo) = $result;
-                       $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
-                       $this->github->queue($call)->then(function($result) use($repo, $call) {
-                               list($repo->hooks) = $result;
-                               if (($hook = $this->github->checkRepoHook($repo))) {
-                                       $this->github->deleteRepoHook($repo->full_name, $hook->id)->then(function() use($repo, $call) {
-                                               $call->dropFromCache();
-                                               $this->redirectBack($repo->full_name);
-                                       });
-                               }
-                       });
+       function delHook($repo_name) {
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
+                       list($hooks) = $result;
+
+                       if (!($hook = $this->github->checkHook($hooks))) {
+                               throw new \Exception("Hook is not set");
+                       }
+                       
+                       return $this->github->deleteRepoHook($repo_name, $hook->id);
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
                });
-               $this->github->drain();
        }
        
        function redirectBack($repo) {
index 0ae7655e1f99e9606c5659950636281bb9ddeb01..493984dbc037f76220c556e109fa6618d49335bb 100644 (file)
@@ -184,13 +184,13 @@ class API
        }
 
        /**
-        * Check if the pharext webhook is set for the repo and return it
-        * @param object $repo
+        * Check if the pharext webhook is set and return it
+        * @param array $hooks
         * @return stdClass hook
         */
-       function checkRepoHook($repo) {
-               if (!empty($repo->hooks)) {
-                       foreach ($repo->hooks as $hook) {
+       function checkHook($hooks) {
+               if (!empty($hooks)) {
+                       foreach ($hooks as $hook) {
                                if ($hook->name === "web" && $hook->config->url === $this->config->hook->url) {
                                        return $hook;
                                }
@@ -199,6 +199,18 @@ class API
                return null;
        }
 
+       /**
+        * Check if the pharext webhook is set for the repo and return it
+        * @param object $repo
+        * @return stdClass hook
+        */
+       function checkRepoHook($repo) {
+               if (!empty($repo->hooks)) {
+                       return $this->checkHook($repo->hooks);
+               }
+               return null;
+       }
+
        function listHooks($repo) {
                return $this->queue(new API\Hooks\ListHooks($this, compact("repo")));
        }