#!/usr/bin/env php * * This file is part of a Moko Consulting project. * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: moko-platform.CLI * INGROUP: moko-platform * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /cli/create_repo.php * BRIEF: Scaffold a new governed repository with full moko-platform baseline */ declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../lib/Enterprise/CliFramework.php'; use MokoEnterprise\CliFramework; use MokoEnterprise\Config; use MokoEnterprise\PlatformAdapterFactory; class CreateRepoCli extends CliFramework { protected function configure(): void { $this->setDescription('Scaffold a new governed repository with full moko-platform baseline'); $this->addArgument('--name', 'Repository name', null); $this->addArgument('--type', 'Project type', null); $this->addArgument('--description', 'Repository description', ''); $this->addArgument('--private', 'Create as private', false); } protected function run(): int { $name = $this->getArgument('--name'); $type = $this->getArgument('--type'); $description = $this->getArgument('--description'); $private = (bool) $this->getArgument('--private'); if (!$name || !$type) { $this->log('ERROR', "Usage: php create_repo.php --name --type [--description \"...\"] [--private] [--dry-run]"); return 2; } $config = Config::load(); $adapter = PlatformAdapterFactory::create($config); $org = $config->getString($adapter->getPlatformName() . '.organization', 'mokoconsulting-tech'); $repoRoot = dirname(__DIR__, 2); $TYPE_TO_PLATFORM = [ 'dolibarr' => 'crm-module', 'dolibarr-platform' => 'crm-platform', 'joomla' => 'waas-component', 'nodejs' => 'nodejs', 'terraform' => 'terraform', 'python' => 'python', 'wordpress' => 'wordpress', 'generic' => 'generic', ]; $TYPE_TO_TOPICS = [ 'dolibarr' => ['dolibarr', 'erp', 'crm', 'php', 'mokostandards'], 'joomla' => ['joomla', 'cms', 'php', 'mokostandards'], 'nodejs' => ['nodejs', 'javascript', 'typescript', 'mokostandards'], 'terraform' => ['terraform', 'infrastructure', 'iac', 'mokostandards'], 'python' => ['python', 'mokostandards'], 'wordpress' => ['wordpress', 'php', 'cms', 'mokostandards'], 'generic' => ['mokostandards'], ]; $platform = $TYPE_TO_PLATFORM[$type] ?? 'generic'; $topics = $TYPE_TO_TOPICS[$type] ?? ['mokostandards']; $platformName = $adapter->getPlatformName(); $vis = $private ? 'private' : 'public'; echo "Scaffolding new repository: {$org}/{$name}" . " (on {$platformName})\n" . " Type: {$type} (platform: {$platform})\n" . " Visibility: {$vis}\n"; if ($description) { echo " Description: {$description}\n"; } echo "\n"; echo "Step 1: Creating repository...\n"; if (!$this->dryRun) { try { $data = $adapter->createOrgRepo($org, $name, [ 'description' => $description ?: "Managed by moko-platform ({$type})", 'private' => $private, 'has_issues' => true, 'has_projects' => true, 'has_wiki' => false, 'auto_init' => true, 'delete_branch_on_merge' => true, 'allow_squash_merge' => true, 'allow_merge_commit' => false, 'allow_rebase_merge' => false, ]); echo " Created: " . ($data['html_url'] ?? "{$org}/{$name}") . "\n"; } catch (\Exception $e) { if (str_contains($e->getMessage(), '422') || str_contains($e->getMessage(), 'already exists')) { echo " Repository already exists -- continuing with setup\n"; } else { $this->log('ERROR', "Failed to create repo: " . $e->getMessage()); return 1; } } } else { echo " (dry-run) would create {$org}/{$name}\n"; } echo "Step 2: Setting topics...\n"; if (!$this->dryRun) { $adapter->setRepoTopics($org, $name, $topics); echo " Topics: " . implode(', ', $topics) . "\n"; } else { echo " (dry-run) would set topics: " . implode(', ', $topics) . "\n"; } echo "Step 3: Creating .github/.mokostandards...\n"; $mokoContent = "platform: {$platform}\nversion: 04.02.30\nmanaged: true\n"; if (!$this->dryRun) { try { $adapter->createOrUpdateFile( $org, $name, '.github/.mokostandards', $mokoContent, 'chore: add .mokostandards platform config [skip ci]' ); echo " .mokostandards created\n"; } catch (\Exception $e) { echo " Warning: " . $e->getMessage() . "\n"; } } else { echo " (dry-run) would create .github/.mokostandards\n"; } echo "Step 4: Creating README.md...\n"; $baseUrl = $platformName === 'gitea' ? $config->getString('gitea.url', 'https://git.mokoconsulting.tech') : 'https://github.com'; $repoUrl = "{$baseUrl}/{$org}/{$name}"; $standardsUrl = "{$baseUrl}/{$org}/MokoStandards"; $readmeContent = "\n\n" . "# {$name}\n\n" . "{$description}\n\n" . "## Getting Started\n\n" . "This repository is governed by" . " [moko-platform]({$standardsUrl}).\n\n" . "## License\n\n" . "GPL-3.0-or-later. See [LICENSE](LICENSE)" . " for details.\n"; if (!$this->dryRun) { $sha = null; try { $existing = $adapter->getFileContents($org, $name, 'README.md'); $sha = $existing['sha'] ?? null; } catch (\Exception $e) { $adapter->getApiClient()->resetCircuitBreaker(); } $adapter->createOrUpdateFile( $org, $name, 'README.md', $readmeContent, 'docs: initialize README with moko-platform header [skip ci]', $sha ); echo " README.md created\n"; } else { echo " (dry-run) would create README.md\n"; } echo "Step 5: Provisioning labels...\n"; if (!$this->dryRun) { $labelScript = "{$repoRoot}/api/maintenance/setup_labels.php"; if (file_exists($labelScript)) { $exitCode = 0; passthru("php " . escapeshellarg($labelScript) . " --org " . escapeshellarg($org) . " --repo " . escapeshellarg($name), $exitCode); } else { echo " Labels will be provisioned on next sync\n"; } } else { echo " (dry-run) would provision standard labels\n"; } echo "Step 6: Running initial sync...\n"; if (!$this->dryRun) { $syncScript = "{$repoRoot}/api/automation/bulk_sync.php"; if (file_exists($syncScript)) { passthru("php " . escapeshellarg($syncScript) . " --repos " . escapeshellarg($name) . " --force --yes"); } else { echo " Run manually: php automation/bulk_sync.php --repos {$name} --force --yes\n"; } } else { echo " (dry-run) would run initial sync\n"; } echo "Step 7: Creating Project...\n"; if (!$this->dryRun) { $projectScript = "{$repoRoot}/api/cli/create_project.php"; if (file_exists($projectScript)) { passthru("php " . escapeshellarg($projectScript) . " --repo " . escapeshellarg($name) . " --type " . escapeshellarg($type)); } else { echo " Run manually: php cli/create_project.php --repo {$name} --type {$type}\n"; } } else { echo " (dry-run) would create Project\n"; } echo "\n" . str_repeat('-', 50) . "\n" . "Repository {$org}/{$name} scaffolded successfully\n" . " URL: {$repoUrl}\n" . " Platform: {$platform} ({$platformName})\n" . " Next: verify the sync and merge any PRs\n"; return 0; } } $app = new CreateRepoCli(); exit($app->execute());