2014-04-16 12:14:19 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Plugin;
|
2014-04-16 12:14:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Git plugin.
|
2017-01-10 18:46:19 +01:00
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2014-04-16 12:14:19 +02:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class Git extends Plugin
|
2014-04-16 12:14:19 +02:00
|
|
|
{
|
2016-04-21 06:58:09 +02:00
|
|
|
protected $actions = [];
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'git';
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Run the Git plugin.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-16 12:14:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
$buildPath = $this->builder->buildPath;
|
2014-04-16 12:14:19 +02:00
|
|
|
|
|
|
|
// Check if there are any actions to be run for the branch we're running on:
|
|
|
|
if (!array_key_exists($this->build->getBranch(), $this->actions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are, run them:
|
|
|
|
$curdir = getcwd();
|
|
|
|
chdir($buildPath);
|
|
|
|
|
|
|
|
$success = true;
|
|
|
|
foreach ($this->actions[$this->build->getBranch()] as $action => $options) {
|
|
|
|
if (!$this->runAction($action, $options)) {
|
|
|
|
$success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chdir($curdir);
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Determine which action to run, and run it.
|
|
|
|
* @param $action
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-21 06:58:09 +02:00
|
|
|
protected function runAction($action, array $options = [])
|
2014-04-16 12:14:19 +02:00
|
|
|
{
|
2014-05-02 16:06:53 +02:00
|
|
|
switch ($action) {
|
|
|
|
case 'merge':
|
|
|
|
return $this->runMergeAction($options);
|
|
|
|
|
|
|
|
case 'tag':
|
|
|
|
return $this->runTagAction($options);
|
|
|
|
|
|
|
|
case 'pull':
|
|
|
|
return $this->runPullAction($options);
|
|
|
|
|
|
|
|
case 'push':
|
|
|
|
return $this->runPushAction($options);
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle a merge action.
|
|
|
|
* @param $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 16:06:53 +02:00
|
|
|
protected function runMergeAction($options)
|
|
|
|
{
|
|
|
|
if (array_key_exists('branch', $options)) {
|
2014-05-12 19:57:55 +02:00
|
|
|
$cmd = 'cd "%s" && git checkout %s && git merge "%s"';
|
2016-07-22 09:05:34 +02:00
|
|
|
$path = $this->builder->buildPath;
|
|
|
|
return $this->builder->executeCommand($cmd, $path, $options['branch'], $this->build->getBranch());
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle a tag action.
|
|
|
|
* @param $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 16:06:53 +02:00
|
|
|
protected function runTagAction($options)
|
|
|
|
{
|
|
|
|
$tagName = date('Ymd-His');
|
2017-02-07 13:05:44 +01:00
|
|
|
$message = sprintf('Tag created by PHP Censor: %s', date('Y-m-d H:i:s'));
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('name', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$tagName = $this->builder->interpolate($options['name']);
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('message', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$message = $this->builder->interpolate($options['message']);
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
$cmd = 'git tag %s -m "%s"';
|
2016-07-22 09:05:34 +02:00
|
|
|
return $this->builder->executeCommand($cmd, $tagName, $message);
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle a pull action.
|
|
|
|
* @param $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 16:06:53 +02:00
|
|
|
protected function runPullAction($options)
|
|
|
|
{
|
|
|
|
$branch = $this->build->getBranch();
|
|
|
|
$remote = 'origin';
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('branch', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$branch = $this->builder->interpolate($options['branch']);
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('remote', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$remote = $this->builder->interpolate($options['remote']);
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
return $this->builder->executeCommand('git pull %s %s', $remote, $branch);
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle a push action.
|
|
|
|
* @param $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-02 16:06:53 +02:00
|
|
|
protected function runPushAction($options)
|
|
|
|
{
|
|
|
|
$branch = $this->build->getBranch();
|
|
|
|
$remote = 'origin';
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('branch', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$branch = $this->builder->interpolate($options['branch']);
|
2014-05-02 16:06:53 +02:00
|
|
|
}
|
2014-04-16 12:14:19 +02:00
|
|
|
|
2014-05-02 16:06:53 +02:00
|
|
|
if (array_key_exists('remote', $options)) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$remote = $this->builder->interpolate($options['remote']);
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
return $this->builder->executeCommand('git push %s %s', $remote, $branch);
|
2014-04-16 12:14:19 +02:00
|
|
|
}
|
|
|
|
}
|