3 namespace app\Controller\Github
;
5 use app\Controller\Github
;
6 use app\Github\API\Hooks\ListHooks
;
8 class RepoHook
extends Github
10 function __invoke(array $args = null) {
11 if (!$this->checkToken()) {
14 if ($this->app
->getRequest()->getRequestMethod() != "POST") {
15 // user had to re-authenticate, and was redirected here
16 $this->app
->redirect($this->app
->getBaseUrl()->mod([
17 "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
18 "query" => "modal=hook&hook=" . $args["action"]
21 $this->changeHook($args)->done(function() use($args) {
22 $this->redirectBack($args["owner"]."/".$args["name"]);
24 $this->github
->drain();
28 function changeHook($args) {
29 switch ($args["action"]) {
31 return $this->updateHook($args["owner"] ."/". $args["name"]);
33 return $this->addHook($args["owner"] ."/". $args["name"]);
35 return $this->delHook($args["owner"] ."/". $args["name"]);
37 throw new \
Exception("Unknown action ".$args["action"]);
41 function addHook($repo_name) {
42 $hook_conf = $this->app
->getRequest()->getForm();
43 $listhooks = new ListHooks($this->github
, ["repo" => $repo_name]);
44 return $this->github
->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
45 $listhooks->dropFromCache();
49 function updateHook($repo_name) {
50 $listhooks = new ListHooks($this->github
, ["repo" => $repo_name]);
51 return $this->github
->queue($listhooks)->then(function($result) use($repo_name) {
52 list($hooks) = $result;
54 if (!($hook = $this->github
->checkHook($hooks))) {
55 throw new \
Exception("Hook is not set");
58 return $this->github
->updateRepoHook($repo_name, $hook->id
, $this->app
->getRequest()->getForm());
59 })->then(function() use($listhooks) {
60 $listhooks->dropFromCache();
64 function delHook($repo_name) {
65 $listhooks = new ListHooks($this->github
, ["repo" => $repo_name]);
66 return $this->github
->queue($listhooks)->then(function($result) use($repo_name) {
67 list($hooks) = $result;
69 if (!($hook = $this->github
->checkHook($hooks))) {
70 throw new \
Exception("Hook is not set");
73 return $this->github
->deleteRepoHook($repo_name, $hook->id
);
74 })->then(function() use($listhooks) {
75 $listhooks->dropFromCache();
79 function redirectBack($repo) {
80 if (($back = $this->app
->getRequest()->getForm("returnback")) && isset($this->session
->previous
)) {
81 $this->app
->redirect($this->app
->getBaseUrl()->mod($this->session
->previous
));
83 $this->app
->redirect($this->app
->getBaseUrl()->mod(":./github/repo/" . $repo));