14d3d69da48fa20e0b13b40f613f7e9938532009
[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)->then(function($hook) use($owner, $repo) {
39 $call = new ListHooks($this->github, ["repo" => "$owner/$repo", "fresh" => true]);
40 $this->github->queue($call)->then(function() use($owner, $repo) {
41 $this->redirectBack("$owner/$repo");
42 });
43 });
44 $this->github->drain();
45 }
46
47 function updateHook($owner, $repo) {
48 $this->github->readRepo("$owner/$repo")->then(function($result) {
49 list($repo) = $result;
50 $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
51 $this->github->queue($call)->then(function($result) use($repo, $call) {
52 list($repo->hooks) = $result;
53 if (($hook = $this->github->checkRepoHook($repo))) {
54 $hook_conf = $this->app->getRequest()->getForm();
55 $this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf)->then(function($hook_result) use($repo, $hook, $result, $call) {
56 list($changed_hook) = $hook_result;
57 foreach ($changed_hook as $key => $val) {
58 $hook->$key = $val;
59 }
60 $call->saveToCache($result);
61 $this->redirectBack($repo->full_name);
62 });
63 }
64 });
65 });
66 $this->github->drain();
67 }
68
69 function delHook($owner, $repo) {
70 $this->github->readRepo("$owner/$repo")->then(function($result) {
71 list($repo) = $result;
72 $call = new ListHooks($this->github, ["repo" => $repo->full_name]);
73 $this->github->queue($call)->then(function($result) use($repo, $call) {
74 list($repo->hooks) = $result;
75 if (($hook = $this->github->checkRepoHook($repo))) {
76 $this->github->deleteRepoHook($repo->full_name, $hook->id)->then(function() use($repo, $call) {
77 $call->dropFromCache();
78 $this->redirectBack($repo->full_name);
79 });
80 }
81 });
82 });
83 $this->github->drain();
84 }
85
86 function redirectBack($repo) {
87 if (($back = $this->app->getRequest()->getForm("returnback")) && isset($this->session->previous)) {
88 $this->app->redirect($this->app->getBaseUrl()->mod($this->session->previous));
89 } else {
90 $this->app->redirect($this->app->getBaseUrl()->mod("./github/repo/" . $repo));
91 }
92 }
93 }