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, 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");
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->github
->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) {
57 $call->saveToCache([$hooks, $links]);
58 $this->redirectBack($repo->full_name
);
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->github
->checkRepoHook($repo))) {
70 $this->github
->deleteRepoHook($repo->full_name
, $hook->id
, function() use($repo, &$call) {
71 $call->dropFromCache();
72 $this->redirectBack($repo->full_name
);
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
));
83 $this->app
->redirect($this->app
->getBaseUrl()->mod("./github/repo/" . $repo));