Refactored project structure.

This commit is contained in:
Dmitry Khomutov 2018-03-04 18:04:15 +07:00
commit c015d8c58b
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
308 changed files with 39 additions and 47 deletions

View file

@ -0,0 +1,320 @@
<?php
namespace PHPCensor\Model\Build;
use GuzzleHttp\Client;
use PHPCensor\Builder;
use PHPCensor\Helper\Bitbucket;
use PHPCensor\Helper\Diff;
use PHPCensor\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
/**
* BitBucket Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class BitbucketBuild extends GitBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://bitbucket.org/' . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getTag();
}
/**
* Send status updates to any relevant third parties (i.e. Bitbucket)
*
* @return boolean
*/
public function sendStatusPostback()
{
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
return false;
}
$project = $this->getProject();
if (empty($project)) {
return false;
}
$username = Config::getInstance()->get('php-censor.bitbucket.username');
$appPassword = Config::getInstance()->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword) || empty($this->data['id'])) {
return false;
}
$allowStatusCommit = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.status.commit',
false
);
if (!$allowStatusCommit) {
return false;
}
switch ($this->getStatus()) {
case 0:
case 1:
$status = 'INPROGRESS';
$description = 'PHP Censor build running.';
break;
case 2:
$status = 'SUCCESSFUL';
$description = 'PHP Censor build passed.';
break;
case 3:
$status = 'FAILED';
$description = 'PHP Censor build failed.';
break;
default:
$status = 'STOPPED';
$description = 'PHP Censor build failed to complete.';
break;
}
$phpCensorUrl = Config::getInstance()->get('php-censor.url');
$url = sprintf(
'/2.0/repositories/%s/commit/%s/statuses/build',
Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()
? $this->getExtra('remote_reference')
: $project->getReference(),
$this->getCommitId()
);
$client = new Client([
'base_uri' => 'https://api.bitbucket.org',
'http_errors' => false,
]);
$response = $client->post($url, [
'auth' => [$username, $appPassword],
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'state' => $status,
'key' => 'PHP-CENSOR',
'url' => $phpCensorUrl . '/build/view/' . $this->getId(),
'name' => 'PHP Censor Build #' . $this->getId(),
'description' => $description,
],
]);
$status = (integer)$response->getStatusCode();
return ($status >= 200 && $status < 300);
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'git@bitbucket.org:' . $this->getProject()->getReference() . '.git';
} else {
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '.git';
}
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $this->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
/**
* @inheritdoc
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$skipGitFinalization = false;
try {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$helper = new Bitbucket();
$diff = $helper->getPullRequestDiff(
$this->getProject()->getReference(),
$this->getExtra('pull_request_number')
);
$diffFile = $this->writeDiff($builder->buildPath, $diff);
$cmd = 'cd "%s" && git checkout -b php-censor/' . $this->getId() . ' && git apply "%s"';
$success = $builder->executeCommand($cmd, $cloneTo, $diffFile);
unlink($diffFile);
$skipGitFinalization = true;
}
} catch (\Exception $ex) {
$success = false;
}
if ($success && !$skipGitFinalization) {
$success = parent::postCloneSetup($builder, $cloneTo, $extra);
}
return $success;
}
/**
* Create an diff file on disk for this build.
*
* @param string $cloneTo
* @param string $diff
*
* @return string
*/
protected function writeDiff($cloneTo, $diff)
{
$filePath = dirname($cloneTo . '/temp');
$diffFile = $filePath . '.patch';
file_put_contents($diffFile, $diff);
chmod($diffFile, 0600);
return $diffFile;
}
/**
* @inheritDoc
*/
public function reportError(
Builder $builder,
$plugin,
$message,
$severity = BuildError::SEVERITY_NORMAL,
$file = null,
$lineStart = null,
$lineEnd = null
) {
$allowCommentCommit = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.comments.commit',
false
);
$allowCommentPullRequest = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.comments.pull_request',
false
);
if ($allowCommentCommit || $allowCommentPullRequest) {
$diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart);
if (!is_null($diffLineNumber)) {
$helper = new Bitbucket();
$repo = $this->getProject()->getReference();
$prNumber = $this->getExtra('pull_request_number');
$commit = $this->getCommitId();
if (!empty($prNumber)) {
if ($allowCommentPullRequest) {
$helper->createPullRequestComment($repo, $prNumber, $commit, $file, $lineStart, $message);
}
} else {
if ($allowCommentCommit) {
$helper->createCommitComment($repo, $commit, $file, $lineStart, $message);
}
}
}
}
parent::reportError($builder, $plugin, $message, $severity, $file, $lineStart, $lineEnd);
}
/**
* Uses git diff to figure out what the diff line position is, based on the error line number.
*
* @param Builder $builder
* @param string $file
* @param integer $line
*
* @return integer|null
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{
$builder->logExecOutput(false);
$line = (integer)$line;
$prNumber = $this->getExtra('pull_request_number');
$path = $builder->buildPath;
if (!empty($prNumber)) {
$builder->executeCommand('cd %s && git diff origin/%s "%s"', $path, $this->getBranch(), $file);
} else {
$commitId = $this->getCommitId();
$compare = empty($commitId) ? 'HEAD' : $commitId;
$builder->executeCommand('cd %s && git diff %s^^ "%s"', $path, $compare, $file);
}
$builder->logExecOutput(true);
$diff = $builder->getLastOutput();
$helper = new Diff();
$lines = $helper->getLinePositions($diff);
return isset($lines[$line]) ? $lines[$line] : null;
}
}

View file

@ -0,0 +1,91 @@
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
/**
* BitbucketHgBuild Build Model
*
* @author Artem Bochkov <artem.v.bochkov@gmail.com>
*/
class BitbucketHgBuild extends HgBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://bitbucket.org/' . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getTag();
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'ssh://hg@bitbucket.org/' . $this->getProject()->getReference();
} else {
return 'https://bitbucket.org/' . $this->getProject()->getReference();
}
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $this->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
}

View file

@ -0,0 +1,168 @@
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
use PHPCensor\Builder;
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.
*/
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);
}
if ($success) {
$success = $this->mergeBranches($builder, $buildPath);
}
if (!$success) {
$builder->logFailure('Failed to clone remote git repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* @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);
return false;
}
$builder->log('Merged branch origin/'.$branch, LogLevel::INFO);
}
}
return true;
}
/**
* Use an HTTP-based git clone.
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$cmd = 'cd .. && git clone --recursive ';
$depth = $builder->getConfig('clone_depth');
if (!is_null($depth)) {
$cmd .= ' --depth ' . intval($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.
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey($cloneTo);
$gitSshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
// Do the git clone:
$cmd = 'cd .. && git clone --recursive ';
$depth = $builder->getConfig('clone_depth');
if (!is_null($depth)) {
$cmd .= ' --depth ' . intval($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;
}
}

View file

@ -0,0 +1,305 @@
<?php
namespace PHPCensor\Model\Build;
use GuzzleHttp\Client;
use PHPCensor\Builder;
use PHPCensor\Helper\Diff;
use PHPCensor\Helper\Github;
use PHPCensor\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
/**
* Github Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class GithubBuild extends GitBuild
{
/**
* Get link to commit from another source (i.e. Github)
*
* @return string
*/
public function getCommitLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. Github)
*
* @return string
*/
public function getBranchLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. Github)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://github.com/' . $remoteReference . '/tree/' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. Github)
*
* @return string
*/
public function getTagLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getTag();
}
/**
* Send status updates to any relevant third parties (i.e. Github)
*
* @return boolean
*/
public function sendStatusPostback()
{
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
return false;
}
$project = $this->getProject();
if (empty($project)) {
return false;
}
$token = Config::getInstance()->get('php-censor.github.token');
if (empty($token) || empty($this->data['id'])) {
return false;
}
$allowStatusCommit = (boolean)Config::getInstance()->get(
'php-censor.github.status.commit',
false
);
if (!$allowStatusCommit) {
return false;
}
switch ($this->getStatus()) {
case 0:
case 1:
$status = 'pending';
$description = 'PHP Censor build running.';
break;
case 2:
$status = 'success';
$description = 'PHP Censor build passed.';
break;
case 3:
$status = 'failure';
$description = 'PHP Censor build failed.';
break;
default:
$status = 'error';
$description = 'PHP Censor build failed to complete.';
break;
}
$phpCensorUrl = Config::getInstance()->get('php-censor.url');
$url = '/repos/' . $project->getReference() . '/statuses/' . $this->getCommitId();
$client = new Client([
'base_uri' => 'https://api.github.com',
'http_errors' => false,
]);
$response = $client->post($url, [
'headers' => [
'Authorization' => 'token ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded'
],
'json' => [
'state' => $status,
'target_url' => $phpCensorUrl . '/build/view/' . $this->getId(),
'description' => $description,
'context' => 'PHP Censor',
]
]);
$status = (integer)$response->getStatusCode();
return ($status >= 200 && $status < 300);
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'git@github.com:' . $this->getProject()->getReference() . '.git';
} else {
return 'https://github.com/' . $this->getProject()->getReference() . '.git';
}
}
/**
* Get a parsed version of the commit message, with links to issues and commits.
*
* @return string
*/
public function getCommitMessage()
{
$rtn = parent::getCommitMessage();
$project = $this->getProject();
if (!is_null($project)) {
$reference = $project->getReference();
$commitLink = '<a href="https://github.com/' . $reference . '/issues/$1">#$1</a>';
$rtn = preg_replace('/\#([0-9]+)/', $commitLink, $rtn);
$rtn = preg_replace('/\@([a-zA-Z0-9_]+)/', '<a href="https://github.com/$1">@$1</a>', $rtn);
}
return $rtn;
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://github.com/' . $reference . '/';
$link .= 'blob/' . $this->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#L{LINE}-L{LINE_END}';
return $link;
}
/**
* @inheritdoc
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
try {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$pullRequestId = $this->getExtra('pull_request_number');
$cmd = 'cd "%s" && git checkout -b php-censor/' . $this->getId()
. ' %s && git pull -q --no-edit origin pull/%s/head';
if (!empty($extra['git_ssh_wrapper'])) {
$cmd = 'export GIT_SSH="'.$extra['git_ssh_wrapper'].'" && ' . $cmd;
}
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch(), $pullRequestId);
}
} catch (\Exception $ex) {
$success = false;
}
if ($success) {
$success = parent::postCloneSetup($builder, $cloneTo, $extra);
}
return $success;
}
/**
* @inheritdoc
*/
public function reportError(
Builder $builder,
$plugin,
$message,
$severity = BuildError::SEVERITY_NORMAL,
$file = null,
$lineStart = null,
$lineEnd = null
) {
$allowCommentCommit = (boolean)Config::getInstance()->get(
'php-censor.github.comments.commit',
false
);
$allowCommentPullRequest = (boolean)Config::getInstance()->get(
'php-censor.github.comments.pull_request',
false
);
if ($allowCommentCommit || $allowCommentPullRequest) {
$diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart);
if (!is_null($diffLineNumber)) {
$helper = new Github();
$repo = $this->getProject()->getReference();
$prNumber = $this->getExtra('pull_request_number');
$commit = $this->getCommitId();
if (!empty($prNumber)) {
if ($allowCommentPullRequest) {
$helper->createPullRequestComment($repo, $prNumber, $commit, $file, $diffLineNumber, $message);
}
} else {
if ($allowCommentCommit) {
$helper->createCommitComment($repo, $commit, $file, $diffLineNumber, $message);
}
}
}
}
parent::reportError($builder, $plugin, $message, $severity, $file, $lineStart, $lineEnd);
}
/**
* Uses git diff to figure out what the diff line position is, based on the error line number.
*
* @param Builder $builder
* @param string $file
* @param integer $line
*
* @return integer|null
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{
$builder->logExecOutput(false);
$line = (integer)$line;
$prNumber = $this->getExtra('pull_request_number');
$path = $builder->buildPath;
if (!empty($prNumber)) {
$builder->executeCommand('cd %s && git diff origin/%s "%s"', $path, $this->getBranch(), $file);
} else {
$commitId = $this->getCommitId();
$compare = empty($commitId) ? 'HEAD' : $commitId;
$builder->executeCommand('cd %s && git diff %s^^ "%s"', $path, $compare, $file);
}
$builder->logExecOutput(true);
$diff = $builder->getLastOutput();
$helper = new Diff();
$lines = $helper->getLinePositions($diff);
return isset($lines[$line]) ? $lines[$line] : null;
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace PHPCensor\Model\Build;
/**
* Gitlab Build Model
*
* @author André Cianfarani <a.cianfarani@c2is.fr>
*/
class GitlabBuild extends GitBuild
{
/**
* Get link to commit from another source (i.e. Github)
*/
public function getCommitLink()
{
$domain = $this->getProject()->getAccessInformation("domain");
return 'http://' . $domain . '/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. Github)
*/
public function getBranchLink()
{
$domain = $this->getProject()->getAccessInformation("domain");
return 'http://' . $domain . '/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
}
/**
* Get link to specific file (and line) in a the repo's branch
*/
public function getFileLinkTemplate()
{
return sprintf(
'http://%s/%s/blob/%s/{FILE}#L{LINE}',
$this->getProject()->getAccessInformation("domain"),
$this->getProject()->getReference(),
$this->getCommitId()
);
}
/**
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
$user = $this->getProject()->getAccessInformation("user");
$domain = $this->getProject()->getAccessInformation("domain");
$port = $this->getProject()->getAccessInformation('port');
$url = $user . '@' . $domain . ':';
if (!empty($port)) {
$url .= $port . '/';
}
$url .= $this->getProject()->getReference() . '.git';
return $url;
}
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace PHPCensor\Model\Build;
/**
* GogsBuild Build Model
*/
class GogsBuild extends GitBuild
{
/**
* Get link to commit from Gogs repository
*/
public function getCommitLink()
{
return $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
* Get link to branch from Gogs repository
*/
public function getBranchLink()
{
return $this->getProject()->getReference() . '/src/' . $this->getBranch();
}
/**
* Get link to specific file (and line) in a the repo's branch
*/
public function getFileLinkTemplate()
{
return sprintf(
'%s/src/%s/{FILE}#L{LINE}',
$this->getProject()->getReference(),
$this->getCommitId()
);
}
}

101
src/Model/Build/HgBuild.php Normal file
View file

@ -0,0 +1,101 @@
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
use PHPCensor\Builder;
/**
* Mercurial Build Model
*
* @author Pavel Gopanenko <pavelgopanenko@gmail.com>
*/
class HgBuild extends Build
{
/**
* Get the URL to be used to clone this remote repository.
*/
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);
}
if (!$success) {
$builder->logFailure('Failed to clone remote hg repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* Use a HTTP-based hg clone.
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
return $builder->executeCommand('hg clone %s "%s" -r %s', $this->getCloneUrl(), $cloneTo, $this->getBranch());
}
/**
* Use an SSH-based hg clone.
*
* @param Builder $builder
* @param string $cloneTo
*
* @return bool
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey($cloneTo);
// Do the hg clone:
$cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s" -r %s';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo, $this->getBranch());
if ($success) {
$success = $this->postCloneSetup($builder, $cloneTo);
}
// Remove the key file:
unlink($keyFile);
return $success;
}
/**
* Handle post-clone tasks (switching branch, etc.)
*
* @param Builder $builder
* @param string $cloneTo
* @param array $extra
*
* @return bool
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commitId = $this->getCommitId();
// Allow switching to a specific branch:
if (!empty($commitId)) {
$cmd = 'cd "%s" && hg checkout %s';
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch());
}
return $success;
}
}

View file

@ -0,0 +1,91 @@
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
use PHPCensor\Builder;
/**
* Local Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class LocalBuild extends Build
{
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$reference = $this->getProject()->getReference();
$reference = substr($reference, -1) == '/' ? substr($reference, 0, -1) : $reference;
$buildPath = substr($buildPath, 0, -1);
// If there's a /config file in the reference directory, it is probably a bare repository
// which we'll extract into our build path directly.
if (is_file($reference.'/config') && $this->handleBareRepository($builder, $reference, $buildPath) === true) {
return $this->handleConfig($builder, $buildPath) !== false;
}
$configHandled = $this->handleConfig($builder, $reference);
if ($configHandled === false) {
return false;
}
$buildSettings = $builder->getConfig('build_settings');
if (isset($buildSettings['prefer_symlink']) && $buildSettings['prefer_symlink'] === true) {
return $this->handleSymlink($builder, $reference, $buildPath);
} else {
$cmd = 'cp -Rf "%s" "%s/"';
$builder->executeCommand($cmd, $reference, $buildPath);
}
return true;
}
/**
* Check if this is a "bare" git repository, and if so, unarchive it.
* @param Builder $builder
* @param $reference
* @param $buildPath
* @return bool
*/
protected function handleBareRepository(Builder $builder, $reference, $buildPath)
{
$gitConfig = parse_ini_file($reference.'/config', true);
// If it is indeed a bare repository, then extract it into our build path:
if ($gitConfig['core']['bare']) {
$cmd = 'mkdir %2$s; git --git-dir="%1$s" archive %3$s | tar -x -C "%2$s"';
$builder->executeCommand($cmd, $reference, $buildPath, $this->getBranch());
return true;
}
return false;
}
/**
* Create a symlink if required.
* @param Builder $builder
* @param $reference
* @param $buildPath
* @return bool
*/
protected function handleSymlink(Builder $builder, $reference, $buildPath)
{
if (is_link($buildPath) && is_file($buildPath)) {
unlink($buildPath);
}
$builder->log(sprintf('Symlinking: %s to %s', $reference, $buildPath));
if (!symlink($reference, $buildPath)) {
$builder->logFailure('Failed to symlink.');
return false;
}
return true;
}
}

View file

@ -0,0 +1,127 @@
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
use PHPCensor\Builder;
/**
* Remote Subversion Build Model
*
* @author Nadir Dzhilkibaev <imam.sharif@gmail.com>
*/
class SvnBuild extends Build
{
protected $svnCommand = 'svn export -q --non-interactive ';
/**
* Get the URL to be used to clone this remote repository.
*/
protected function getCloneUrl()
{
$url = rtrim($this->getProject()->getReference(), '/') . '/';
$branch = ltrim($this->getBranch(), '/');
// For empty default branch or default branch name like "/trunk" or "trunk" (-> "trunk")
if (empty($branch) || $branch == 'trunk') {
$url .= 'trunk';
// For default branch with standard default branch directory ("branches") like "/branch-1" or "branch-1"
// (-> "branches/branch-1")
} elseif (false === strpos($branch, '/')) {
$url .= 'branches/' . $branch;
// For default branch with non-standard branch directory like "/branch/branch-1" or "branch/branch-1"
// (-> "branch/branch-1")
} else {
$url .= $branch;
}
return $url;
}
/**
* @param Builder $builder
*
* @return void
*/
protected function extendSvnCommandFromConfig(Builder $builder)
{
$cmd = $this->svnCommand;
$svn = $builder->getConfig('svn');
if ($svn) {
foreach ($svn as $key => $value) {
$cmd .= " --$key $value ";
}
}
$depth = $builder->getConfig('clone_depth');
if (!is_null($depth)) {
$cmd .= ' --depth ' . intval($depth) . ' ';
}
$this->svnCommand = $cmd;
}
/**
* Create a working copy by cloning, copying, or similar.
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$this->handleConfig($builder, $buildPath);
$this->extendSvnCommandFromConfig($builder);
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
$success = $this->cloneBySsh($builder, $buildPath);
} else {
$success = $this->cloneByHttp($builder, $buildPath);
}
if (!$success) {
$builder->logFailure('Failed to export remote subversion repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* Use an HTTP-based svn export.
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand;
if (!empty($this->getCommitId())) {
$cmd .= ' -r %s %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCommitId(), $this->getCloneUrl(), $cloneTo);
} else {
$cmd .= ' %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
}
return $success;
}
/**
* Use an SSH-based svn export.
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand . ' %s "%s"';
$keyFile = $this->writeSshKey($cloneTo);
$sshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
$cmd = 'export SVN_SSH="' . $sshWrapper . '" && ' . $cmd;
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
// Remove the key file and svn wrapper:
unlink($keyFile);
unlink($sshWrapper);
return $success;
}
}