|
6 | 6 |
|
7 | 7 | use Illuminate\Support\Facades\File; |
8 | 8 | use LaravelPlus\LaravelUpdater\Traits\HasPrompts; |
| 9 | +use LaravelPlus\LaravelUpdater\Support\VersionControl; |
9 | 10 |
|
10 | 11 | final class UpstreamSetupCommand extends AbstractUpstreamCommand |
11 | 12 | { |
@@ -106,6 +107,9 @@ private function setupPreset(string $preset): int |
106 | 107 | 'UPSTREAM_PRESET' => $preset, |
107 | 108 | ]); |
108 | 109 |
|
| 110 | + // Optional GitHub PR auto-creation setup |
| 111 | + $this->setupGitHubPrOption(); |
| 112 | + |
109 | 113 | // Update config file if needed |
110 | 114 | if ($this->option('force') || $this->confirmPrompt('Update config/upstream.php with preset hooks?', default: true)) { |
111 | 115 | $this->updateConfigFile($config); |
@@ -160,6 +164,9 @@ private function setupCustom(): int |
160 | 164 | 'UPSTREAM_PRESET' => 'custom', |
161 | 165 | ]); |
162 | 166 |
|
| 167 | + // Optional GitHub PR auto-creation setup |
| 168 | + $this->setupGitHubPrOption(); |
| 169 | + |
163 | 170 | $this->newLine(); |
164 | 171 | $this->info('✅ Custom setup complete!'); |
165 | 172 | $this->line('📝 Configuration added to .env file'); |
@@ -234,4 +241,70 @@ private function updateConfigFile(array $config): void |
234 | 241 |
|
235 | 242 | File::put($configPath, $configContent); |
236 | 243 | } |
| 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 | + } |
237 | 310 | } |
0 commit comments