webhook management
[pharext/pharext.org] / app / Controller / Github / Repo.php
1 <?php
2
3 namespace app\Controller\Github;
4
5 use app\Controller\Github;
6
7 class Repo extends Github
8 {
9 function __invoke(array $args = null) {
10 extract($args);
11 if ($this->checkToken()) {
12 try {
13 $this->github->fetchRepo(
14 "$owner/$name",
15 [$this, "repoCallback"]
16 )->send();
17 } catch (\app\Github\Exception $exception) {
18 $this->app->getView()->addData(compact("exception", "owner", "name"));
19 }
20
21 if (($modal = $this->app->getRequest()->getQuery("modal"))) {
22 $this->app->getView()->addData(compact("modal") + [
23 "action" => $this->app->getRequest()->getQuery($modal)
24 ]);
25 }
26
27 $this->app->display("github/repo");
28 }
29 }
30
31 function repoCallback($repo, $links) {
32 $this->app->getView()->addData(compact("repo") + [
33 "title" => "Github: {$repo->name}"
34 ]);
35 settype($repo->tags, "object");
36 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
37 $repo->hooks = $hooks;
38 });
39 $this->github->fetchTags($repo->full_name, 1, $this->createTagsCallback($repo));
40 $this->github->fetchReleases($repo->full_name, 1, $this->createReleasesCallback($repo));
41 $this->github->fetchContents($repo->full_name, null, $this->createContentsCallback($repo));
42 }
43
44 function createReleasesCallback($repo) {
45 return function($releases, $links) use($repo) {
46 foreach ($releases as $release) {
47 $tag = $release->tag_name;
48 settype($repo->tags->$tag, "object");
49 $repo->tags->$tag->release = $release;
50 }
51 };
52 }
53
54 function createTagsCallback($repo) {
55 return function($tags, $links) use ($repo) {
56 foreach ($tags as $tag) {
57 $name = $tag->name;
58 settype($repo->tags->$name, "object");
59 $repo->tags->$name->tag = $tag;
60 }
61 };
62 }
63
64 function createContentsCallback($repo) {
65 return function($tree) use($repo) {
66 foreach ($tree as $entry) {
67 if ($entry->type !== "file" || $entry->size <= 0) {
68 continue;
69 }
70 if ($entry->name === "config.m4" || fnmatch("config?.m4", $entry->name)) {
71 $repo->config_m4 = $entry->name;
72 } elseif ($entry->name === "package.xml" || $entry->name === "package2.xml") {
73 $repo->package_xml = $entry->name;
74 } elseif ($entry->name === "pharext_package.php") {
75 $repo->pharext_package_php = $entry->name;
76 }
77 }
78 };
79 }
80 }