php-censor/src/Model/Build/GitBuild.php

167 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Model\Build;
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;
/**
* Remote Git Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class GitBuild extends Build
{
/**
* 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();
}
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
$success = $this->cloneBySsh($builder, $buildPath);
} else {
$success = $this->cloneByHttp($builder, $buildPath);
}
2017-03-23 13:53:24 +01:00
if ($success) {
$success = $this->mergeBranches($builder, $buildPath);
}
if (!$success) {
$builder->logFailure('Failed to clone remote git repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
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) {
$builder->log('Fail merge branch origin/' . $branch, LogLevel::ERROR);
2017-03-23 13:53:24 +01:00
return false;
}
$builder->log('Merged branch origin/' . $branch, LogLevel::INFO);
2017-03-23 13:53:24 +01:00
}
}
return true;
}
/**
* Use an HTTP-based git clone.
*/
2013-10-10 02:01:06 +02:00
protected function cloneByHttp(Builder $builder, $cloneTo)
{
2017-04-13 17:52:03 +02:00
$cmd = 'cd .. && git clone --recursive ';
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings && isset($buildSettings['clone_depth'])) {
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
}
$cmd .= ' -b "%s" "%s" "%s"';
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
if ($success) {
$success = $this->postCloneSetup($builder, $cloneTo);
}
return $success;
}
/**
* Use an SSH-based git clone.
*/
2013-10-10 02:01:06 +02:00
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey($cloneTo);
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
2013-07-30 03:55:29 +02:00
// Do the git clone:
2017-04-13 17:52:03 +02:00
$cmd = 'cd .. && git clone --recursive ';
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings && isset($buildSettings['clone_depth'])) {
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
}
$cmd .= ' -b "%s" "%s" "%s"';
$cmd = 'export GIT_SSH="' . $gitSshWrapper . '" && ' . $cmd;
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
if ($success) {
$extra = [
'git_ssh_wrapper' => $gitSshWrapper
];
$success = $this->postCloneSetup($builder, $cloneTo, $extra);
}
// Remove the key file and git wrapper:
unlink($keyFile);
unlink($gitSshWrapper);
return $success;
}
/**
* Handle any post-clone tasks, like switching branches.
*
* @param Builder $builder
* @param string $cloneTo
* @param array $extra
*
* @return boolean
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commitId = $this->getCommitId();
$chdir = 'cd "%s"';
if (empty($this->getEnvironment()) && !empty($commitId)) {
$cmd = $chdir . ' && git checkout %s --quiet';
$success = $builder->executeCommand($cmd, $cloneTo, $commitId);
}
// Always update the commit hash with the actual HEAD hash
if ($builder->executeCommand($chdir . ' && git rev-parse HEAD', $cloneTo)) {
$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()));
}
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%ae %s', $cloneTo, $commitId)) {
$this->setCommitterEmail(trim($builder->getLastOutput()));
}
}
return $success;
}
}