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

View file

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

View file

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

View file

@ -2,10 +2,21 @@
namespace PHPCensor\Model\Base; namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model; use PHPCensor\Model;
class Project extends 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 * @var string
*/ */
@ -32,6 +43,21 @@ class Project extends Model
'user_id' => 0, '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 * @return integer
*/ */
@ -247,12 +273,20 @@ class Project extends Model
* @param string $value * @param string $value
* *
* @return boolean * @return boolean
*
* @throws ValidationException
*/ */
public function setType($value) public function setType($value)
{ {
$this->validateNotNull('type', $value); $this->validateNotNull('type', $value);
$this->validateString('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) { if ($this->data['type'] === $value) {
return false; return false;
} }