Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions bin/bb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@ $staticCommands = [
],
];

// Check for --project argument and extract it
// Check for --project, --title and --description arguments and extract them
$projectUrl = null;
$prTitle = null;
$prDescription = null;
$interactive = false;
$filteredArgv = [];
for ($i = 0; $i < count($argv); $i++) {
if ($argv[$i] === '--project' && isset($argv[$i + 1])) {
$projectUrl = $argv[$i + 1];
$i++; // Skip the next argument (the URL)
} elseif ($argv[$i] === '--title' && isset($argv[$i + 1])) {
$prTitle = $argv[$i + 1];
$i++;
} elseif ($argv[$i] === '--description' && isset($argv[$i + 1])) {
$prDescription = $argv[$i + 1];
$i++;
} elseif ($argv[$i] === '-i' || $argv[$i] === '--interactive') {
$interactive = true;
} else {
$filteredArgv[] = $argv[$i];
}
Expand All @@ -77,6 +88,17 @@ if ($projectUrl) {
$GLOBALS['bb_cli_project_url'] = $projectUrl;
}

// Set global PR title and description for use in Pr::create()
if ($prTitle) {
$GLOBALS['bb_cli_pr_title'] = $prTitle;
}
if ($prDescription) {
$GLOBALS['bb_cli_pr_description'] = $prDescription;
}
if ($interactive) {
$GLOBALS['bb_cli_interactive'] = true;
}

$userBaseAction = $argv[1] ?? null;

// Print version
Expand All @@ -91,7 +113,10 @@ if (is_null($userBaseAction) || in_array($userBaseAction, $staticCommands['help'
o(array_keys($actions), 'green');
o('', 'white');
o('Global options:', 'green');
o(' --project <repo> Work with repository (e.g., --project "owner/repo" or --project "https://bitbucket.org/owner/repo")', 'yellow');
o(' --project <repo> Work with repository (e.g., --project "owner/repo" or --project "https://bitbucket.org/owner/repo")', 'yellow');
o(' -i, --interactive Enable interactive mode (e.g., prompt for PR title and description)', 'yellow');
o(' --title <title> Set PR title (used with pr create)', 'yellow');
o(' --description <desc> Set PR description (used with pr create)', 'yellow');
exit(0);
}

Expand Down
33 changes: 28 additions & 5 deletions src/Actions/Pr.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,25 @@ public function create($fromBranch, $toBranch = '', $addDefaultReviewers = 1)
$fromBranch = trim(exec('git symbolic-ref --short HEAD'));
}

$interactive = !empty($GLOBALS['bb_cli_interactive']);
$title = $GLOBALS['bb_cli_pr_title'] ?? null;
$description = $GLOBALS['bb_cli_pr_description'] ?? null;

if ($interactive) {
if (!$title) {
$title = getUserInput('PR title (leave empty for default):') ?: null;
}
if (!$description) {
$description = getUserInput('PR description (leave empty to skip):') ?: null;
}
}

$this->bulkCreate(
explode(',', $toBranch),
$fromBranch,
$addDefaultReviewers == 1
$addDefaultReviewers == 1,
$title,
$description
);
}

Expand All @@ -259,19 +274,21 @@ public function create($fromBranch, $toBranch = '', $addDefaultReviewers = 1)
* @param array $toBranches
* @param string $fromBranch
* @param bool $addDefaultReviewers
* @param string|null $title
* @param string|null $description
* @return void
*
* @throws \Exception
*/
private function bulkCreate($toBranches, $fromBranch, $addDefaultReviewers = true)
private function bulkCreate($toBranches, $fromBranch, $addDefaultReviewers = true, $title = null, $description = null)
{
$responses = [];

$defaultReviewers = $addDefaultReviewers ? $this->defaultReviewers() : [];

foreach ($toBranches as $toBranch) {
$response = $this->makeRequest('POST', '/pullrequests', [
'title' => "Merge {$fromBranch} into {$toBranch}",
$payload = [
'title' => $title ?? "Merge {$fromBranch} into {$toBranch}",
'source' => [
'branch' => [
'name' => $fromBranch,
Expand All @@ -283,7 +300,13 @@ private function bulkCreate($toBranches, $fromBranch, $addDefaultReviewers = tru
],
],
'reviewers' => $defaultReviewers,
]);
];

if ($description) {
$payload['description'] = $description;
}

$response = $this->makeRequest('POST', '/pullrequests', $payload);

$responses[] = [
'id' => array_get($response, 'id'),
Expand Down