From 5cc9ddb0682ea275a8a505f27d3cae2a95e089df Mon Sep 17 00:00:00 2001 From: "a.cianfarani" Date: Tue, 27 Aug 2013 18:35:10 +0200 Subject: [PATCH] Added campfire plugin to allow pushing messages --- PHPCI/Plugin/Campfire.php | 114 ++++++++++++++++++++++++++++++++++++++ README.md | 11 +++- 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 PHPCI/Plugin/Campfire.php diff --git a/PHPCI/Plugin/Campfire.php b/PHPCI/Plugin/Campfire.php new file mode 100644 index 00000000..78269520 --- /dev/null +++ b/PHPCI/Plugin/Campfire.php @@ -0,0 +1,114 @@ + + * @package PHPCI + * @subpackage Plugins + */ +class Campfire implements \PHPCI\Plugin +{ + private $args; + private $config; + private $directory; + + private $url; + private $authToken; + private $userAgent; + private $cookie; + private $verbose; + private $roomId; + + public function __construct(\PHPCI\Builder $phpci, array $options = array()) + { + $this->phpci = $phpci; + + $this->message = $options['message']; + $this->userAgent = "Phpci/1.0 (http://www.phptesting.org/)"; + $this->cookie = "phpcicookie"; + + $buildSettings = $phpci->getConfig('build_settings'); + if (isset($buildSettings['campfire'])) { + $campfire = $buildSettings['campfire']; + $this->url = $campfire['url']; + $this->authToken = $campfire['authToken']; + $this->roomId = $campfire['roomId']; + } else { + throw new \Exception("No connexion parameters given for Campfire plugin"); + } + + } + + public function execute() + { + $this->joinRoom($this->roomId); + $status = $this->speak($this->message,$this->roomId); + $this->leaveRoom($this->roomId); + return $status; + + } + public function joinRoom($roomId) + { + $this->_getPageByPost('/room/'.$roomId.'/join.json'); + } + + public function leaveRoom($roomId) + { + $this->_getPageByPost('/room/'.$roomId.'/leave.json'); + } + + public function logout() + { + // New API is stateless, no concept of logout + } + + public function speak($message, $roomId, $isPaste = false) + { + $page = '/room/'.$roomId.'/speak.json'; + if ($isPaste) { + $type = 'PasteMessage'; + } else { + $type = 'TextMessage'; + } + return $this->_getPageByPost($page, + array('message' => array('type' => $type, 'body' => $message))); + + } + + private function _getPageByPost($page, $data = null) + { + $url = $this->url . $page; + // The new API allows JSON, so we can pass + // PHP data structures instead of old school POST + $json = json_encode($data); + + // cURL init & config + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST,1); + curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); + curl_setopt($ch, CURLOPT_VERBOSE, $this->verbose); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_USERPWD, $this->authToken . ':x'); + curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json")); + curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); + + curl_setopt($ch, CURLOPT_POSTFIELDS,$json); + $output = curl_exec($ch); + + curl_close($ch); + + // We tend to get one space with an otherwise blank response + $output = trim($output); + if (strlen($output)) { + /* Responses are JSON. Decode it to a data structure */ + return json_decode($output); + } + // Simple 200 OK response (such as for joining a room) + // TODO: check for other result codes here + return true; + } +} diff --git a/README.md b/README.md index 917ec901..add3ddb9 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ _**Please be aware that PHPCI is a beta-release project, so whilst it is very st * PHP Copy/Paste Detector * PHP Code Sniffer * PHP Spec + * Atoum * You can mark directories for the plugins to ignore. * You can mark certain plugins as being allowed to fail (but still run.) @@ -80,8 +81,11 @@ Similar to Travis CI, to support PHPCI in your project, you simply need to add a mysql: host: "localhost" user: "root" - pass: "" - + pass: "" + campfire: + url: "https://youraccount.campfirenow.com" + authToken: "605b32dd" + roomId: "570102" setup: mysql: - "DROP DATABASE IF EXISTS test;" @@ -110,6 +114,9 @@ Similar to Travis CI, to support PHPCI in your project, you simply need to add a complete: mysql: - "DROP DATABASE IF EXISTS test;" + failure: + campfire: + message: "Phpci : build failed." As mentioned earlier, PHPCI is powered by plugins, there are several phases in which plugins can be run: