diff --git a/README.md b/README.md index 2dbc17b..8b23fe9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A bitbucket cli tool. ## Installation ```bash -$ sudo curl -#fLo /usr/local/bin/bitbucket https://github.com/NINEJKH/bitbucket-cli/releases/download/0.0.3/bitbucket.phar && sudo chmod +x /usr/local/bin/bitbucket +$ curl -#fL "$(curl -s https://api.github.com/repos/NINEJKH/bitbucket-cli/releases/latest | grep 'browser_download_url' | sed -n 's/.*"\(http.*\)".*/\1/p')" | sudo tee /usr/local/bin/bitbucket > /dev/null && sudo chmod +x /usr/local/bin/bitbucket ``` ## Auth diff --git a/app/Commands/BitbucketCommand.php b/app/Commands/BitbucketCommand.php index db88fd0..4e9efd0 100644 --- a/app/Commands/BitbucketCommand.php +++ b/app/Commands/BitbucketCommand.php @@ -11,10 +11,13 @@ abstract class BitbucketCommand extends Command protected function throwApiResponseError(MessageInterface $message) { $error_message = $message->getContent(); + if (preg_match('~^{.*}$~', $error_message)) { $error_message = json_decode($error_message, true); - if (!empty($error_message['error'])) { + if (!empty($error_message['error']) && !empty($error_message['error']['detail'])) { $error_message = sprintf('[%s]%s%s', $error_message['error']['message'], PHP_EOL, $error_message['error']['detail']); + } elseif (!empty($error_message['error'])) { + $error_message = sprintf('%s', $error_message['error']['message']); } } diff --git a/app/Commands/Repositories/PullRequests/UpdateDescriptionCommand.php b/app/Commands/Repositories/PullRequests/UpdateDescriptionCommand.php index 814d6cd..3f07c46 100644 --- a/app/Commands/Repositories/PullRequests/UpdateDescriptionCommand.php +++ b/app/Commands/Repositories/PullRequests/UpdateDescriptionCommand.php @@ -55,6 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output) ] ]; + if (!empty($current_pr['reviewers'])) { + $payload['reviewers'] = $current_pr['reviewers']; + } + + $pull = $this->create(); $pr = $pull->update($username, $repo_slug, $input->getArgument('pull_request_id'), $payload); if (!$pr->isOk()) { diff --git a/app/Commands/Repositories/PullRequestsCommand.php b/app/Commands/Repositories/PullRequestsCommand.php index 8384566..94205ee 100644 --- a/app/Commands/Repositories/PullRequestsCommand.php +++ b/app/Commands/Repositories/PullRequestsCommand.php @@ -4,20 +4,29 @@ use App\Commands\BitbucketCommand; use App\Providers\BitbucketConfigProvider; -use Bitbucket\API\Http\Listener\BasicAuthListener; use Bitbucket\API\Repositories\PullRequests; abstract class PullRequestsCommand extends BitbucketCommand { protected function create() { - $bitbucket_config = new BitbucketConfigProvider; - $pull = new PullRequests; - $pull->getClient()->addListener( - new BasicAuthListener($bitbucket_config->getBasicAuthUsername(), $bitbucket_config->getBasicAuthPassword()) - ); + $pull->getClient()->addListener($this->createAuthListener()); return $pull; } + + protected function createAuthListener() + { + $bitbucket_config = new BitbucketConfigProvider; + + if ($bitbucket_config->hasOAuth2()) { + return new \Bitbucket\API\Http\Listener\OAuth2Listener([ + 'client_id' => $bitbucket_config->getOAuth2Id(), + 'client_secret' => $bitbucket_config->getOAuth2Secret(), + ]); + } elseif ($bitbucket_config->hasBasicAuth()) { + return new \Bitbucket\API\Http\Listener\BasicAuthListener($bitbucket_config->getBasicAuthUsername(), $bitbucket_config->getBasicAuthPassword()); + } + } } diff --git a/app/Providers/BitbucketConfigProvider.php b/app/Providers/BitbucketConfigProvider.php index 2d4fd3e..dbe3248 100644 --- a/app/Providers/BitbucketConfigProvider.php +++ b/app/Providers/BitbucketConfigProvider.php @@ -13,6 +13,30 @@ public function __construct() } } + public function hasOAuth2() + { + return (bool) isset(static::$config['auth']['client_id'], static::$config['auth']['client_secret']); + } + + public function hasBasicAuth() + { + return (bool) isset(static::$config['auth']['username'], static::$config['auth']['password']); + } + + public function getOAuth2Id() + { + if (isset(static::$config['auth']['client_id'])) { + return static::$config['auth']['client_id']; + } + } + + public function getOAuth2Secret() + { + if (isset(static::$config['auth']['client_secret'])) { + return static::$config['auth']['client_secret']; + } + } + public function getBasicAuthUsername() { if (isset(static::$config['auth']['username'])) {