fix hooks
[pharext/pharext.org] / app / Controller / Github / RepoHook.php
1 <?php
2
3 namespace app\Controller\Github;
4
5 use app\Controller\Github;
6
7 class RepoHook extends Github
8 {
9 function __invoke(array $args = null) {
10 if ($this->checkToken()) {
11 if ($this->app->getRequest()->getRequestMethod() != "POST") {
12 // user had to re-authenticate, and was redirected here
13 $this->app->redirect($this->app->getBaseUrl()->mod([
14 "path" => "./github/repo/" . $args["owner"] ."/". $args["name"],
15 "query" => "modal=hook&hook=" . $args["action"]
16 ]));
17 } else {
18 switch ($args["action"]) {
19 case "upd":
20 $this->updateHook($args["owner"], $args["name"]);
21 break;
22
23 case "add":
24 $this->addHook($args["owner"], $args["name"]);
25 break;
26
27 case "del":
28 $this->delHook($args["owner"], $args["name"]);
29 break;
30 }
31 }
32 }
33 }
34
35 function addHook($owner, $repo) {
36 $hook_conf = $this->app->getRequest()->getForm();
37 $call = $this->github->createRepoHook("$owner/$repo", $hook_conf, function($hook) use($owner, $repo, &$call) {
38 $call->dropFromCache();
39 $this->redirectBack("$owner/$repo");
40 });
41 $call->send();
42 }
43
44 function updateHook($owner, $repo) {
45 $this->github->fetchRepo("$owner/$repo", function($repo) {
46 $call = $this->github->fetchHooks($repo->full_name, function($hooks, $links) use($repo, &$call) {
47 $repo->hooks = $hooks;
48 if (($hook = $this->checkRepoHook($repo))) {
49 $hook_conf = $this->app->getRequest()->getForm();
50 $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, $call) {
51 foreach ($changed_hook as $key => $val) {
52 $hook->$key = $val;
53 }
54 $call->saveToCache([$hooks, $links]);
55 $this->redirectBack($repo->full_name);
56 });
57 }
58 });
59 })->send();
60 }
61
62 function delHook($owner, $repo) {
63 $this->github->fetchRepo("$owner/$repo", function($repo) {
64 $call = $this->github->fetchHooks($repo->full_name, function($hooks) use($repo, &$call) {
65 $repo->hooks = $hooks;
66 if (($hook = $this->checkRepoHook($repo))) {
67 $this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, $call) {
68 $call->dropFromCache();
69 $this->redirectBack($repo->full_name);
70 });
71 }
72 });
73 })->send();
74 }
75
76 function redirectBack($repo) {
77 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
78 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
79 } else {
80 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo));
81 }
82 }
83 }