2013-05-16 00:47:37 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Model\Build;
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Builder;
|
2017-03-23 13:53:24 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2013-05-16 00:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2017-02-07 13:50:09 +01:00
|
|
|
* Remote Git Build Model
|
2017-12-27 16:38:58 +01:00
|
|
|
*
|
2017-02-07 13:50:09 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
*/
|
2018-02-28 04:00:10 +01:00
|
|
|
class GitBuild extends Build
|
2013-05-16 00:47:37 +02:00
|
|
|
{
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get the URL to be used to clone this remote repository.
|
|
|
|
*/
|
2013-06-19 17:47:25 +02:00
|
|
|
protected function getCloneUrl()
|
|
|
|
{
|
|
|
|
return $this->getProject()->getReference();
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Create a working copy by cloning, copying, or similar.
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function createWorkingCopy(Builder $builder, $buildPath)
|
|
|
|
{
|
2014-05-13 17:15:33 +02:00
|
|
|
$key = trim($this->getProject()->getSshPrivateKey());
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
if (!empty($key)) {
|
|
|
|
$success = $this->cloneBySsh($builder, $buildPath);
|
|
|
|
} else {
|
|
|
|
$success = $this->cloneByHttp($builder, $buildPath);
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
if ($success) {
|
|
|
|
$success = $this->mergeBranches($builder, $buildPath);
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
if (!$success) {
|
|
|
|
$builder->logFailure('Failed to clone remote git repository.');
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2014-03-15 06:22:59 +01:00
|
|
|
return $this->handleConfig($builder, $buildPath);
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
/**
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param string $buildPath
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function mergeBranches(Builder $builder, $buildPath)
|
|
|
|
{
|
|
|
|
$branches = $this->getExtra('branches');
|
|
|
|
if (!empty($branches)) {
|
|
|
|
$cmd = 'cd "%s" && git merge --quiet origin/%s';
|
|
|
|
foreach ($branches as $branch) {
|
|
|
|
$success = $builder->executeCommand($cmd, $buildPath, $branch);
|
|
|
|
if (!$success) {
|
2018-04-15 10:48:50 +02:00
|
|
|
$builder->log('Fail merge branch origin/' . $branch, LogLevel::ERROR);
|
2017-03-23 13:53:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-15 10:48:50 +02:00
|
|
|
$builder->log('Merged branch origin/' . $branch, LogLevel::INFO);
|
2017-03-23 13:53:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Use an HTTP-based git clone.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function cloneByHttp(Builder $builder, $cloneTo)
|
2013-05-16 16:40:40 +02:00
|
|
|
{
|
2017-04-13 17:52:03 +02:00
|
|
|
$cmd = 'cd .. && git clone --recursive ';
|
2014-05-05 22:03:44 +02:00
|
|
|
|
2018-04-15 10:48:50 +02:00
|
|
|
$buildSettings = $builder->getConfig('build_settings');
|
|
|
|
if ($buildSettings && isset($buildSettings['clone_depth'])) {
|
|
|
|
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
|
2014-05-05 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 07:00:42 +01:00
|
|
|
$cmd .= ' -b "%s" "%s" "%s"';
|
2014-05-01 17:53:29 +02:00
|
|
|
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
|
2014-03-12 17:04:56 +01:00
|
|
|
|
2014-05-12 16:18:42 +02:00
|
|
|
if ($success) {
|
|
|
|
$success = $this->postCloneSetup($builder, $cloneTo);
|
2014-03-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 21:34:31 +01:00
|
|
|
return $success;
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Use an SSH-based git clone.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function cloneBySsh(Builder $builder, $cloneTo)
|
2013-05-16 16:40:40 +02:00
|
|
|
{
|
2017-02-01 16:27:47 +01:00
|
|
|
$keyFile = $this->writeSshKey($cloneTo);
|
|
|
|
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
|
2013-07-30 03:55:29 +02:00
|
|
|
|
2014-04-16 18:38:19 +02:00
|
|
|
// Do the git clone:
|
2017-04-13 17:52:03 +02:00
|
|
|
$cmd = 'cd .. && git clone --recursive ';
|
2014-05-05 22:03:44 +02:00
|
|
|
|
2018-04-15 10:48:50 +02:00
|
|
|
$buildSettings = $builder->getConfig('build_settings');
|
|
|
|
if ($buildSettings && isset($buildSettings['clone_depth'])) {
|
|
|
|
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
|
2014-05-05 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 07:00:42 +01:00
|
|
|
$cmd .= ' -b "%s" "%s" "%s"';
|
2018-03-16 00:49:35 +01:00
|
|
|
$cmd = 'export GIT_SSH="' . $gitSshWrapper . '" && ' . $cmd;
|
2014-04-16 18:38:19 +02:00
|
|
|
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
|
2014-02-25 11:16:58 +01:00
|
|
|
|
2014-05-12 16:18:42 +02:00
|
|
|
if ($success) {
|
2017-06-01 22:20:29 +02:00
|
|
|
$extra = [
|
|
|
|
'git_ssh_wrapper' => $gitSshWrapper
|
|
|
|
];
|
|
|
|
|
|
|
|
$success = $this->postCloneSetup($builder, $cloneTo, $extra);
|
2014-05-12 16:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the key file and git wrapper:
|
|
|
|
unlink($keyFile);
|
2017-02-01 16:27:47 +01:00
|
|
|
unlink($gitSshWrapper);
|
2014-05-12 16:18:42 +02:00
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handle any post-clone tasks, like switching branches.
|
2017-12-27 16:38:58 +01:00
|
|
|
*
|
2014-12-08 12:25:33 +01:00
|
|
|
* @param Builder $builder
|
2017-12-27 16:38:58 +01:00
|
|
|
* @param string $cloneTo
|
|
|
|
* @param array $extra
|
|
|
|
*
|
2018-02-23 11:40:56 +01:00
|
|
|
* @return boolean
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2017-06-01 22:20:29 +02:00
|
|
|
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
|
2014-05-12 16:18:42 +02:00
|
|
|
{
|
2017-04-13 17:00:29 +02:00
|
|
|
$success = true;
|
|
|
|
$commitId = $this->getCommitId();
|
|
|
|
$chdir = 'cd "%s"';
|
2014-02-24 21:34:31 +01:00
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
if (empty($this->getEnvironment()) && !empty($commitId)) {
|
|
|
|
$cmd = $chdir . ' && git checkout %s --quiet';
|
|
|
|
$success = $builder->executeCommand($cmd, $cloneTo, $commitId);
|
2015-04-21 14:51:55 +02:00
|
|
|
}
|
2014-07-10 15:27:15 +02:00
|
|
|
|
2015-04-21 14:51:55 +02:00
|
|
|
// Always update the commit hash with the actual HEAD hash
|
2017-12-27 16:38:58 +01:00
|
|
|
if ($builder->executeCommand($chdir . ' && git rev-parse HEAD', $cloneTo)) {
|
2017-04-13 17:00:29 +02:00
|
|
|
$commitId = trim($builder->getLastOutput());
|
|
|
|
|
|
|
|
$this->setCommitId($commitId);
|
|
|
|
|
|
|
|
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%s %s', $cloneTo, $commitId)) {
|
|
|
|
$this->setCommitMessage(trim($builder->getLastOutput()));
|
|
|
|
}
|
2017-12-27 16:38:58 +01:00
|
|
|
|
|
|
|
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%ae %s', $cloneTo, $commitId)) {
|
|
|
|
$this->setCommitterEmail(trim($builder->getLastOutput()));
|
|
|
|
}
|
2014-05-12 16:18:42 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
return $success;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
}
|