Skip to content

Commit e46b5be

Browse files
Copilotmglaman
andauthored
issue:checkout: auto-setup missing remote instead of hard failing (#324)
* Initial plan * feat: prompt user to set up remote when missing in issue:checkout Co-authored-by: mglaman <3698644+mglaman@users.noreply.github.com> * fix: cast ask() result to bool to satisfy PHPStan strict-rules Co-authored-by: mglaman <3698644+mglaman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mglaman <3698644+mglaman@users.noreply.github.com>
1 parent 849b409 commit e46b5be

1 file changed

Lines changed: 42 additions & 9 deletions

File tree

src/Cli/Command/Issue/Checkout.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace mglaman\DrupalOrgCli\Command\Issue;
44

55
use mglaman\DrupalOrg\Action\Issue\GetIssueForkAction;
6+
use mglaman\DrupalOrg\Action\Issue\SetupIssueRemoteAction;
67
use mglaman\DrupalOrg\GitLab\Client as GitLabClient;
78
use Symfony\Component\Console\Input\InputArgument;
89
use Symfony\Component\Console\Input\InputInterface;
910
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Question\ConfirmationQuestion;
1012
use Symfony\Component\Process\Process;
1113

1214
class Checkout extends IssueCommandBase
@@ -22,21 +24,52 @@ protected function configure(): void
2224

2325
protected function execute(InputInterface $input, OutputInterface $output): int
2426
{
25-
$action = new GetIssueForkAction($this->client, new GitLabClient());
27+
$gitLabClient = new GitLabClient();
28+
$action = new GetIssueForkAction($this->client, $gitLabClient);
2629
$fork = $action($this->nid);
2730

28-
// Verify the remote exists locally.
31+
// Verify the remote exists locally; offer to set it up if missing.
2932
$checkRemote = new Process(['git', 'remote', 'get-url', $fork->remoteName]);
3033
$checkRemote->run();
3134
if (!$checkRemote->isSuccessful()) {
32-
$this->stdErr->writeln(
33-
sprintf(
34-
'<error>Remote %s does not exist. Run `issue:setup-remote %s` first.</error>',
35-
$fork->remoteName,
36-
$this->nid
37-
)
35+
$shouldSetup = true;
36+
if (self::$interactive) {
37+
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
38+
$helper = $this->getHelper('question');
39+
$question = new ConfirmationQuestion(
40+
sprintf(
41+
'<comment>Remote %s does not exist. Set it up now? [Y/n]</comment> ',
42+
$fork->remoteName
43+
),
44+
true
45+
);
46+
// ask() returns mixed; cast to bool and fall back to the default (true) on null.
47+
$shouldSetup = (bool) ($helper->ask($input, $output, $question) ?? true);
48+
}
49+
if (!$shouldSetup) {
50+
$this->stdErr->writeln(
51+
sprintf(
52+
'<error>Remote %s does not exist. Run `issue:setup-remote %s` first.</error>',
53+
$fork->remoteName,
54+
$this->nid
55+
)
56+
);
57+
return 1;
58+
}
59+
try {
60+
$setupAction = new SetupIssueRemoteAction($this->client, $gitLabClient);
61+
$setupResult = $setupAction($this->nid);
62+
} catch (\RuntimeException $e) {
63+
$this->stdErr->writeln(
64+
sprintf('<error>Failed to set up remote: %s</error>', $e->getMessage())
65+
);
66+
return 1;
67+
}
68+
$this->stdOut->writeln(
69+
sprintf('<info>Remote %s added and fetched.</info>', $setupResult->remoteName)
3870
);
39-
return 1;
71+
// Refresh fork data after setup so branches are populated.
72+
$fork = $action($this->nid);
4073
}
4174

4275
// Filter branches to those starting with the issue NID.

0 commit comments

Comments
 (0)