Merge branch 'feature-commit-status'

This commit is contained in:
Dmitry Khomutov 2017-12-28 22:38:16 +07:00
commit a9d247ecb5
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
5 changed files with 71 additions and 26 deletions

View file

@ -45,11 +45,15 @@ php-censor:
comments: comments:
commit: false # This option allow/deny to post comments to Bitbucket commit commit: false # This option allow/deny to post comments to Bitbucket commit
pull_request: false # This option allow/deny to post comments to Bitbucket Pull Request pull_request: false # This option allow/deny to post comments to Bitbucket Pull Request
status:
commit: false # This option allow/deny to post status to Bitbucket commit
github: github:
token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
comments: comments:
commit: false # This option allow/deny to post comments to Github commit commit: false # This option allow/deny to post comments to Github commit
pull_request: false # This option allow/deny to post comments to Github Pull Request pull_request: false # This option allow/deny to post comments to Github Pull Request
status:
commit: false # This option allow/deny to post status to Github commit
build: build:
remove_builds: true # This option allow/deny build cleaning remove_builds: true # This option allow/deny build cleaning
writer_buffer_size: 500 # BuildErrorWriter buffer size (count of inserts in one SQL query) writer_buffer_size: 500 # BuildErrorWriter buffer size (count of inserts in one SQL query)

View file

@ -22,7 +22,7 @@ use Symfony\Component\Yaml\Dumper;
/** /**
* Install console command - Installs PHP Censor * Install console command - Installs PHP Censor
* *
* @author Dan Cryer <dan@block8.co.uk> * @author Dan Cryer <dan@block8.co.uk>
*/ */
class InstallCommand extends Command class InstallCommand extends Command
@ -61,7 +61,7 @@ class InstallCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$configFromFile = (boolean)$input->getOption('config-from-file'); $configFromFile = (boolean)$input->getOption('config-from-file');
if (!$configFromFile && !$this->verifyNotInstalled($output)) { if (!$configFromFile && !$this->verifyNotInstalled($output)) {
return; return;
} }
@ -272,6 +272,9 @@ class InstallCommand extends Command
'commit' => false, 'commit' => false,
'pull_request' => false, 'pull_request' => false,
], ],
'status' => [
'commit' => false,
],
], ],
'github' => [ 'github' => [
'token' => null, 'token' => null,
@ -279,6 +282,9 @@ class InstallCommand extends Command
'commit' => false, 'commit' => false,
'pull_request' => false, 'pull_request' => false,
], ],
'status' => [
'commit' => false,
],
], ],
'build' => [ 'build' => [
'remove_builds' => true, 'remove_builds' => true,
@ -320,7 +326,7 @@ class InstallCommand extends Command
'name' => null, 'name' => null,
'lifetime' => 600, 'lifetime' => 600,
]; ];
if (!$input->getOption('queue-use')) { if (!$input->getOption('queue-use')) {
return $skipQueueConfig; return $skipQueueConfig;
} }
@ -348,7 +354,7 @@ class InstallCommand extends Command
$questionQueue = new Question('Enter your beanstalkd hostname [localhost]: ', 'localhost'); $questionQueue = new Question('Enter your beanstalkd hostname [localhost]: ', 'localhost');
$queueConfig['host'] = $helper->ask($input, $output, $questionQueue); $queueConfig['host'] = $helper->ask($input, $output, $questionQueue);
$questionName = new Question('Enter the queue (tube) name to use [php-censor-queue]: ', 'php-censor-queue'); $questionName = new Question('Enter the queue (tube) name to use [php-censor-queue]: ', 'php-censor-queue');
$queueConfig['name'] = $helper->ask($input, $output, $questionName); $queueConfig['name'] = $helper->ask($input, $output, $questionName);
} }

View file

@ -44,7 +44,7 @@ class BitbucketBuild extends RemoteGitBuild
/** /**
* Send status updates to any relevant third parties (i.e. Bitbucket) * Send status updates to any relevant third parties (i.e. Bitbucket)
* *
* @return bool * @return boolean
*/ */
public function sendStatusPostback() public function sendStatusPostback()
{ {
@ -64,6 +64,15 @@ class BitbucketBuild extends RemoteGitBuild
return false; return false;
} }
$allowStatusCommit = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.status.commit',
false
);
if (!$allowStatusCommit) {
return false;
}
switch ($this->getStatus()) { switch ($this->getStatus()) {
case 0: case 0:
case 1: case 1:
@ -86,7 +95,7 @@ class BitbucketBuild extends RemoteGitBuild
$phpCensorUrl = Config::getInstance()->get('php-censor.url'); $phpCensorUrl = Config::getInstance()->get('php-censor.url');
$url = sprintf( $url = sprintf(
'/2.0/repositories/%s/commit/%s/statuses/build', '/2.0/repositories/%s/commit/%s/statuses/build',
$this->getExtra('build_type') == 'pull_request' $this->getExtra('build_type') == 'pull_request'
? $this->getExtra('remote_reference') ? $this->getExtra('remote_reference')
@ -95,15 +104,15 @@ class BitbucketBuild extends RemoteGitBuild
); );
$client = new Client([ $client = new Client([
'base_uri' => 'https://api.bitbucket.org', 'base_uri' => 'https://api.bitbucket.org',
'http_errors' => false, 'http_errors' => false,
]); ]);
$response = $client->post($url, [ $response = $client->post($url, [
'auth' => [$username, $appPassword], 'auth' => [$username, $appPassword],
'headers' => [ 'headers' => [
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
], ],
'json' => [ 'json' => [
'state' => $status, 'state' => $status,
'key' => 'PHP-CENSOR', 'key' => 'PHP-CENSOR',
'url' => $phpCensorUrl . '/build/view/' . $this->getId(), 'url' => $phpCensorUrl . '/build/view/' . $this->getId(),
@ -224,8 +233,16 @@ class BitbucketBuild extends RemoteGitBuild
$lineStart = null, $lineStart = null,
$lineEnd = null $lineEnd = null
) { ) {
$allowCommentCommit = (boolean)Config::getInstance()->get('php-censor.bitbucket.comments.commit', false); $allowCommentCommit = (boolean)Config::getInstance()->get(
$allowCommentPullRequest = (boolean)Config::getInstance()->get('php-censor.bitbucket.comments.pull_request', false); 'php-censor.bitbucket.comments.commit',
false
);
$allowCommentPullRequest = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.comments.pull_request',
false
);
//$file = $builder->buildPath.'test.php'; //$file = $builder->buildPath.'test.php';
if ($allowCommentCommit || $allowCommentPullRequest) { if ($allowCommentCommit || $allowCommentPullRequest) {
$diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart); $diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart);

View file

@ -42,8 +42,10 @@ class GithubBuild extends RemoteGitBuild
} }
/** /**
* Send status updates to any relevant third parties (i.e. Github) * Send status updates to any relevant third parties (i.e. Github)
*/ *
* @return boolean
*/
public function sendStatusPostback() public function sendStatusPostback()
{ {
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) { if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
@ -56,11 +58,19 @@ class GithubBuild extends RemoteGitBuild
} }
$token = Config::getInstance()->get('php-censor.github.token'); $token = Config::getInstance()->get('php-censor.github.token');
if (empty($token) || empty($this->data['id'])) { if (empty($token) || empty($this->data['id'])) {
return false; return false;
} }
$allowStatusCommit = (boolean)Config::getInstance()->get(
'php-censor.github.status.commit',
false
);
if (!$allowStatusCommit) {
return false;
}
switch ($this->getStatus()) { switch ($this->getStatus()) {
case 0: case 0:
case 1: case 1:
@ -83,9 +93,12 @@ class GithubBuild extends RemoteGitBuild
$phpCensorUrl = Config::getInstance()->get('php-censor.url'); $phpCensorUrl = Config::getInstance()->get('php-censor.url');
$url = 'https://api.github.com/repos/' . $project->getReference() . '/statuses/' . $this->getCommitId(); $url = '/repos/' . $project->getReference() . '/statuses/' . $this->getCommitId();
$client = new Client(); $client = new Client([
$client->post($url, [ 'base_uri' => 'https://api.github.com',
'http_errors' => false,
]);
$response = $client->post($url, [
'headers' => [ 'headers' => [
'Authorization' => 'token ' . $token, 'Authorization' => 'token ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded' 'Content-Type' => 'application/x-www-form-urlencoded'
@ -98,7 +111,9 @@ class GithubBuild extends RemoteGitBuild
] ]
]); ]);
return true; $status = (integer)$response->getStatusCode();
return ($status >= 200 && $status < 300);
} }
/** /**
@ -207,8 +222,15 @@ class GithubBuild extends RemoteGitBuild
$lineStart = null, $lineStart = null,
$lineEnd = null $lineEnd = null
) { ) {
$allowCommentCommit = (boolean)Config::getInstance()->get('php-censor.github.comments.commit', false); $allowCommentCommit = (boolean)Config::getInstance()->get(
$allowCommentPullRequest = (boolean)Config::getInstance()->get('php-censor.github.comments.pull_request', false); 'php-censor.github.comments.commit',
false
);
$allowCommentPullRequest = (boolean)Config::getInstance()->get(
'php-censor.github.comments.pull_request',
false
);
if ($allowCommentCommit || $allowCommentPullRequest) { if ($allowCommentCommit || $allowCommentPullRequest) {
$diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart); $diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart);
@ -220,9 +242,6 @@ class GithubBuild extends RemoteGitBuild
$prNumber = $this->getExtra('pull_request_number'); $prNumber = $this->getExtra('pull_request_number');
$commit = $this->getCommitId(); $commit = $this->getCommitId();
$allowCommentCommit = (boolean)Config::getInstance()->get('php-censor.github.comments.commit', false);
$allowCommentPullRequest = (boolean)Config::getInstance()->get('php-censor.github.comments.pull_request', false);
if (!empty($prNumber)) { if (!empty($prNumber)) {
if ($allowCommentPullRequest) { if ($allowCommentPullRequest) {
$helper->createPullRequestComment($repo, $prNumber, $commit, $file, $diffLineNumber, $message); $helper->createPullRequestComment($repo, $prNumber, $commit, $file, $diffLineNumber, $message);

View file

@ -129,8 +129,7 @@ class BuildService
$build->setStatus(Build::STATUS_PENDING); $build->setStatus(Build::STATUS_PENDING);
/** @var Build $build */ /** @var Build $build */
$build = $this->buildStore->save($build); $build = $this->buildStore->save($build);
$buildId = $build->getId(); $buildId = $build->getId();
if (!empty($buildId)) { if (!empty($buildId)) {