aboutsummaryrefslogtreecommitdiff
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
parent32a54ab3d31dfe234186793e4ed610ec63d88f3c (diff)
downloadphorkie-f88c38723e3c807769627aff2866fc3cf5c8472f.tar.gz
phorkie-f88c38723e3c807769627aff2866fc3cf5c8472f.zip
webhook support
-rw-r--r--ChangeLog4
-rw-r--r--README.rst32
-rw-r--r--data/config.default.php4
-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
7 files changed, 128 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 35a5218..3109e7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-09-17 Christian Weiske <cweiske@cweiske.de>
+
+ * Add webhook support
+
2013-09-16 Elan Ruusamäe <glen@pld-linux.org>
* Disable editor on commit with --no-edit
diff --git a/README.rst b/README.rst
index 0e8cadf..8f43667 100644
--- a/README.rst
+++ b/README.rst
@@ -36,6 +36,7 @@ Features
- search across pastes: description, file names and file content
- options: quoting, logical and, or, not, partial words
+- webhook support - get notified when pastes are created, edited or deleted
============
@@ -208,6 +209,37 @@ It is possible to get this information for each single commit::
http://cweiske.de/
+Notifications via webhooks
+==========================
+Depending on how you use phorkie, it might be nice to notify some other service
+when pastes are added or updated.
+Phorkie contains a simply mechanism to post data to a given URL which
+you can then use as needed.
+
+The data are json-encoded POSTed to the URLs contained in the
+``$GLOBALS['phorkie']['cfg']['webhooks']`` setting array, with
+a MIME type of ``application/vnd.phorkie.webhook+json``::
+
+ {
+ 'event': 'create',
+ 'author': {
+ 'name':'Anonymous',
+ 'email': 'anonymous@phorkie',
+ },
+ 'repository': {
+ 'name': 'webhooktest',
+ 'url': 'http://example.org/33',
+ 'description': 'webhooktest',
+ 'owner': {
+ 'name': 'Anonymous',
+ 'email': 'anonymous@phorkie',
+ }
+ }
+ }
+
+The event may be ``create``, ``edit`` or ``delete``.
+
+
=================
Technical details
=================
diff --git a/data/config.default.php b/data/config.default.php
index b4c7d55..070070d 100644
--- a/data/config.default.php
+++ b/data/config.default.php
@@ -14,6 +14,10 @@ $GLOBALS['phorkie']['cfg'] = array(
'geshi' => 'MediaWiki/geshi/geshi/geshi.php',
'index' => 'new',//"new" or "list"
'perPage' => 10,
+ 'webhooks' => array(
+ /* array of urls that get called when
+ a paste is created, edited or deleted */
+ )
);
$GLOBALS['phorkie']['auth'] = array(
// 0 = public, no authentication, 1 = protect adds/edits/deletes,
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);
}
}