diff --git a/src/Actions/Pr.php b/src/Actions/Pr.php index 511ebb3..4757034 100644 --- a/src/Actions/Pr.php +++ b/src/Actions/Pr.php @@ -37,7 +37,7 @@ class Pr extends Base /** * List pull request for repository. * - * @param string $destination + * @param string $destination * @return void */ public function list($destination = '') @@ -86,7 +86,7 @@ public function list($destination = '') /** * Get pull request diff. * - * @param int $prNumber + * @param int $prNumber * @return void */ public function diff($prNumber) @@ -97,8 +97,9 @@ public function diff($prNumber) /** * Diff stats file. * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function files($prNumber) @@ -113,8 +114,8 @@ public function files($prNumber) /** * Get pull request commits. * - * @param $prNumber * @return void + * * @throws \Exception */ public function commits($prNumber) @@ -131,8 +132,9 @@ public function commits($prNumber) /** * Approve pull request. * - * @param array $prNumbers + * @param array $prNumbers * @return void + * * @throws \Exception */ public function approve(...$prNumbers) @@ -163,8 +165,9 @@ public function approve(...$prNumbers) /** * Revert pull request to not approved status. * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function unApprove($prNumber) @@ -175,8 +178,9 @@ public function unApprove($prNumber) /** * Request changes for pull request * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function requestChanges($prNumber) @@ -187,8 +191,9 @@ public function requestChanges($prNumber) /** * Revert pull request to not request changes status. * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function unRequestChanges($prNumber) @@ -199,8 +204,9 @@ public function unRequestChanges($prNumber) /** * Decline pull request. * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function decline($prNumber) @@ -212,8 +218,9 @@ public function decline($prNumber) /** * Merge pull request. * - * @param int $prNumber + * @param int $prNumber * @return void + * * @throws \Exception */ public function merge($prNumber) @@ -224,10 +231,11 @@ public function merge($prNumber) /** * Create pull request from "x" to test "y". * - * @param string $fromBranch - * @param string $toBranch - * @param int $addDefaultReviewers + * @param string $fromBranch + * @param string $toBranch + * @param int $addDefaultReviewers * @return void + * * @throws \Exception */ public function create($fromBranch, $toBranch = '', $addDefaultReviewers = 1) @@ -237,24 +245,53 @@ public function create($fromBranch, $toBranch = '', $addDefaultReviewers = 1) $fromBranch = trim(exec('git symbolic-ref --short HEAD')); } - $response = $this->makeRequest('POST', "/pullrequests", [ - 'title' => "Merge {$fromBranch} into {$toBranch}", - 'source' => [ - 'branch' => [ - 'name' => $fromBranch, + $this->bulkCreate( + explode(',', $toBranch), + $fromBranch, + $addDefaultReviewers == 1 + ); + } + + /** + * Create pull request from "x" to test "y". + * + * @param array $toBranches + * @param string $fromBranch + * @param bool $addDefaultReviewers + * @return void + * + * @throws \Exception + */ + private function bulkCreate($toBranches, $fromBranch, $addDefaultReviewers = true) + { + $responses = []; + + $defaultReviewers = $addDefaultReviewers ? $this->defaultReviewers() : []; + + foreach ($toBranches as $toBranch) { + $response = $this->makeRequest('POST', '/pullrequests', [ + 'title' => "Merge {$fromBranch} into {$toBranch}", + 'source' => [ + 'branch' => [ + 'name' => $fromBranch, + ], ], - ], - 'destination' => [ - 'branch' => [ - 'name' => $toBranch, + 'destination' => [ + 'branch' => [ + 'name' => $toBranch, + ], ], - ], - 'reviewers' => $addDefaultReviewers ? $this->defaultReviewers() : [], - ]); + 'reviewers' => $defaultReviewers, + ]); + + $responses[] = [ + 'id' => array_get($response, 'id'), + 'link' => array_get($response, 'links.html.href'), + ]; + } o([ - 'id' => array_get($response, 'id'), - 'link' => array_get($response, 'links.html.href'), + 'pullRequests' => $responses, ]); } @@ -262,12 +299,13 @@ public function create($fromBranch, $toBranch = '', $addDefaultReviewers = 1) * Get default reviewers for repository. * * @return array + * * @throws \Exception */ private function defaultReviewers() { $currentUserUuid = $this->currentUserUuid(); - $response = $this->makeRequest('GET', "/default-reviewers"); + $response = $this->makeRequest('GET', '/default-reviewers'); // remove current user from reviewers return array_values(array_filter($response['values'] ?? [], function ($reviewer) use ($currentUserUuid) { @@ -279,6 +317,7 @@ private function defaultReviewers() * Get current user uuid. * * @return string + * * @throws \Exception */ private function currentUserUuid()