From eca63d81e2415afa2fcb149368f54205c8100998 Mon Sep 17 00:00:00 2001 From: Stepan Strelets Date: Wed, 8 Nov 2017 11:50:32 +0300 Subject: [PATCH] Allow specify project title in webhook request --- docs/en/sources/git.md | 7 +++++++ src/PHPCensor/Controller/WebhookController.php | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/en/sources/git.md b/docs/en/sources/git.md index 25be4287..604ae497 100644 --- a/docs/en/sources/git.md +++ b/docs/en/sources/git.md @@ -17,6 +17,13 @@ Installation #!/bin/sh PROJECT_ID=1 + +# You can use the project title instead of the id: +# PROJECT_ID=my_project_title + +# If the names of the repository and the project match: +# PROJECT_ID=`basename \`pwd\` | sed 's#\.git$##'` + APP_URL="http://my.server.com/php-censor/" trigger_hook() { diff --git a/src/PHPCensor/Controller/WebhookController.php b/src/PHPCensor/Controller/WebhookController.php index afa43337..542241fc 100644 --- a/src/PHPCensor/Controller/WebhookController.php +++ b/src/PHPCensor/Controller/WebhookController.php @@ -722,7 +722,7 @@ class WebhookController extends Controller /** * Fetch a project and check its type. * - * @param int $projectId + * @param int|string $projectId id or title * @param array|string $expectedType * * @return Project @@ -731,12 +731,23 @@ class WebhookController extends Controller */ protected function fetchProject($projectId, $expectedType) { - $project = $this->projectStore->getById($projectId); - if (empty($projectId)) { throw new Exception('Project does not exist: ' . $projectId); } + if (is_numeric($projectId)) { + $project = $this->projectStore->getById($projectId); + } else { + $projects = $this->projectStore->getByTitle($projectId, 2); + if ($projects['count'] < 1) { + throw new Exception('Project does not found: ' . $projectId); + } + if ($projects['count'] > 1) { + throw new Exception('Project id is ambiguous: ' . $projectId); + } + $project = reset($projects['items']); + } + if (is_array($expectedType) ? !in_array($project->getType(), $expectedType) : $project->getType() !== $expectedType