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()) {
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"]
19 $this->changeHook($args)->done(function() use($args) {
20 $this->redirectBack($args["owner"]."/".$args["name"]);
22 $this->github
->drain();
27 function changeHook($args) {
28 switch ($args["action"]) {
30 return $this->updateHook($args["owner"] ."/". $args["name"]);
32 return $this->addHook($args["owner"] ."/". $args["name"]);
34 return $this->delHook($args["owner"] ."/". $args["name"]);
36 throw new \
Exception("Unknown action ".$args["action"]);
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();
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;
53 if (!($hook = $this->github
->checkHook($hooks))) {
54 throw new \
Exception("Hook is not set");
57 return $this->github
->updateRepoHook($repo_name, $hook->id
, $this->app
->getRequest()->getForm());
58 })->then(function() use($listhooks) {
59 $listhooks->dropFromCache();
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;
68 if (!($hook = $this->github
->checkHook($hooks))) {
69 throw new \
Exception("Hook is not set");
72 return $this->github
->deleteRepoHook($repo_name, $hook->id
);
73 })->then(function() use($listhooks) {
74 $listhooks->dropFromCache();
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
));
82 $this->app
->redirect($this->app
->getBaseUrl()->mod("./github/repo/" . $repo));