Skip to content

Commit ccbd338

Browse files
author
nejc
committed
feat: add optional GitHub PR setup to laravelplus:setup
- Add setupGitHubPrOption() method to UpstreamSetupCommand - Explain auto-PR vs manual PR workflow to users - Auto-detect GITHUB_OWNER/GITHUB_REPO from origin remote - Prompt for GitHub token with clear requirements explanation - Support both classic PAT and fine-grained PAT tokens - Write GITHUB_OWNER, GITHUB_REPO, and GITHUB_TOKEN to .env - Token is optional - manual PR fallback remains available - Integrated into both preset and custom setup flows - Better user experience with guided configuration Users can now: - Choose whether to enable auto-PR creation - Get clear token requirements explanation - Have owner/repo auto-detected from Git remotes - Skip token setup and use manual PR workflow
1 parent 8681ff9 commit ccbd338

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/Console/Commands/UpstreamSetupCommand.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Facades\File;
88
use LaravelPlus\LaravelUpdater\Traits\HasPrompts;
9+
use LaravelPlus\LaravelUpdater\Support\VersionControl;
910

1011
final class UpstreamSetupCommand extends AbstractUpstreamCommand
1112
{
@@ -106,6 +107,9 @@ private function setupPreset(string $preset): int
106107
'UPSTREAM_PRESET' => $preset,
107108
]);
108109

110+
// Optional GitHub PR auto-creation setup
111+
$this->setupGitHubPrOption();
112+
109113
// Update config file if needed
110114
if ($this->option('force') || $this->confirmPrompt('Update config/upstream.php with preset hooks?', default: true)) {
111115
$this->updateConfigFile($config);
@@ -160,6 +164,9 @@ private function setupCustom(): int
160164
'UPSTREAM_PRESET' => 'custom',
161165
]);
162166

167+
// Optional GitHub PR auto-creation setup
168+
$this->setupGitHubPrOption();
169+
163170
$this->newLine();
164171
$this->info('✅ Custom setup complete!');
165172
$this->line('📝 Configuration added to .env file');
@@ -234,4 +241,70 @@ private function updateConfigFile(array $config): void
234241

235242
File::put($configPath, $configContent);
236243
}
244+
245+
/**
246+
* Optionally configure GitHub PR auto-creation.
247+
*/
248+
private function setupGitHubPrOption(): void
249+
{
250+
$this->newLine();
251+
$this->line('<comment>Optional: Auto-create GitHub Pull Requests</comment>');
252+
$this->line('If enabled, the updater will call the GitHub API to create the PR for you.');
253+
$this->line('If disabled, it will push a branch and print a compare URL for manual PR creation.');
254+
$this->newLine();
255+
256+
$enable = $this->confirmPrompt('Enable auto-creating PRs via GitHub API?', default: false);
257+
if (! $enable) {
258+
return;
259+
}
260+
261+
// Try to infer owner/repo from origin remote
262+
$owner = (string) (config('upstream.github_owner') ?: '');
263+
$repo = (string) (config('upstream.github_repo') ?: '');
264+
265+
if ($owner === '' || $repo === '') {
266+
try {
267+
$git = new VersionControl(
268+
bin: (string) (config('upstream.git_binary') ?: 'git'),
269+
cwd: (string) (config('upstream.working_dir') ?: base_path())
270+
);
271+
$origin = $git->run(['remote', 'get-url', 'origin'], throw: false);
272+
if ($origin) {
273+
// Supported formats:
274+
// - git@github.com:Owner/Repo.git
275+
// - https://github.com/Owner/Repo.git
276+
// - https://github.com/Owner/Repo
277+
$normalized = rtrim(preg_replace('/\.git$/', '', (string) $origin) ?? '', '/');
278+
if (preg_match('#[:/]([^/]+)/([^/]+)$#', (string) $normalized, $m)) {
279+
$owner = $owner !== '' ? $owner : (string) $m[1];
280+
$repo = $repo !== '' ? $repo : (string) $m[2];
281+
}
282+
}
283+
} catch (\Throwable) {
284+
// ignore
285+
}
286+
}
287+
288+
$owner = $this->text('GitHub owner (organization or username)', default: $owner ?: '');
289+
$repo = $this->text('GitHub repository name', default: $repo ?: '');
290+
291+
$this->line('You need a GitHub token to create PRs automatically:');
292+
$this->line('- Classic PAT: grant repo scope');
293+
$this->line('- Fine-grained PAT: grant Pull requests: Read and write, Contents: Read and write');
294+
$this->line('You can also use CI’s built-in GITHUB_TOKEN with appropriate permissions.');
295+
$token = $this->secret('GitHub token (leave blank to skip for now)');
296+
297+
// Save what we have; token can be empty (manual PR fallback)
298+
$this->updateEnvFile([
299+
'GITHUB_OWNER' => $owner,
300+
'GITHUB_REPO' => $repo,
301+
] + ($token ? ['GITHUB_TOKEN' => $token] : []));
302+
303+
$this->newLine();
304+
if ($token) {
305+
$this->info('✅ GitHub PR auto-creation enabled.');
306+
} else {
307+
$this->warn('⚠️ Token not provided. PRs will be pushed and a compare URL will be printed.');
308+
}
309+
}
237310
}

0 commit comments

Comments
 (0)