Skip to content
Permalink
Browse files

Merge branch 'BitBucketServer'

  • Loading branch information...
corpsee committed Mar 12, 2019
2 parents 71aedd2 + 074d609 commit f0ec3417d2d5272129cfc9fce4ad3a4d118390a8
@@ -66,6 +66,9 @@ public static function getBuild(Build $build)
case Project::TYPE_BITBUCKET_HG:
$type = 'BitbucketHgBuild';
break;
case Project::TYPE_BITBUCKET_SERVER:
$type = 'BitbucketServerBuild';
break;
case Project::TYPE_SVN:
$type = 'SvnBuild';
break;
@@ -103,10 +103,10 @@ public function view($projectId)
}
/** @var PHPCensor\Model\User $user */
$user = $this->getUser();
$perPage = $user->getFinalPerPage();
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
$pages = ($builds[1] === 0)
$user = $this->getUser();
$perPage = $user->getFinalPerPage();
$builds = $this->getLatestBuildsHtml($projectId, $branch, $environment, (($page - 1) * $perPage), $perPage);
$pages = ($builds[1] === 0)
? 1
: (integer)ceil($builds[1] / $perPage);
@@ -194,7 +194,7 @@ public function build($projectId)
$environment = null;
$branch = null;
switch($type) {
switch ($type) {
case 'environment':
$environment = $id;
break;
@@ -486,16 +486,17 @@ protected function projectForm($values, $type = 'add')
$form->addField(new Form\Element\Hidden('ssh_public_key'));
$options = [
'choose' => Lang::get('select_repository_type'),
Project::TYPE_GITHUB => 'GitHub',
Project::TYPE_BITBUCKET => 'Bitbucket (Git)',
Project::TYPE_BITBUCKET_HG => 'Bitbucket (Hg)',
Project::TYPE_GITLAB => 'GitLab',
Project::TYPE_GOGS => 'Gogs',
Project::TYPE_GIT => 'Git',
Project::TYPE_LOCAL => Lang::get('local'),
Project::TYPE_HG => 'Hg (Mercurial)',
Project::TYPE_SVN => 'Svn (Subversion)',
'choose' => Lang::get('select_repository_type'),
Project::TYPE_GITHUB => 'GitHub',
Project::TYPE_BITBUCKET => 'Bitbucket (Git)',
Project::TYPE_BITBUCKET_SERVER => 'Bitbucket (Server)',
Project::TYPE_BITBUCKET_HG => 'Bitbucket (Hg)',
Project::TYPE_GITLAB => 'GitLab',
Project::TYPE_GOGS => 'Gogs',
Project::TYPE_GIT => 'Git',
Project::TYPE_LOCAL => Lang::get('local'),
Project::TYPE_HG => 'Hg (Mercurial)',
Project::TYPE_SVN => 'Svn (Subversion)',
];
$sourcesPattern = sprintf('^(%s)', implode('|', Project::$allowedTypes));
@@ -627,6 +628,10 @@ protected function getReferenceValidator($values)
'regex' => $gitRegex,
'message' => Lang::get('error_bitbucket')
],
Project::TYPE_BITBUCKET_SERVER => [
'regex' => $gitRegex,
'message' => Lang::get('error_bitbucket')
],
Project::TYPE_BITBUCKET_HG => [
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
'message' => Lang::get('error_bitbucket')
@@ -16,6 +16,7 @@
use PHPCensor\Store\Factory;
use PHPCensor\Http\Response;
use PHPCensor\Model\Build\BitbucketBuild;
use PHPCensor\Model\Build\BitbucketServerBuild;
use PHPCensor\Model\Build\GithubBuild;
/**
@@ -116,7 +117,7 @@ protected function createBuild(
$ignoreEnvironments = [];
$ignoreTags = [];
if ($builds['count']) {
foreach($builds['items'] as $build) {
foreach ($builds['items'] as $build) {
/** @var Build $build */
$ignoreEnvironments[$build->getId()] = $build->getEnvironment();
$ignoreTags[$build->getId()] = $build->getTag();
@@ -139,8 +140,7 @@ protected function createBuild(
if (!empty($environmentNames)) {
$duplicates = [];
foreach ($environmentNames as $environmentName) {
if (
!in_array($environmentName, $ignoreEnvironments) ||
if (!in_array($environmentName, $ignoreEnvironments) ||
($tag && !in_array($tag, $ignoreTags, true))
) {
// If not, create a new build job for it:
@@ -179,8 +179,7 @@ protected function createBuild(
}
} else {
$environmentName = null;
if (
!in_array($environmentName, $ignoreEnvironments, true) ||
if (!in_array($environmentName, $ignoreEnvironments, true) ||
($tag && !in_array($tag, $ignoreTags, true))
) {
$build = $this->buildService->createBuild(
@@ -350,6 +349,7 @@ public function bitbucket($projectId)
$project = $this->fetchProject($projectId, [
Project::TYPE_BITBUCKET,
Project::TYPE_BITBUCKET_HG,
Project::TYPE_BITBUCKET_SERVER,
Project::TYPE_GIT,
Project::TYPE_HG,
]);
@@ -366,6 +366,11 @@ public function bitbucket($projectId)
return $this->bitbucketPullRequest($project, $payload);
}
// Handle Pull Request webhook for BB server:
if (!empty($payload['pullRequest'])) {
return $this->bitbucketSvrPullRequest($project, $payload);
}
// Handle Push (and Tag) webhooks:
if (!empty($payload['push']['changes'])) {
return $this->bitbucketCommitRequest($project, $payload);
@@ -508,6 +513,62 @@ protected function bitbucketPullRequest(Project $project, array $payload)
return ['status' => $status, 'commits' => $results];
}
/**
* Handle the payload when Bitbucket Server sends a Pull Request webhook.
*
* @param Project $project
* @param array $payload
*
* @return array
*
* @throws Exception
*/
protected function bitbucketSvrPullRequest(Project $project, array $payload)
{
$triggerType = trim($_SERVER['HTTP_X_EVENT_KEY']);
if (!array_key_exists(
$triggerType,
BitbucketServerBuild::$pullrequestTriggersToSources
)) {
return [
'status' => 'ignored',
'message' => 'Trigger type "' . $triggerType . '" is not supported.'
];
}
try {
$branch = $payload['pullRequest']['toRef']['displayId'];
$committer = $payload['pullRequest']['author']['user']['emailAddress'];
$message = $payload['pullRequest']['description'];
$id = $payload['pullRequest']['fromRef']['latestCommit'];
$extra = [
'pull_request_number' => $payload['pullRequest']['id'],
'remote_branch' => $payload['pullrequest']['fromRef']['displayId'],
'remote_reference' => $payload['pullrequest']['fromRef']['repository']['project']['name'],
];
$results = [];
$results[$id] = $this->createBuild(
BitbucketServerBuild::$pullrequestTriggersToSources[$triggerType],
$project,
$id,
$branch,
null,
$committer,
$message,
$extra
);
$status = 'ok';
} catch (Exception $ex) {
$results[$id] = ['status' => 'failed', 'error' => $ex->getMessage()];
}
return ['status' => $status, 'commits' => $results];
}
/**
* Bitbucket POST service.
*
@@ -597,8 +658,7 @@ public function github($projectId)
protected function githubCommitRequest(Project $project, array $payload)
{
// Github sends a payload when you close a pull request with a non-existent commit. We don't want this.
if (
array_key_exists('after', $payload) &&
if (array_key_exists('after', $payload) &&
$payload['after'] === '0000000000000000000000000000000000000000'
) {
return ['status' => 'ignored'];
@@ -443,5 +443,4 @@ private function getDefaultEnv()
return $env;
}
}
@@ -7,15 +7,16 @@
class Project extends Model
{
const TYPE_LOCAL = 'local';
const TYPE_GIT = 'git';
const TYPE_GITHUB = 'github';
const TYPE_BITBUCKET = 'bitbucket';
const TYPE_GITLAB = 'gitlab';
const TYPE_GOGS = 'gogs';
const TYPE_HG = 'hg';
const TYPE_BITBUCKET_HG = 'bitbucket-hg';
const TYPE_SVN = 'svn';
const TYPE_LOCAL = 'local';
const TYPE_GIT = 'git';
const TYPE_GITHUB = 'github';
const TYPE_BITBUCKET = 'bitbucket';
const TYPE_GITLAB = 'gitlab';
const TYPE_GOGS = 'gogs';
const TYPE_HG = 'hg';
const TYPE_BITBUCKET_HG = 'bitbucket-hg';
const TYPE_BITBUCKET_SERVER = 'bitbucket-server';
const TYPE_SVN = 'svn';
/**
* @var array
@@ -52,6 +53,7 @@ class Project extends Model
self::TYPE_HG,
self::TYPE_BITBUCKET_HG,
self::TYPE_SVN,
self::TYPE_BITBUCKET_SERVER
];
/**
@@ -8,6 +8,7 @@
use PHPCensor\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
use PHPCensor\Helper\Diff;
/**
* BitBucket Build Model
@@ -294,7 +295,7 @@ public function reportError(
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.
*
Oops, something went wrong.

0 comments on commit f0ec341

Please sign in to comment.
You can’t perform that action at this time.