fix hooks
authorMichael Wallner <mike@php.net>
Mon, 11 May 2015 12:30:04 +0000 (14:30 +0200)
committerMichael Wallner <mike@php.net>
Mon, 11 May 2015 12:30:04 +0000 (14:30 +0200)
app/Controller/Github/RepoHook.php
app/Github/API/Call.php
app/Github/API/Hooks/CreateHook.php
app/Github/API/Hooks/UpdateHook.php [new file with mode: 0644]

index 8ab1eed050a04575af2855b8e30e0bc078a9b5d7..6733b3f680e6b3cc247a048b14d4fc0bb41168a9 100644 (file)
@@ -34,35 +34,50 @@ class RepoHook extends Github
        
        function addHook($owner, $repo) {
                $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"));
-                       }
-                       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"));
-                       }
+               $call = $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo, &$call) {
+                       $call->dropFromCache();
+                       $this->redirectBack("$owner/$repo");
+               });
+               $call->send();
+       }
+       
+       function updateHook($owner, $repo) {
+               $this->github->fetchRepo("$owner/$repo", function($repo) {
+                       $call = $this->github->fetchHooks($repo->full_name, function($hooks, $links) use($repo, &$call) {
+                               $repo->hooks = $hooks;
+                               if (($hook = $this->checkRepoHook($repo))) {
+                                       $hook_conf = $this->app->getRequest()->getForm();
+                                       $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, $call) {
+                                               foreach ($changed_hook as $key => $val) {
+                                                       $hook->$key = $val;
+                                               }
+                                               $call->saveToCache([$hooks, $links]);
+                                               $this->redirectBack($repo->full_name);
+                                       });
+                               }
+                       });
                })->send();
        }
        
        function delHook($owner, $repo) {
                $this->github->fetchRepo("$owner/$repo", function($repo) {
-                       $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
+                       $call = $this->github->fetchHooks($repo->full_name, function($hooks) use($repo, &$call) {
                                $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));
-                                               }
+                                       $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, $call) {
+                                               $call->dropFromCache();
+                                               $this->redirectBack($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));
+               }
+       }
 }
index 3615851693f1870d2094ea70d8a3a018298326f6..ea9b59a70c461e7d0837e6e650c0eaf8c408e573 100644 (file)
@@ -67,7 +67,7 @@ abstract class Call
                }
                
                $this->enqueue($callback);
-               return $this->api->getClient();
+               return $this;
        }
        
        /**
@@ -79,6 +79,13 @@ abstract class Call
                return strtolower(end($parts));
        }
        
+       /**
+        * Call Client::send()
+        */
+       function send() {
+               return $this->api->getClient()->send();
+       }
+       
        /**
         * Get associated cache storage
         * @param int $ttl out param of configure ttl
@@ -92,8 +99,11 @@ abstract class Call
        }
        
        function getCacheKey() {
+               $args = $this->args;
+               unset($args["fresh"]);
+               ksort($args);
                return sprintf("github:%s:%s:%s", $this->api->getToken(), $this, 
-                       new QueryString($this->args));
+                       new QueryString($args));
        }
 
        function readFromCache(array &$cached = null, &$ttl = null) {
@@ -116,4 +126,11 @@ abstract class Call
                        $cache->set($key, $fresh, $ttl);
                }
        }
+       
+       function dropFromCache() {
+               if (($cache = $this->api->getCacheStorage())) {
+                       $key = $this->getCacheKey();
+                       $cache->del($key);
+               }
+       }
 }
index 325dca7fc32528a4e435724c4e7eea7a53bed0d4..866e93964236bdd12bccdb95c2025d5db3368553 100644 (file)
@@ -15,7 +15,8 @@ class CreateHook extends Call
                        "Accept" => $this->config->api->accept,
                        "Content-Type" => "application/json",
                ]);
-               
+
+               $events = [];
                if (!empty($this->args["conf"]["tag"])) {
                        $events[] = "create";
                }
@@ -35,7 +36,7 @@ class CreateHook extends Call
                ]));
                
                $this->api->getClient()->enqueue($request, function($response) use($callback) {
-                       if ($response->getReesponseCode() != 400 || null === ($json = json_decode($response->getBody()))) {
+                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
                                throw new RequestException($response);
                        }
                        $callback($json);
diff --git a/app/Github/API/Hooks/UpdateHook.php b/app/Github/API/Hooks/UpdateHook.php
new file mode 100644 (file)
index 0000000..cd249ba
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace app\Github\API\Hooks;
+
+class UpdateHook extends \app\Github\API\Call
+{
+       function enqueue(callable $callback) {
+               $url = $this->url->mod(uri_template("./repos/{+repo}/hooks{/id}", $this->args));
+               $request = new \http\Client\Request("PATCH", $url, [
+                       "Authorization" => "token ". $this->api->getToken(),
+                       "Accept" => $this->config->api->accept,
+                       "Content-Type" => "application/json",
+               ]);
+               
+               $events = [];
+               if (!empty($this->args["conf"]["tag"])) {
+                       $events[] = "create";
+               }
+               if (!empty($this->args["conf"]["release"])) {
+                       $events[] = "release";
+               }
+               
+               $request->getBody()->append(json_encode(compact("events")));
+               $this->api->getClient()->enqueue($request, function($response) use($callback) {
+                       if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
+                               throw new \app\Github\Exception\RequestException($response);
+                       }
+                       $callback($json);
+                       return true;
+               });
+       }
+}