From 42db3fc395a5a8807e70fef23b79acc9c5babd81 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 10:58:25 +0200 Subject: [PATCH 01/13] PHPCI/Plugin/XMPP.php : Add XMPP plugin notification to send notification via XMPP Network to recipients list --- PHPCI/Plugin/XMPP.php | 193 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 PHPCI/Plugin/XMPP.php diff --git a/PHPCI/Plugin/XMPP.php b/PHPCI/Plugin/XMPP.php new file mode 100644 index 00000000..2fb3ae63 --- /dev/null +++ b/PHPCI/Plugin/XMPP.php @@ -0,0 +1,193 @@ + +* @package PHPCI +* @subpackage Plugins +*/ +class XMPP implements \PHPCI\Plugin +{ + protected $directory; + protected $args; + protected $phpci; + protected $build; + + /** + * @var string, username of sender account xmpp + */ + protected $username; + + /** + * @var string, alias server of sender account xmpp + */ + protected $server; + + /** + * @var string, password of sender account xmpp + */ + protected $password; + + /** + * @var string, alias for sender + */ + protected $alias; + + /** + * @var string, use tls + */ + protected $tls; + + /** + * @var array, list of recipients xmpp accounts + */ + protected $recipients + + /** + * + * @param Builder $phpci + * @param Build $build + * @param array $options + */ + public function __construct(Builder $phpci, Build $build, array $options = array()) + { + $this->phpci = $phpci; + $this->build = $build; + + $this->username = ''; + $this->password = ''; + $this->server = ''; + $this->alias = ''; + $this->recipients= array(); + $this->tls = false; + + /* + * Set recipients list + */ + if(!empty($options['recipients'])) { + if(is_string($options['recipients'])) { + $this->recipients = array($options['recipients']); + } elseif(is_array($options['recipients'])) { + $this->recipients = $options['recipients']; + } + } + + $this->setOptions($options); + } + + /** + * Set options configuration for plugin + * + * @param array $options + */ + protected function setOptions($options) + { + foreach (array('username', 'password', 'alias', 'tls', 'server') as $key) { + if (array_key_exists($key, $options)) { + $this->{$key} = $options[$key]; + } + } + } + + /** + * Get config format for sendxmpp config file + * + * @return string + */ + protected function getConfigFormat() + { + $conf = $this->username; + if(!empty($this->server)) { + $conf .= ';'.$this->server; + } + + $conf .= ' '.$this->password; + + if(!empty($this->alias)) { + $conf .= ' '.$this->alias; + } + + return $conf; + } + + /** + * Find config file for sendxmpp binary (default is ~/.sendxmpp) + */ + public static function findConfigFile() + { + if (file_exists('~/.sendxmpp')) { + if( md5(file_get_contents('~/.sendxmpp')) !== + md5($this->getConfigFormat())) { + return null; + } + + return true; + } + + return null; + } + + /** + * Send notification message. + */ + public function execute() + { + $sendxmpp = $this->phpci->findBinary('/usr/bin/sendxmpp'); + + if (!$sendxmpp) { + $this->phpci->logFailure('Could not find sendxmpp.'); + return false; + } + + /* + * Try to build conf file + */ + if(is_null(self::findConfigFile())) { + file_put_contents('~/.sendxmpp', $this->getConfigFormat()); + chmod('~/.sendxmpp', 0600); + } + + /* + * Enabled ssl for connection + */ + $tls = ''; + if($this->tls) { + $tls = ' -t'; + } + + /* + * Build message from status build + */ + if($this->build->isSuccessful()) { + $message = "✔ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." successful"; + } else { + $message = "✘ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." failure"; + } + + /* + * Send XMPP notification for all recipients + */ + $success = false; + foreach($this->recipients as $recipient) { + $success = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . + ' %s %s', $message, $tls, $recipients); + print $this->phpci->getLastOutput(); + } + + return $success; + } +} From 7f05051dc73c2a7b710a6f001a2c9d7764ac1056 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 11:08:45 +0200 Subject: [PATCH 02/13] PHPCI/Plugin/XMPP.php : Add test on empty recipients list and add test to check that all command was successful --- PHPCI/Plugin/XMPP.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/PHPCI/Plugin/XMPP.php b/PHPCI/Plugin/XMPP.php index 2fb3ae63..9bef3402 100644 --- a/PHPCI/Plugin/XMPP.php +++ b/PHPCI/Plugin/XMPP.php @@ -151,6 +151,13 @@ class XMPP implements \PHPCI\Plugin return false; } + /* + * Without recipients we can't send notification + */ + if (count($this->recipients) == 0) { + return false; + } + /* * Try to build conf file */ @@ -181,13 +188,16 @@ class XMPP implements \PHPCI\Plugin /* * Send XMPP notification for all recipients */ - $success = false; + $success = array() foreach($this->recipients as $recipient) { - $success = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . - ' %s %s', $message, $tls, $recipients); + if($cmd = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . + ' %s %s', $message, $tls, $recipients)) { + $success[] = $cmd; + } + print $this->phpci->getLastOutput(); } - return $success; + return (count($success) === count($this->recipients)); } } From 40b52fb1bd73617d9be3f7f0f4e84e7cc4ffa44e Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 11:16:05 +0200 Subject: [PATCH 03/13] =?UTF-8?q?PHPCI/Plugin/XMPP.php=20:=20Add=20new=20p?= =?UTF-8?q?aram=20=C2=ABdateFormat=C2=BB=20to=20display=20date/hour=20in?= =?UTF-8?q?=20notification=20message.=20Move=20code=20to=20build=20message?= =?UTF-8?q?=20string=20in=20separate=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHPCI/Plugin/XMPP.php | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/PHPCI/Plugin/XMPP.php b/PHPCI/Plugin/XMPP.php index 9bef3402..261e95a1 100644 --- a/PHPCI/Plugin/XMPP.php +++ b/PHPCI/Plugin/XMPP.php @@ -55,6 +55,11 @@ class XMPP implements \PHPCI\Plugin */ protected $recipients + /** + * @var string, mask to format date + */ + protected $dateFormat + /** * * @param Builder $phpci @@ -72,6 +77,7 @@ class XMPP implements \PHPCI\Plugin $this->alias = ''; $this->recipients= array(); $this->tls = false; + $this->dateFormat= '%d/%m/%Y %l:%M %P'; /* * Set recipients list @@ -94,7 +100,8 @@ class XMPP implements \PHPCI\Plugin */ protected function setOptions($options) { - foreach (array('username', 'password', 'alias', 'tls', 'server') as $key) { + foreach (array('username', 'password', 'alias', 'tls', 'server', 'dateFormat') + as $key) { if (array_key_exists($key, $options)) { $this->{$key} = $options[$key]; } @@ -174,24 +181,13 @@ class XMPP implements \PHPCI\Plugin $tls = ' -t'; } - /* - * Build message from status build - */ - if($this->build->isSuccessful()) { - $message = "✔ [".$this->build->getProjectTitle()."] Build #". - $this->build->getId()." successful"; - } else { - $message = "✘ [".$this->build->getProjectTitle()."] Build #". - $this->build->getId()." failure"; - } - /* * Send XMPP notification for all recipients */ $success = array() foreach($this->recipients as $recipient) { if($cmd = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . - ' %s %s', $message, $tls, $recipients)) { + ' %s %s', $this->getMessage(), $tls, $recipients)) { $success[] = $cmd; } @@ -200,4 +196,25 @@ class XMPP implements \PHPCI\Plugin return (count($success) === count($this->recipients)); } + + /** + * Build message for notification + * + * @return string + */ + protected function getMessage() + { + $message = ''; + + if($this->build->isSuccessful()) { + $message = "✔ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." successful"; + } else { + $message = "✘ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." failure"; + } + + $message .= ' ('.strftime($this->dateFormat).')'; + return $message; + } } From 581f9d1bfa6327cddd8fb788507024663a52cbb4 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 11:20:10 +0200 Subject: [PATCH 04/13] PHPCI/Plugin/XMPP.php rename to PHPCI/Plugin/Xmpp.php --- PHPCI/Plugin/{XMPP.php => Xmpp.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename PHPCI/Plugin/{XMPP.php => Xmpp.php} (100%) diff --git a/PHPCI/Plugin/XMPP.php b/PHPCI/Plugin/Xmpp.php similarity index 100% rename from PHPCI/Plugin/XMPP.php rename to PHPCI/Plugin/Xmpp.php From 501b0138b3385e70a26842f078fe013347fe1c74 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 11:24:17 +0200 Subject: [PATCH 05/13] =?UTF-8?q?PHPCI/Plugin/Xmpp.php=20:=20Fix=20syntax?= =?UTF-8?q?=20variable=20=C2=ABdate=5Fformat=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHPCI/Plugin/Xmpp.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 261e95a1..fd487df3 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -53,12 +53,12 @@ class XMPP implements \PHPCI\Plugin /** * @var array, list of recipients xmpp accounts */ - protected $recipients + protected $recipients; /** * @var string, mask to format date */ - protected $dateFormat + protected $date_format; /** * @@ -71,13 +71,13 @@ class XMPP implements \PHPCI\Plugin $this->phpci = $phpci; $this->build = $build; - $this->username = ''; - $this->password = ''; - $this->server = ''; - $this->alias = ''; - $this->recipients= array(); - $this->tls = false; - $this->dateFormat= '%d/%m/%Y %l:%M %P'; + $this->username = ''; + $this->password = ''; + $this->server = ''; + $this->alias = ''; + $this->recipients = array(); + $this->tls = false; + $this->date_format = '%d/%m/%Y %l:%M %P'; /* * Set recipients list @@ -214,7 +214,7 @@ class XMPP implements \PHPCI\Plugin $this->build->getId()." failure"; } - $message .= ' ('.strftime($this->dateFormat).')'; + $message .= ' ('.strftime($this->date_format).')'; return $message; } } From 6f0763202b3ca5af455b8aef95c6d89aea0f772f Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 11:25:51 +0200 Subject: [PATCH 06/13] PHPCI/Plugin/Xmpp.php : Fix config file name of sendxmpp (sendxmpprc instead of sendxmpp) --- PHPCI/Plugin/Xmpp.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index fd487df3..dda88473 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -130,12 +130,12 @@ class XMPP implements \PHPCI\Plugin } /** - * Find config file for sendxmpp binary (default is ~/.sendxmpp) + * Find config file for sendxmpp binary (default is ~/.sendxmpprc) */ public static function findConfigFile() { - if (file_exists('~/.sendxmpp')) { - if( md5(file_get_contents('~/.sendxmpp')) !== + if (file_exists('~/.sendxmpprc')) { + if( md5(file_get_contents('~/.sendxmpprc')) !== md5($this->getConfigFormat())) { return null; } @@ -169,8 +169,8 @@ class XMPP implements \PHPCI\Plugin * Try to build conf file */ if(is_null(self::findConfigFile())) { - file_put_contents('~/.sendxmpp', $this->getConfigFormat()); - chmod('~/.sendxmpp', 0600); + file_put_contents('~/.sendxmpprc', $this->getConfigFormat()); + chmod('~/.sendxmpprc', 0600); } /* From edd1409a2ddfac85cbfc25c2b84953bbccd1f6b2 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 12:19:42 +0200 Subject: [PATCH 07/13] PHPCI/Plugin/Xmpp.php : Fix date_format variable --- PHPCI/Plugin/Xmpp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index dda88473..994bdc6e 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -100,7 +100,7 @@ class XMPP implements \PHPCI\Plugin */ protected function setOptions($options) { - foreach (array('username', 'password', 'alias', 'tls', 'server', 'dateFormat') + foreach (array('username', 'password', 'alias', 'tls', 'server', 'date_format') as $key) { if (array_key_exists($key, $options)) { $this->{$key} = $options[$key]; From c0e64f3ff02fd6d68759461081d10d0982fbb6ee Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 12:20:12 +0200 Subject: [PATCH 08/13] PHPCI/Plugin/Xmpp.php : Fix get configuration file method --- PHPCI/Plugin/Xmpp.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 994bdc6e..160a664a 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -130,12 +130,12 @@ class XMPP implements \PHPCI\Plugin } /** - * Find config file for sendxmpp binary (default is ~/.sendxmpprc) + * Find config file for sendxmpp binary (default is .sendxmpprc) */ - public static function findConfigFile() + public function findConfigFile() { - if (file_exists('~/.sendxmpprc')) { - if( md5(file_get_contents('~/.sendxmpprc')) !== + if (file_exists('.sendxmpprc')) { + if( md5(file_get_contents('.sendxmpprc')) !== md5($this->getConfigFormat())) { return null; } @@ -168,9 +168,9 @@ class XMPP implements \PHPCI\Plugin /* * Try to build conf file */ - if(is_null(self::findConfigFile())) { - file_put_contents('~/.sendxmpprc', $this->getConfigFormat()); - chmod('~/.sendxmpprc', 0600); + if(is_null($this->findConfigFile())) { + file_put_contents('.sendxmpprc', $this->getConfigFormat()); + chmod('.sendxmpprc', 0600); } /* From 33a85af0b7964e350e0c968cc9228b6afd6f80fd Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 12:24:24 +0200 Subject: [PATCH 09/13] PHPCI/Plugin/Xmpp.php : default date format is now %c --- PHPCI/Plugin/Xmpp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 160a664a..28c10920 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -77,7 +77,7 @@ class XMPP implements \PHPCI\Plugin $this->alias = ''; $this->recipients = array(); $this->tls = false; - $this->date_format = '%d/%m/%Y %l:%M %P'; + $this->date_format = '%c'; /* * Set recipients list From b84f3e2c2275dcc93430ac2ef835c69b51596c73 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 12:24:51 +0200 Subject: [PATCH 10/13] PHPCI/Plugin/Xmpp.php : Fix build message method --- PHPCI/Plugin/Xmpp.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 28c10920..9f4c0696 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -181,6 +181,11 @@ class XMPP implements \PHPCI\Plugin $tls = ' -t'; } + $message_file = uniqid('xmppmessage'); + if($this->buildMessage($message_file) === false) { + return false; + } + /* * Send XMPP notification for all recipients */ @@ -202,7 +207,7 @@ class XMPP implements \PHPCI\Plugin * * @return string */ - protected function getMessage() + protected function buildMessage($message_file) { $message = ''; @@ -215,6 +220,7 @@ class XMPP implements \PHPCI\Plugin } $message .= ' ('.strftime($this->date_format).')'; - return $message; + + return file_put_contents($message_file, $message); } } From 7b5dab6185ff5fc4a8ffd8a4c3c28dfda86561d2 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 12:25:05 +0200 Subject: [PATCH 11/13] PHPCI/Plugin/Xmpp.php : Fix command to send to system --- PHPCI/Plugin/Xmpp.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 9f4c0696..6cf4576f 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -189,17 +189,24 @@ class XMPP implements \PHPCI\Plugin /* * Send XMPP notification for all recipients */ - $success = array() + $cmd = $sendxmpp . "%s -f .sendxmpprc -m %s %s"; + $recipients = ''; foreach($this->recipients as $recipient) { - if($cmd = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . - ' %s %s', $this->getMessage(), $tls, $recipients)) { - $success[] = $cmd; - } - - print $this->phpci->getLastOutput(); + if(!empty($recipients)) + $recipients .= ' '; + $recipients .= $recipient; } - return (count($success) === count($this->recipients)); + $success = $this->phpci->executeCommand( + $cmd, $tls, $message_file, $recipients); + print $this->phpci->getLastOutput(); + + /* + * Remove temp message file + */ + $this->phpci->executeCommand("rm -rf ".$message_file); + + return $success; } /** From b02e2f0b66b9dbfd108d787fbf4c808b43195b0d Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 21:57:20 +0200 Subject: [PATCH 12/13] PHPCI/Plugin/Xmpp.php : Remove unused $args variable --- PHPCI/Plugin/Xmpp.php | 1 - 1 file changed, 1 deletion(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 6cf4576f..25ddbeee 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -21,7 +21,6 @@ use PHPCI\Model\Build; class XMPP implements \PHPCI\Plugin { protected $directory; - protected $args; protected $phpci; protected $build; From cdf5e46d542c6d8fc5d2aab203759fab27622000 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 21:57:40 +0200 Subject: [PATCH 13/13] PHPCI/Plugin/Xmpp.php : Use implode() method instead of foreach() to build recipients for command --- PHPCI/Plugin/Xmpp.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/PHPCI/Plugin/Xmpp.php b/PHPCI/Plugin/Xmpp.php index 25ddbeee..5de3106b 100644 --- a/PHPCI/Plugin/Xmpp.php +++ b/PHPCI/Plugin/Xmpp.php @@ -189,15 +189,11 @@ class XMPP implements \PHPCI\Plugin * Send XMPP notification for all recipients */ $cmd = $sendxmpp . "%s -f .sendxmpprc -m %s %s"; - $recipients = ''; - foreach($this->recipients as $recipient) { - if(!empty($recipients)) - $recipients .= ' '; - $recipients .= $recipient; - } + $recipients = implode(' ', $this->recipients); $success = $this->phpci->executeCommand( $cmd, $tls, $message_file, $recipients); + print $this->phpci->getLastOutput(); /*