Merge 4c613385c5
into c49821adf1
This commit is contained in:
commit
df0c053abd
118
PHPCI/Plugin/Stash.php
Normal file
118
PHPCI/Plugin/Stash.php
Normal file
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2014, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link https://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Plugin;
|
||||
|
||||
use b8\View;
|
||||
use PHPCI\Builder;
|
||||
use PHPCI\Helper\Lang;
|
||||
use PHPCI\Model\Build;
|
||||
|
||||
/**
|
||||
* Stash Plugin - Provides simple stash capability to PHPCI.
|
||||
* @author Alasdair Campbell <alasdair@campbelldiagnostics.co.uk>
|
||||
* @package PHPCI
|
||||
* @subpackage Plugins
|
||||
*/
|
||||
class Stash implements \PHPCI\Plugin
|
||||
{
|
||||
/**
|
||||
* @var \PHPCI\Builder
|
||||
*/
|
||||
protected $phpci;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Model\Build
|
||||
*/
|
||||
protected $build;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Set up the plugin, configure options, etc.
|
||||
* @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->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the notification to stash.
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$required_keys = array('phpci_hostname', 'stash_hostname', 'stash_username', 'stash_auth_token');
|
||||
|
||||
if (count(array_diff($required_keys, array_keys(array_filter($this->options)))) != 0) {
|
||||
$this->phpci->log("All of the values '" . implode("', '", $required_keys) . " must be specified.");
|
||||
error_log("All of the values '" . implode("', '", $required_keys) . " must be specified.");
|
||||
return false;
|
||||
}
|
||||
|
||||
$phpciHostname = $this->options['phpci_hostname'];
|
||||
$stashHostname = $this->options['stash_hostname'];
|
||||
$stashPort = (isset($this->options['stash_port'])) ? $this->options['stash_port'] : 7990;
|
||||
$stashUsername = $this->options['stash_username'];
|
||||
$stashToken = $this->options['stash_auth_token'];
|
||||
|
||||
$buildStatus = $this->build->isSuccessful() ? "SUCCESSFUL" : "FAILED";
|
||||
$buildId = $this->build->getId();
|
||||
$commitId = $this->build->getCommitId();
|
||||
$projectName = $this->build->getProject()->getTitle();
|
||||
|
||||
$buildUrl = "http://" . $phpciHostname . "/build/view/" . $buildId;
|
||||
$url = 'http://' . $stashHostname . ':' . $stashPort;
|
||||
$url .= '/rest/build-status/1.0/commits/' . $commitId;
|
||||
|
||||
$data = array("state" => $buildStatus, "key" => $projectName . " (build $buildId)", "url" => $buildUrl);
|
||||
$data_string = json_encode($data);
|
||||
|
||||
$curlHeaders = array(
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($data_string),
|
||||
"X-Auth-User:$stashUsername",
|
||||
"X-Auth-Token:$stashToken"
|
||||
);
|
||||
$handle = curl_init($url);
|
||||
curl_setopt($handle, CURLOPT_HTTPHEADER, $curlHeaders);
|
||||
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);
|
||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$result = curl_exec($handle);
|
||||
$error = curl_error($handle);
|
||||
curl_close($handle);
|
||||
|
||||
$this->phpci->log("Sent to Stash.");
|
||||
|
||||
if ($result === false) {
|
||||
$this->phpci->log("Unknown issue with curl request $error");
|
||||
$this->phpci->log($result);
|
||||
return false;
|
||||
} else {
|
||||
$decoded = json_decode($result);
|
||||
if (array_key_exists('errors', $decoded)) {
|
||||
$this->phpci->log($result);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
154
Tests/PHPCI/Plugin/StashTest.php
Normal file
154
Tests/PHPCI/Plugin/StashTest.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
namespace PHPCI\Plugin\Tests;
|
||||
|
||||
use PHPCI\Plugin\Stash as StashPlugin;
|
||||
use Prophecy\PhpUnit\ProphecyTestCase;
|
||||
|
||||
class StashTest extends ProphecyTestCase
|
||||
{
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject $mockCiBuilder
|
||||
*/
|
||||
protected $mockCiBuilder;
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject $mockBuild
|
||||
*/
|
||||
protected $mockBuild;
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject $mockProject
|
||||
*/
|
||||
protected $mockProject;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mockProject = $this->prophesize('\PHPCI\Model\Project');
|
||||
$this->mockBuild = $this->prophesize('\PHPCI\Model\Build');
|
||||
$this->mockCiBuilder = $this->prophesize('\PHPCI\Builder');
|
||||
$this->mockBuild->getProject()->willReturn($this->mockProject);
|
||||
$this->mockProject->getTitle()->willReturn('Test Project');
|
||||
}
|
||||
|
||||
protected function getPlugin(array $options = array())
|
||||
{
|
||||
return new StashPlugin($this->mockCiBuilder->reveal(), $this->mockBuild->reveal(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginNoOptions()
|
||||
{
|
||||
$plugin = $this->getPlugin();
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginNoPhpciHost()
|
||||
{
|
||||
$options = array('stash_hostname' => 'stash',
|
||||
'stash_username' => 'username',
|
||||
'stash_password' => 'password'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginNoStashHost()
|
||||
{
|
||||
$options = array('phpci_hostname' => 'phpci',
|
||||
'stash_username' => 'username',
|
||||
'stash_password' => 'password'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginNoStashUsername()
|
||||
{
|
||||
$options = array('phpci_hostname' => 'phpci',
|
||||
'stash_hostname' => 'stash',
|
||||
'stash_password' => 'password'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginNoStashPassword()
|
||||
{
|
||||
$options = array('phpci_hostname' => 'phpci',
|
||||
'stash_hostname' => 'stash',
|
||||
'stash_username' => 'username'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginTokenFailed()
|
||||
{
|
||||
$this->mockBuild->isSuccessful()->willReturn('Failed');
|
||||
$this->mockBuild->getId()->willReturn(1);
|
||||
$this->mockBuild->getCommitId()->willReturn('000001');
|
||||
|
||||
$options = array('phpci_hostname' => 'phpci',
|
||||
'stash_hostname' => 'stash',
|
||||
'stash_username' => 'username',
|
||||
'stash_auth_token' => 'token'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = false;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers PHPUnit::execute
|
||||
*/
|
||||
public function testPluginTokenSuccessful()
|
||||
{
|
||||
$this->markTestIncomplete('Still to mock curl requests...???');
|
||||
$this->mockBuild->isSuccessful()->willReturn('Successful');
|
||||
$this->mockBuild->getId()->willReturn(2);
|
||||
$this->mockBuild->getCommitId()->willReturn('000002');
|
||||
|
||||
$options = array('phpci_hostname' => 'phpci',
|
||||
'stash_hostname' => 'stash',
|
||||
'stash_username' => 'username',
|
||||
'stash_auth_token' => 'token'
|
||||
);
|
||||
$plugin = $this->getPlugin($options);
|
||||
$this->assertInstanceOf('PHPCI\Plugin', $plugin);
|
||||
$returnValue = $plugin->execute();
|
||||
$expectedReturn = true;
|
||||
$this->assertEquals($expectedReturn, $returnValue);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue