2nd round
[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 $this->changeHook($args)->done(function() use($args) {
20 $this->redirectBack($args["owner"]."/".$args["repo"]);
21 });
22 $this->github->drain();
23 }
24 }
25 }
26
27 function changeHook($args) {
28 switch ($args["action"]) {
29 case "upd":
30 return $this->updateHook($args["owner"] ."/". $args["name"]);
31 case "add":
32 return $this->addHook($args["owner"] ."/". $args["name"]);
33 case "del":
34 return $this->delHook($args["owner"] ."/". $args["name"]);
35 default:
36 throw new \Exception("Unknown action ".$args["action"]);
37 }
38 }
39
40 function addHook($repo_name) {
41 $hook_conf = $this->app->getRequest()->getForm();
42 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
43 return $this->github->createRepoHook($repo_name, $hook_conf)->then(function() use($listhooks) {
44 $listhooks->dropFromCache();
45 });
46 }
47
48 function updateHook($repo_name) {
49 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
50 return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
51 list($hooks) = $result;
52
53 if (!($hook = $this->github->checkHook($hooks))) {
54 throw new \Exception("Hook is not set");
55 }
56
57 return $this->github->updateRepoHook($repo_name, $hook->id, $this->app->getRequest()->getForm());
58 })->then(function() use($listhooks) {
59 $listhooks->dropFromCache();
60 });
61 }
62
63 function delHook($repo_name) {
64 $listhooks = new ListHooks($this->github, ["repo" => $repo_name]);
65 return $this->github->queue($listhooks)->then(function($result) use($repo_name) {
66 list($hooks) = $result;
67
68 if (!($hook = $this->github->checkHook($hooks))) {
69 throw new \Exception("Hook is not set");
70 }
71
72 return $this->github->deleteRepoHook($repo_name, $hook->id);
73 })->then(function() use($listhooks) {
74 $listhooks->dropFromCache();
75 });
76 }
77
78 function redirectBack($repo) {
79 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
80 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
81 } else {
82 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo));
83 }
84 }
85 }