controllers: fix base url to omit scheme for when we connect through a https gateway
[pharext/pharext.org] / app / Controller / Github / RepoHook.php
index 8ab1eed050a04575af2855b8e30e0bc078a9b5d7..96240f678015f891be45df43c623fcafe4fc9757 100644 (file)
@@ -3,6 +3,7 @@
 namespace app\Controller\Github;
 
 use app\Controller\Github;
+use app\Github\API\Hooks\ListHooks;
 
 class RepoHook extends Github
 {
@@ -15,54 +16,70 @@ 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["name"]);
+                               });
+                               $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, function($hook) use($owner, $repo) {
-                       if (($cache = $this->github->getCacheStorage())) {
-                               $cache->del($this->github->getCacheKey("hooks:$owner/$repo"));
+               $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
+               return $this->github->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
+       }
+       
+       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");
                        }
-                       if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
-                               $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
-                       } else {
-                               $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/$owner/$repo"));
+
+                       return $this->github->updateRepoHook($repo_name, $hook->id, $this->app->getRequest()->getForm());
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
+       }
+       
+       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");
                        }
-               })->send();
+                       
+                       return $this->github->deleteRepoHook($repo_name, $hook->id);
+               })->then(function() use($listhooks) {
+                       $listhooks->dropFromCache();
+               });
        }
        
-       function delHook($owner, $repo) {
-               $this->github->fetchRepo("$owner/$repo", function($repo) {
-                       $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
-                               $repo->hooks = $hooks;
-                               if (($hook = $this->checkRepoHook($repo))) {
-                                       $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo) {
-                                               if (($cache = $this->github->getCacheStorage())) {
-                                                       $cache->del($this->github->getCacheKey("hooks:" . $repo->full_name));
-                                               }
-                                               if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
-                                                       $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
-                                               } else {
-                                                       $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo->full_name));
-                                               }
-                                       });
-                               }
-                       });
-               })->send();
+       function redirectBack($repo) {
+               if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
+                       $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
+               } else {
+                       $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo));
+               }
        }
 }