#!/usr/bin/env php * * This file is part of a Moko Consulting project. * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: MokoStandards.CLI * INGROUP: MokoStandards * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /cli/create_project.php * BRIEF: Create baseline GitHub Projects for repositories with standard fields and views * * USAGE * php cli/create_project.php --repo MokoCRM # Auto-detect type, create project * php cli/create_project.php --repo MokoCRM --type dolibarr # Force type * php cli/create_project.php --org mokoconsulting-tech --all # All repos without projects * php cli/create_project.php --repo MokoCRM --dry-run # Preview without changes */ declare(strict_types=1); $dryRun = in_array('--dry-run', $argv); $allMode = in_array('--all', $argv); $org = 'mokoconsulting-tech'; $repoName = null; $typeOverride = null; foreach ($argv as $i => $arg) { if ($arg === '--repo' && isset($argv[$i + 1])) { $repoName = $argv[$i + 1]; } if ($arg === '--org' && isset($argv[$i + 1])) { $org = $argv[$i + 1]; } if ($arg === '--type' && isset($argv[$i + 1])) { $typeOverride = $argv[$i + 1]; } } if (!$repoName && !$allMode) { fwrite(STDERR, "Usage: php create_project.php --repo [--type ] [--dry-run]\n"); fwrite(STDERR, " php create_project.php --all [--org ] [--dry-run]\n"); fwrite(STDERR, "\nTypes: generic, dolibarr, joomla, nodejs, terraform, python, wordpress, mobile-app, api, documentation\n"); exit(2); } $config = \MokoEnterprise\Config::load(); $platform = $config->getString('platform', 'gitea'); try { $adapter = \MokoEnterprise\PlatformAdapterFactory::create($config); $api = $adapter->getApiClient(); } catch (\Exception $e) { fwrite(STDERR, "Platform initialization failed: " . $e->getMessage() . "\n"); exit(1); } $token = $platform === 'gitea' ? $config->getString('gitea.token', '') : $config->getString('github.token', ''); $repoRoot = dirname(__DIR__, 2); $templatesDir = "{$repoRoot}/templates/projects"; // ── Always-exclude list (no project needed) ───────────────────────────── $ALWAYS_EXCLUDE = ['MokoStandards', '.github-private']; // ── Platform type map ─────────────────────────────────────────────────── $PLATFORM_TO_TYPE = [ 'crm-module' => 'dolibarr', 'crm-platform' => 'dolibarr', 'waas-component' => 'joomla', 'waas-library' => 'joomla', 'waas-plugin' => 'joomla', 'waas-package' => 'joomla', 'nodejs' => 'nodejs', 'terraform' => 'terraform', 'python' => 'python', 'wordpress' => 'wordpress', 'mobile' => 'mobile-app', 'api' => 'api', 'documentation' => 'documentation', ]; // ── Template file map ─────────────────────────────────────────────────── $TYPE_TO_TEMPLATE = [ 'generic' => 'generic-project-definition.tf', 'dolibarr' => 'dolibarr-project-definition.tf', 'joomla' => 'joomla-project-definition.tf', 'nodejs' => 'nodejs-project-definition.tf', 'terraform' => 'terraform-project-definition.tf', 'python' => 'python-project-definition.tf', 'wordpress' => 'wordpress-project-definition.tf', 'mobile-app' => 'mobile-app-project-definition.tf', 'api' => 'api-project-definition.tf', 'documentation' => 'documentation-project-definition.tf', ]; /** * Execute a GraphQL query (GitHub only — Gitea does not support GraphQL). * * @return array */ function graphql(string $query, array $variables, string $token, string $platformName = 'gitea'): array { if ($platformName !== 'github') { return []; } $payload = json_encode(['query' => $query, 'variables' => $variables]); $ch = curl_init('https://api.github.com/graphql'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_HTTPHEADER => [ 'Authorization: bearer ' . $token, 'Content-Type: application/json', 'User-Agent: MokoStandards-CreateProject', ], ]); $body = (string) curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($status !== 200) { fwrite(STDERR, "GraphQL request failed (HTTP {$status}): {$body}\n"); return []; } $data = json_decode($body, true) ?? []; if (!empty($data['errors'])) { foreach ($data['errors'] as $err) { fwrite(STDERR, " GraphQL error: " . ($err['message'] ?? 'unknown') . "\n"); } } return $data['data'] ?? []; } /** * Execute a REST API GET call via the platform adapter's ApiClient. * * @return array */ function restGet(string $path, string $token, ?\MokoEnterprise\ApiClient $apiClient = null): array { if ($apiClient !== null) { try { return $apiClient->get("/{$path}"); } catch (\Exception $e) { return []; } } return []; } /** * Detect platform type from .mokostandards file in the repo. */ function detectPlatform(string $org, string $repo, string $token, ?\MokoEnterprise\ApiClient $apiClient = null): string { // Try platform metadata dir first, then root foreach (['.github/.mokostandards', '.gitea/.mokostandards', '.mokostandards'] as $path) { $data = restGet("repos/{$org}/{$repo}/contents/{$path}", $token, $apiClient); if (!empty($data['content'])) { $content = base64_decode($data['content']); if (preg_match('/^platform:\s*(.+)/m', $content, $m)) { return trim($m[1], " \t\n\r\"'"); } } } return ''; } /** * Get the GitHub node ID for a repository. */ function getRepoNodeId(string $org, string $repo, string $token): string { $data = graphql( 'query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { id } }', ['owner' => $org, 'name' => $repo], $token ); return $data['repository']['id'] ?? ''; } /** * Get the GitHub node ID for the organization owner. */ function getOrgNodeId(string $org, string $token): string { $data = graphql( 'query($login: String!) { organization(login: $login) { id } }', ['login' => $org], $token ); return $data['organization']['id'] ?? ''; } /** * Check if a repo already has a GitHub Project linked. * * @return array{bool, string} [hasProject, projectTitle] */ function repoHasProject(string $org, string $repo, string $token): array { $data = graphql( 'query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { projectsV2(first: 1) { nodes { id title } totalCount } } }', ['owner' => $org, 'name' => $repo], $token ); $count = $data['repository']['projectsV2']['totalCount'] ?? 0; $title = $data['repository']['projectsV2']['nodes'][0]['title'] ?? ''; return [$count > 0, $title]; } /** * Parse a .tf template file to extract custom fields. * * @return array{name: string, fields: array, views: array} */ function parseTemplate(string $filePath): array { if (!file_exists($filePath)) { return ['name' => 'Development Board', 'fields' => [], 'views' => []]; } $content = file_get_contents($filePath); $result = ['name' => 'Development Board', 'fields' => [], 'views' => []]; // Extract project name if (preg_match('/name\s*=\s*"([^"]+)"/', $content, $m)) { $result['name'] = $m[1]; } // Extract custom fields if (preg_match_all('/\{\s*name\s*=\s*"([^"]+)"\s*type\s*=\s*"([^"]+)"\s*description\s*=\s*"([^"]+)"(?:\s*options\s*=\s*\[([^\]]*)\])?\s*\}/s', $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $field = [ 'name' => $match[1], 'type' => $match[2], 'description' => $match[3], ]; if (!empty($match[4])) { $field['options'] = array_map( fn($o) => trim($o, " \t\n\r\"'"), explode(',', $match[4]) ); $field['options'] = array_filter($field['options']); } $result['fields'][] = $field; } } return $result; } /** * Create a GitHub Project V2 for a repository. */ function createProject( string $org, string $repo, string $ownerId, string $repoId, array $template, string $token, bool $dryRun ): bool { $title = "{$repo} — {$template['name']}"; if ($dryRun) { echo " (dry-run) would create project: {$title}\n"; echo " (dry-run) fields: " . count($template['fields']) . "\n"; return true; } // Step 1: Create the project echo " Creating project: {$title}\n"; $data = graphql( 'mutation($ownerId: ID!, $title: String!) { createProjectV2(input: { ownerId: $ownerId, title: $title }) { projectV2 { id number url } } }', ['ownerId' => $ownerId, 'title' => $title], $token ); $projectId = $data['createProjectV2']['projectV2']['id'] ?? ''; $projectUrl = $data['createProjectV2']['projectV2']['url'] ?? ''; if (empty($projectId)) { fwrite(STDERR, " Failed to create project for {$repo}\n"); return false; } echo " Project created: {$projectUrl}\n"; // Step 2: Link the project to the repository graphql( 'mutation($projectId: ID!, $repositoryId: ID!) { linkProjectV2ToRepository(input: { projectId: $projectId, repositoryId: $repositoryId }) { repository { id } } }', ['projectId' => $projectId, 'repositoryId' => $repoId], $token ); echo " Linked to {$org}/{$repo}\n"; // Step 3: Create custom fields $fieldCount = 0; foreach ($template['fields'] as $field) { $fieldType = match ($field['type']) { 'single_select' => 'SINGLE_SELECT', 'text' => 'TEXT', 'number' => 'NUMBER', 'date' => 'DATE', 'iteration' => 'ITERATION', default => 'TEXT', }; $vars = [ 'projectId' => $projectId, 'name' => $field['name'], 'dataType' => $fieldType, ]; // Single select fields need options created with the field if ($fieldType === 'SINGLE_SELECT' && !empty($field['options'])) { $optionInputs = array_map( fn($o) => ['name' => $o, 'description' => '', 'color' => 'GRAY'], $field['options'] ); $vars['singleSelectOptions'] = $optionInputs; graphql( 'mutation($projectId: ID!, $name: String!, $dataType: ProjectV2CustomFieldType!, $singleSelectOptions: [ProjectV2SingleSelectFieldOptionInput!]) { createProjectV2Field(input: { projectId: $projectId, dataType: $dataType, name: $name, singleSelectOptions: $singleSelectOptions }) { projectV2Field { ... on ProjectV2SingleSelectField { id name } } } }', $vars, $token ); } else { graphql( 'mutation($projectId: ID!, $name: String!, $dataType: ProjectV2CustomFieldType!) { createProjectV2Field(input: { projectId: $projectId, dataType: $dataType, name: $name }) { projectV2Field { ... on ProjectV2Field { id name } } } }', $vars, $token ); } $fieldCount++; } echo " Created {$fieldCount} custom fields\n"; // Step 4: Update project description and README graphql( 'mutation($projectId: ID!, $shortDescription: String!) { updateProjectV2(input: { projectId: $projectId, shortDescription: $shortDescription, readme: "Managed by MokoStandards. Run `php cli/create_project.php` to regenerate." }) { projectV2 { id } } }', [ 'projectId' => $projectId, 'shortDescription' => "Standard project board for {$repo}. Auto-created by MokoStandards.", ], $token ); echo " Project setup complete\n"; return true; } // ── Main ──────────────────────────────────────────────────────────────── $repos = []; if ($allMode) { echo "Fetching repositories from {$org}...\n"; $page = 1; do { $batch = restGet("orgs/{$org}/repos?per_page=100&page={$page}&type=all", $token); foreach ($batch as $r) { if (!$r['archived'] && !in_array($r['name'], $ALWAYS_EXCLUDE, true)) { $repos[] = $r['name']; } } $page++; } while (count($batch) === 100); sort($repos); echo "Found " . count($repos) . " repositories\n\n"; } else { $repos = [$repoName]; } $ownerId = getOrgNodeId($org, $token); if (empty($ownerId)) { fwrite(STDERR, "Could not resolve org node ID for {$org}\n"); exit(1); } $created = 0; $skipped = 0; $failed = 0; foreach ($repos as $repo) { echo "Processing {$repo}...\n"; // Check if project already exists [$hasProject, $existingTitle] = repoHasProject($org, $repo, $token); if ($hasProject) { echo " Already has project: {$existingTitle} — skipping\n"; $skipped++; continue; } // Detect project type $type = $typeOverride; if (!$type) { $platform = detectPlatform($org, $repo, $token); $type = $PLATFORM_TO_TYPE[$platform] ?? 'generic'; echo " Platform: {$platform} → type: {$type}\n"; } // Load template $templateFile = $TYPE_TO_TEMPLATE[$type] ?? $TYPE_TO_TEMPLATE['generic']; $template = parseTemplate("{$templatesDir}/{$templateFile}"); // Get repo node ID $repoId = getRepoNodeId($org, $repo, $token); if (empty($repoId)) { fwrite(STDERR, " Could not resolve repo node ID for {$repo}\n"); $failed++; continue; } // Create the project $ok = createProject($org, $repo, $ownerId, $repoId, $template, $token, $dryRun); if ($ok) { $created++; } else { $failed++; } echo "\n"; } echo str_repeat('-', 50) . "\n"; echo "Done: {$created} created, {$skipped} skipped, {$failed} failed\n"; exit($failed > 0 ? 1 : 0);