Added constants for project types (git, hg, github...).

This commit is contained in:
Dmitry Khomutov 2018-03-12 00:34:48 +07:00
parent 2ed6377971
commit 10d41dba81
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
4 changed files with 85 additions and 32 deletions

View file

@ -2,6 +2,7 @@
namespace PHPCensor;
use PHPCensor\Model\Project;
use PHPCensor\Store\Factory;
use PHPCensor\Model\Build;
@ -41,31 +42,31 @@ class BuildFactory
if (!empty($project)) {
switch ($project->getType()) {
case 'local':
case Project::TYPE_LOCAL:
$type = 'LocalBuild';
break;
case 'git':
case Project::TYPE_GIT:
$type = 'GitBuild';
break;
case 'github':
case Project::TYPE_GITHUB:
$type = 'GithubBuild';
break;
case 'bitbucket':
case Project::TYPE_BITBUCKET:
$type = 'BitbucketBuild';
break;
case 'gitlab':
case Project::TYPE_GITLAB:
$type = 'GitlabBuild';
break;
case 'gogs':
case Project::TYPE_GOGS:
$type = 'GogsBuild';
break;
case 'hg':
case Project::TYPE_HG:
$type = 'HgBuild';
break;
case 'bitbucket-hg':
case Project::TYPE_BITBUCKET_HG:
$type = 'BitbucketHgBuild';
break;
case 'svn':
case Project::TYPE_SVN:
$type = 'SvnBuild';
break;
default:

View file

@ -16,6 +16,7 @@ use PHPCensor\Model\Build;
use PHPCensor\Http\Response\RedirectResponse;
use PHPCensor\View;
use PHPCensor\Store\Factory;
use PHPCensor\Model\Project;
/**
* Project Controller - Allows users to create, edit and view projects.
@ -426,16 +427,16 @@ class ProjectController extends PHPCensor\Controller
$form->addField(new Form\Element\Hidden('pubkey'));
$options = [
'choose' => Lang::get('select_repository_type'),
'github' => 'GitHub',
'bitbucket' => 'Bitbucket (Git)',
'bitbucket-hg' => 'Bitbucket (Hg)',
'gitlab' => 'GitLab',
'gogs' => 'Gogs',
'git' => 'Git',
'local' => Lang::get('local'),
'hg' => 'Hg (Mercurial)',
'svn' => 'Svn (Subversion)',
'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)',
];
$field = Form\Element\Select::create('type', Lang::get('where_hosted'), true);
@ -566,7 +567,7 @@ class ProjectController extends PHPCensor\Controller
if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) {
throw new \Exception($validators[$type]['message']);
} elseif ($type == 'local' && !is_dir($val)) {
} elseif (Project::TYPE_LOCAL === $type && !is_dir($val)) {
throw new \Exception(Lang::get('error_path'));
}

View file

@ -99,7 +99,12 @@ class WebhookController extends Controller
*/
public function bitbucket($projectId)
{
$project = $this->fetchProject($projectId, ['bitbucket', 'bitbucket-hg', 'hg', 'git']);
$project = $this->fetchProject($projectId, [
Project::TYPE_BITBUCKET,
Project::TYPE_BITBUCKET_HG,
Project::TYPE_HG,
Project::TYPE_GIT,
]);
// Support both old services and new webhooks
if ($payload = $this->getParam('payload')) {
@ -292,7 +297,10 @@ class WebhookController extends Controller
*/
public function git($projectId)
{
$project = $this->fetchProject($projectId, ['local', 'git']);
$project = $this->fetchProject($projectId, [
Project::TYPE_LOCAL,
Project::TYPE_GIT,
]);
$branch = $this->getParam('branch', $project->getBranch());
$commit = $this->getParam('commit');
$commitMessage = $this->getParam('message');
@ -318,7 +326,10 @@ class WebhookController extends Controller
*/
public function github($projectId)
{
$project = $this->fetchProject($projectId, ['github', 'git']);
$project = $this->fetchProject($projectId, [
Project::TYPE_GITHUB,
Project::TYPE_GIT,
]);
switch ($_SERVER['CONTENT_TYPE']) {
case 'application/json':
@ -503,7 +514,10 @@ class WebhookController extends Controller
*/
public function gitlab($projectId)
{
$project = $this->fetchProject($projectId, ['gitlab', 'git']);
$project = $this->fetchProject($projectId, [
Project::TYPE_GITLAB,
Project::TYPE_GIT,
]);
$payloadString = file_get_contents("php://input");
$payload = json_decode($payloadString, true);
@ -570,7 +584,9 @@ class WebhookController extends Controller
*/
public function svn($projectId)
{
$project = $this->fetchProject($projectId, 'svn');
$project = $this->fetchProject($projectId, [
Project::TYPE_SVN
]);
$branch = $this->getParam('branch', $project->getBranch());
$commit = $this->getParam('commit');
$commitMessage = $this->getParam('message');
@ -596,7 +612,11 @@ class WebhookController extends Controller
*/
public function gogs($projectId)
{
$project = $this->fetchProject($projectId, ['gogs', 'git']);
$project = $this->fetchProject($projectId, [
Project::TYPE_GOGS,
Project::TYPE_GIT,
]);
switch ($_SERVER['CONTENT_TYPE']) {
case 'application/json':
$payload = json_decode(file_get_contents('php://input'), true);
@ -883,13 +903,13 @@ class WebhookController extends Controller
* Fetch a project and check its type.
*
* @param integer $projectId id or title of project
* @param string $expectedType
* @param array $expectedType
*
* @return Project
*
* @throws Exception If the project does not exist or is not of the expected type.
*/
protected function fetchProject($projectId, $expectedType)
protected function fetchProject($projectId, array $expectedType)
{
if (empty($projectId)) {
throw new Exception('Project does not exist: ' . $projectId);
@ -908,10 +928,7 @@ class WebhookController extends Controller
$project = reset($projects['items']);
}
if (is_array($expectedType)
? !in_array($project->getType(), $expectedType)
: $project->getType() !== $expectedType
) {
if (!in_array($project->getType(), $expectedType, true)) {
throw new Exception('Wrong project type: ' . $project->getType());
}

View file

@ -2,10 +2,21 @@
namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model;
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';
/**
* @var string
*/
@ -32,6 +43,21 @@ class Project extends Model
'user_id' => 0,
];
/**
* @var array
*/
protected $allowedTypes = [
self::TYPE_LOCAL,
self::TYPE_GIT,
self::TYPE_GITHUB,
self::TYPE_BITBUCKET,
self::TYPE_GITLAB,
self::TYPE_GOGS,
self::TYPE_HG,
self::TYPE_BITBUCKET_HG,
self::TYPE_SVN,
];
/**
* @return integer
*/
@ -247,12 +273,20 @@ class Project extends Model
* @param string $value
*
* @return boolean
*
* @throws ValidationException
*/
public function setType($value)
{
$this->validateNotNull('type', $value);
$this->validateString('type', $value);
if (!in_array($value, $this->allowedTypes, true)) {
throw new ValidationException(
'Column "type" must be one of: ' . join(', ', $this->allowedTypes) . '.'
);
}
if ($this->data['type'] === $value) {
return false;
}