Merge branch 'github-enterprise'

This commit is contained in:
Dmitry Khomutov 2018-04-10 20:19:02 +07:00
commit 0a022390ed
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
9 changed files with 90 additions and 159 deletions

View file

@ -482,91 +482,6 @@ var PHPCensorConfirmDialog = Class.extend({
}
});
/**
* Used to initialise the project form:
*/
function setupProjectForm() {
$('.github-container').hide();
$('#element-reference').change(function () {
var el = $(this);
var val = el.val();
var type = $('#element-type').val();
var acceptable = {
'github': {
'ssh': /git\@github\.com\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'git': /git\:\/\/github.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/github\.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
},
'bitbucket': {
'ssh': /git\@bitbucket\.org\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/[a-zA-Z0-9_\-]+\@bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'anon': /https\:\/\/bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
}
};
if (acceptable[type] !== undefined) {
for (var i in acceptable[type]) {
if (val.match(acceptable[type][i])) {
el.val(val.replace(acceptable[type][i], '$1'));
}
}
}
});
$('#element-type').change(function () {
if ($(this).val() == 'github') {
$('#loading').show();
$.ajax({
dataType: "json",
url: window.APP_URL + 'project/ajax-github-repositories',
success: function (data) {
$('#loading').hide();
if (data && data.repos) {
$('#element-github').empty();
for (var i in data.repos) {
var name = data.repos[i];
$('#element-github').append($('<option></option>').text(name).val(name));
}
$('.github-container').slideDown();
}
},
error: handleFailedAjax
});
} else {
$('.github-container').slideUp();
}
$('#element-reference').trigger('change');
});
$('#element-github').change(function () {
var val = $('#element-github').val();
if (val != 'choose') {
$('#element-type').val('github');
$('#element-reference').val(val);
$('label[for=element-reference]').hide();
$('label[for=element-type]').hide();
$('#element-reference').hide();
$('#element-token').val(window.github_token);
$('#element-title').val(val);
} else {
$('label[for=element-reference]').show();
$('label[for=element-type]').show();
$('#element-reference').show();
$('#element-type').show();
$('#element-reference').val('');
$('#element-token').val('');
}
});
}
var Lang = {
get: function () {
var args = Array.prototype.slice.call(arguments);

View file

@ -7,7 +7,6 @@ use PHPCensor\Form;
use JasonGrimes\Paginator;
use PHPCensor;
use PHPCensor\BuildFactory;
use PHPCensor\Helper\Github;
use PHPCensor\Helper\Lang;
use PHPCensor\Helper\SshKey;
use PHPCensor\Service\BuildService;
@ -375,7 +374,7 @@ class ProjectController extends WebController
$values['pubkey'] = $values['ssh_public_key'];
$values['environments'] = $project->getEnvironments();
if ($values['type'] == 'gitlab') {
if (Project::TYPE_GITLAB === $values['type']) {
$accessInfo = $project->getAccessInformation();
$reference = $accessInfo["user"] . '@' . $accessInfo["domain"] . ':' . $accessInfo["port"] . '/' . ltrim($project->getReference(), '/') . ".git";
$values['reference'] = $reference;
@ -453,14 +452,6 @@ class ProjectController extends WebController
$field->setClass('form-control')->setContainerClass('form-group');
$form->addField($field);
$container = new Form\ControlGroup('github-container');
$container->setClass('github-container');
$field = Form\Element\Select::create('github', Lang::get('choose_github'), false);
$field->setClass('form-control')->setContainerClass('form-group');
$container->addField($field);
$form->addField($container);
$field = Form\Element\Text::create('reference', Lang::get('repo_name'), true);
$field->setValidator($this->getReferenceValidator($values));
$field->setClass('form-control')->setContainerClass('form-group');
@ -547,27 +538,27 @@ class ProjectController extends WebController
$type = $values['type'];
$validators = [
'hg' => [
Project::TYPE_HG => [
'regex' => '/^(ssh|https?):\/\//',
'message' => Lang::get('error_hg')
],
'git' => [
Project::TYPE_GIT => [
'regex' => '/^(git|https?):\/\//',
'message' => Lang::get('error_git')
],
'gitlab' => [
'regex' => '`^(.*)@(.*):(.*)/(.*)\.git`',
Project::TYPE_GITLAB => [
'regex' => '/^(git|https?):\/\//',
'message' => Lang::get('error_gitlab')
],
'github' => [
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
Project::TYPE_GITHUB => [
'regex' => '/^(git|https?):\/\//',
'message' => Lang::get('error_github')
],
'bitbucket' => [
Project::TYPE_BITBUCKET => [
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
'message' => Lang::get('error_bitbucket')
],
'bitbucket-hg' => [
Project::TYPE_BITBUCKET_HG => [
'regex' => '/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-\.]+$/',
'message' => Lang::get('error_bitbucket')
],
@ -582,17 +573,4 @@ class ProjectController extends WebController
return true;
};
}
/**
* Get an array of repositories from Github's API.
*/
public function ajaxGithubRepositories()
{
$github = new Github();
$response = new PHPCensor\Http\Response\JsonResponse();
$response->setContent($github->getRepositories());
return $response;
}
}

View file

@ -17,6 +17,19 @@ use PHPCensor\Model\BuildError;
*/
class GithubBuild extends GitBuild
{
/**
* @return string
*/
protected function getDomain()
{
$domain = $this->getProject()->getAccessInformation('domain');
if (!$domain) {
$domain = 'github.com';
}
return $domain;
}
/**
* Get link to commit from another source (i.e. Github)
*
@ -24,7 +37,7 @@ class GithubBuild extends GitBuild
*/
public function getCommitLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
return '//' . $this->getDomain() . '/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
@ -34,7 +47,7 @@ class GithubBuild extends GitBuild
*/
public function getBranchLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
return '//' . $this->getDomain() . '/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
}
/**
@ -45,7 +58,7 @@ class GithubBuild extends GitBuild
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://github.com/' . $remoteReference . '/tree/' . $remoteBranch;
return '//' . $this->getDomain() . '/' . $remoteReference . '/tree/' . $remoteBranch;
}
/**
@ -55,7 +68,7 @@ class GithubBuild extends GitBuild
*/
public function getTagLink()
{
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getTag();
return '//' . $this->getDomain() . '/' . $this->getProject()->getReference() . '/tree/' . $this->getTag();
}
/**
@ -112,7 +125,7 @@ class GithubBuild extends GitBuild
$url = '/repos/' . $project->getReference() . '/statuses/' . $this->getCommitId();
$client = new Client([
'base_uri' => 'https://api.github.com',
'base_uri' => 'https://api.' . $this->getDomain(),
'http_errors' => false,
]);
$response = $client->post($url, [
@ -143,9 +156,15 @@ class GithubBuild extends GitBuild
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'git@github.com:' . $this->getProject()->getReference() . '.git';
$port = $this->getProject()->getAccessInformation('port');
$url = 'git@' . $this->getDomain() . ':';
if (!empty($port)) {
$url .= $port . '/';
}
return $url . $this->getProject()->getReference() . '.git';
} else {
return 'https://github.com/' . $this->getProject()->getReference() . '.git';
return 'https://' . $this->getDomain() . '/' . $this->getProject()->getReference() . '.git';
}
}
@ -156,18 +175,21 @@ class GithubBuild extends GitBuild
*/
public function getCommitMessage()
{
$rtn = parent::getCommitMessage();
$message = 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);
$commitLink = '<a href="//' . $this->getDomain() . '/' . $reference . '/issues/$1">#$1</a>';
$message = preg_replace('/\#([0-9]+)/', $commitLink, $message);
$message = preg_replace(
'/\@([a-zA-Z0-9_]+)/',
'<a href="//' . $this->getDomain() . '/$1">@$1</a>',
$message
);
}
return $rtn;
return $message;
}
/**
@ -178,12 +200,11 @@ class GithubBuild extends GitBuild
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 = '//' . $this->getDomain() . '/' . $reference . '/';
$link .= 'blob/' . $this->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#L{LINE}-L{LINE_END}';

View file

@ -16,7 +16,7 @@ class GitlabBuild extends GitBuild
public function getCommitLink()
{
$domain = $this->getProject()->getAccessInformation("domain");
return 'http://' . $domain . '/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
return '//' . $domain . '/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
}
/**
@ -25,7 +25,7 @@ class GitlabBuild extends GitBuild
public function getBranchLink()
{
$domain = $this->getProject()->getAccessInformation("domain");
return 'http://' . $domain . '/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
return '//' . $domain . '/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
}
/**
@ -34,7 +34,7 @@ class GitlabBuild extends GitBuild
public function getFileLinkTemplate()
{
return sprintf(
'http://%s/%s/blob/%s/{FILE}#L{LINE}',
'//%s/%s/blob/%s/{FILE}#L{LINE}',
$this->getProject()->getAccessInformation("domain"),
$this->getProject()->getReference(),
$this->getCommitId()

View file

@ -102,18 +102,20 @@ class Project extends BaseProject
public function getIcon()
{
switch ($this->getType()) {
case 'github':
case Project::TYPE_GITHUB:
$icon = 'github';
break;
case 'bitbucket':
case 'bitbucket-hg':
case Project::TYPE_BITBUCKET:
case Project::TYPE_BITBUCKET_HG:
$icon = 'bitbucket';
break;
case 'git':
case 'gitlab':
case 'gogs':
case Project::TYPE_GIT:
case Project::TYPE_GITLAB:
case Project::TYPE_GOGS:
case Project::TYPE_HG:
case Project::TYPE_SVN:
default:
$icon = 'code-fork';
break;

View file

@ -99,8 +99,7 @@ class ProjectService
$project->setGroupId($options['group']);
}
// Allow certain project types to set access information:
$this->processAccessInformation($project);
$project = $this->processAccessInformation($project);
// Save and return the project:
/** @var Project $project */
@ -138,27 +137,32 @@ class ProjectService
/**
* In circumstances where it is necessary, populate access information based on other project properties.
*
* @see ProjectService::createProject()
*
* @param Project $project
*
* @return Project
*/
protected function processAccessInformation(Project &$project)
protected function processAccessInformation(Project $project)
{
$matches = [];
$reference = $project->getReference();
if ($project->getType() == 'gitlab') {
if (in_array($project->getType(), [
Project::TYPE_GITHUB,
Project::TYPE_GITLAB
], true)) {
$info = [];
if (preg_match('`^(.+)@(.+):([0-9]*)\/?(.+)\.git`', $reference, $matches)) {
$info['user'] = $matches[1];
$info['user'] = $matches[1];
$info['domain'] = $matches[2];
$info['port'] = $matches[3];
$info['port'] = $matches[3];
$project->setReference($matches[4]);
}
$project->setAccessInformation($info);
}
return $project;
}
}

View file

@ -5,6 +5,7 @@
*/
use PHPCensor\Helper\Lang;
use PHPCensor\Model\Project;
?>
<script>
@ -159,7 +160,12 @@ use PHPCensor\Helper\Lang;
</div>
<?php endif; ?>
<?php if (in_array($project->getType(), ['github', 'gitlab', 'bitbucket'])): ?>
<?php if (in_array($project->getType(), [
Project::TYPE_GITHUB,
Project::TYPE_GITLAB,
Project::TYPE_BITBUCKET,
Project::TYPE_BITBUCKET_HG
])): ?>
<div class="box">
<div class="box-header">
<h4 class="box-title"><?= Lang::get('webhooks'); ?></h4>
@ -172,18 +178,18 @@ use PHPCensor\Helper\Lang;
<div class="box-body">
<?php switch($project->getType()) {
case 'github':
case Project::TYPE_GITHUB:
$url = APP_URL . 'webhook/github/' . $project->getId();
echo Lang::get('webhooks_help_github', $project->getReference());
break;
case 'gitlab':
case Project::TYPE_GITLAB:
$url = APP_URL. 'webhook/gitlab/' . $project->getId();
echo Lang::get('webhooks_help_gitlab');
break;
case 'bitbucket':
case 'bitbucket-hg':
case Project::TYPE_BITBUCKET:
case Project::TYPE_BITBUCKET_HG:
$url = APP_URL . 'webhook/bitbucket/' . $project->getId();
echo Lang::get('webhooks_help_bitbucket', $project->getReference());
break;

View file

@ -39,7 +39,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
public function testExecute_TestGithubDefaultBranch()
{
$project = new Project();
$project->setType('github');
$project->setType(Project::TYPE_GITHUB);
self::assertEquals('master', $project->getBranch());
}
@ -47,7 +47,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
public function testExecute_TestGitlabDefaultBranch()
{
$project = new Project();
$project->setType('gitlab');
$project->setType(Project::TYPE_GITLAB);
self::assertEquals('master', $project->getBranch());
}
@ -55,7 +55,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
public function testExecute_TestBitbucketDefaultBranch()
{
$project = new Project();
$project->setType('bitbucket');
$project->setType(Project::TYPE_BITBUCKET);
self::assertEquals('master', $project->getBranch());
}
@ -63,7 +63,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
public function testExecute_TestMercurialDefaultBranch()
{
$project = new Project();
$project->setType('hg');
$project->setType(Project::TYPE_HG);
self::assertEquals('default', $project->getBranch());
}

View file

@ -67,8 +67,13 @@ class ProjectServiceTest extends \PHPUnit\Framework\TestCase
*/
public function testExecute_CreateGitlabProjectWithoutPort()
{
$reference = 'git@gitlab.block8.net:block8/phpci.git';
$returnValue = $this->testedService->createProject('Gitlab', 'gitlab', $reference, 0);
$reference = 'git@gitlab.block8.net:block8/phpci.git';
$returnValue = $this->testedService->createProject(
'Gitlab',
Project::TYPE_GITLAB,
$reference,
0
);
self::assertEquals('git', $returnValue->getAccessInformation('user'));
self::assertEquals('gitlab.block8.net', $returnValue->getAccessInformation('domain'));