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));
+ }
+ }
}
}
$this->enqueue($callback);
- return $this->api->getClient();
+ return $this;
}
/**
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
}
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) {
$cache->set($key, $fresh, $ttl);
}
}
+
+ function dropFromCache() {
+ if (($cache = $this->api->getCacheStorage())) {
+ $key = $this->getCacheKey();
+ $cache->del($key);
+ }
+ }
}
--- /dev/null
+<?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;
+ });
+ }
+}