aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2013-09-17 16:58:09 +0200
committerChristian Weiske <cweiske@cweiske.de>2013-09-17 16:58:47 +0200
commitf88c38723e3c807769627aff2866fc3cf5c8472f (patch)
tree1958adfe5c2b768aaf8982cea8e2fc7c99b22f08 /src
parent32a54ab3d31dfe234186793e4ed610ec63d88f3c (diff)
downloadphorkie-f88c38723e3c807769627aff2866fc3cf5c8472f.tar.gz
phorkie-f88c38723e3c807769627aff2866fc3cf5c8472f.zip
webhook support
Diffstat (limited to 'src')
-rw-r--r--src/phorkie/Forker.php8
-rw-r--r--src/phorkie/Notificator.php71
-rw-r--r--src/phorkie/Repository.php7
-rw-r--r--src/phorkie/Repository/Post.php3
4 files changed, 88 insertions, 1 deletions
diff --git a/src/phorkie/Forker.php b/src/phorkie/Forker.php
index 157cb5e..f4e1295 100644
--- a/src/phorkie/Forker.php
+++ b/src/phorkie/Forker.php
@@ -8,6 +8,10 @@ class Forker
$new = $this->fork($repo->gitDir);
\copy($repo->gitDir . '/description', $new->gitDir . '/description');
$this->index($new);
+
+ $not = new Notificator();
+ $not->create($new);
+
return $new;
}
@@ -19,6 +23,10 @@ class Forker
'Fork of ' . $originalUrl
);
$this->index($new);
+
+ $not = new Notificator();
+ $not->create($new);
+
return $new;
}
diff --git a/src/phorkie/Notificator.php b/src/phorkie/Notificator.php
new file mode 100644
index 0000000..3ef5c81
--- /dev/null
+++ b/src/phorkie/Notificator.php
@@ -0,0 +1,71 @@
+<?php
+namespace phorkie;
+
+/**
+ * Send out webhook callbacks when something happens
+ */
+class Notificator
+{
+ /**
+ * A repository has been created
+ */
+ public function create(Repository $repo)
+ {
+ $this->send('create', $repo);
+ }
+
+ /**
+ * A repository has been modified
+ */
+ public function edit(Repository $repo)
+ {
+ $this->send('edit', $repo);
+ }
+
+ /**
+ * A repository has been deleted
+ */
+ public function delete(Repository $repo)
+ {
+ $this->send('delete', $repo);
+ }
+
+ /**
+ * Call webhook URLs with our payload
+ */
+ protected function send($event, Repository $repo)
+ {
+ if (count($GLOBALS['phorkie']['cfg']['webhooks']) == 0) {
+ return;
+ }
+
+ /* slightly inspired by
+ https://help.github.com/articles/post-receive-hooks */
+ $payload = (object) array(
+ 'event' => $event,
+ 'author' => array(
+ 'name' => $_SESSION['name'],
+ 'email' => $_SESSION['email']
+ ),
+ 'repository' => array(
+ 'name' => $repo->getTitle(),
+ 'url' => $repo->getLink('display', null, true),
+ 'description' => $repo->getDescription(),
+ 'owner' => $repo->getOwner()
+ )
+ );
+ foreach ($GLOBALS['phorkie']['cfg']['webhooks'] as $url) {
+ $req = new \HTTP_Request2($url);
+ $req->setMethod(\HTTP_Request2::METHOD_POST)
+ ->setHeader('Content-Type: application/vnd.phorkie.webhook+json')
+ ->setBody(json_encode($payload));
+ try {
+ $response = $req->send();
+ //FIXME log response codes != 200
+ } catch (HTTP_Request2_Exception $e) {
+ //FIXME log exceptions
+ }
+ }
+ }
+}
+?>
diff --git a/src/phorkie/Repository.php b/src/phorkie/Repository.php
index 815ef5e..f45c76f 100644
--- a/src/phorkie/Repository.php
+++ b/src/phorkie/Repository.php
@@ -208,8 +208,13 @@ class Repository
$db = new Database();
$db->getIndexer()->deleteRepo($this);
- return Tools::recursiveDelete($this->workDir)
+ $bOk = Tools::recursiveDelete($this->workDir)
&& Tools::recursiveDelete($this->gitDir);
+
+ $not = new Notificator();
+ $not->delete($this);
+
+ return $bOk;
}
public function getTitle()
diff --git a/src/phorkie/Repository/Post.php b/src/phorkie/Repository/Post.php
index e2e4e91..0860cad 100644
--- a/src/phorkie/Repository/Post.php
+++ b/src/phorkie/Repository/Post.php
@@ -154,8 +154,10 @@ class Repository_Post
//FIXME: index changed files only
//also handle file deletions
$db = new Database();
+ $not = new Notificator();
if ($bNew) {
$db->getIndexer()->addRepo($this->repo);
+ $not->create($this->repo);
} else {
$commits = $this->repo->getHistory();
$db->getIndexer()->updateRepo(
@@ -163,6 +165,7 @@ class Repository_Post
$commits[count($commits)-1]->committerTime,
$commits[0]->committerTime
);
+ $not->edit($this->repo);
}
}