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 switch ($args["action"]) {
21 $this->updateHook($args["owner"], $args["name"]);
25 $this->addHook($args["owner"], $args["name"]);
29 $this->delHook($args["owner"], $args["name"]);
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");
44 $this->github
->drain();
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) {
60 $call->saveToCache($result);
61 $this->redirectBack($repo->full_name
);
66 $this->github
->drain();
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
);
83 $this->github
->drain();
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
));
90 $this->app
->redirect($this->app
->getBaseUrl()->mod("./github/repo/" . $repo));