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