refactor(cli): migrate 64 legacy scripts to CliFramework (#235)
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (push) Blocked by required conditions
Platform: moko-platform CI / Gate 5: Template Integrity (push) Blocked by required conditions
Platform: moko-platform CI / CI Summary (push) Blocked by required conditions
Generic: Repo Health / Release configuration (push) Blocked by required conditions
Generic: Repo Health / Scripts governance (push) Blocked by required conditions
Generic: Repo Health / Repository health (push) Blocked by required conditions
Generic: Repo Health / Site Health (push) Has been skipped
Generic: Repo Health / Access control (push) Successful in 2s
Platform: moko-platform CI / Gate 1: Code Quality (push) Failing after 36s
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Blocked by required conditions
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Blocked by required conditions
Platform: moko-platform CI / Gate 4: Governance (push) Blocked by required conditions
Platform: moko-platform CI / Gate 5: Template Integrity (push) Blocked by required conditions
Platform: moko-platform CI / CI Summary (push) Blocked by required conditions
Generic: Repo Health / Release configuration (push) Blocked by required conditions
Generic: Repo Health / Scripts governance (push) Blocked by required conditions
Generic: Repo Health / Repository health (push) Blocked by required conditions
Generic: Repo Health / Site Health (push) Has been skipped
Generic: Repo Health / Access control (push) Successful in 2s
Platform: moko-platform CI / Gate 1: Code Quality (push) Failing after 36s
Wrap all CLI tools in cli/, automation/, maintenance/, deploy/, and release/ in classes extending CliFramework. Replaces manual $argv parsing with configure()/addArgument(), moves logic into run(): int, and converts fwrite(STDERR,...) to $this->log(). Two CLIApp subclasses (generate_dolibarr_version_txt, generate_joomla_update_xml) converted to extend CliFramework directly. Every script now gets free --help, --verbose, --quiet, --dry-run, --json, --no-color, banners, coloured logging, and progress bars. Authored-by: Moko Consulting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+410
-454
@@ -12,469 +12,425 @@
|
||||
* 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);
|
||||
require_once __DIR__ . '/../lib/Enterprise/CliFramework.php';
|
||||
|
||||
$org = 'mokoconsulting-tech';
|
||||
$repoName = null;
|
||||
$typeOverride = null;
|
||||
use MokoEnterprise\CliFramework;
|
||||
|
||||
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 <name> [--type <type>] [--dry-run]\n");
|
||||
fwrite(STDERR, " php create_project.php --all [--org <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<string, mixed>
|
||||
*/
|
||||
function graphql(string $query, array $variables, string $token, string $platformName = 'gitea'): array
|
||||
class CreateProjectCli extends CliFramework
|
||||
{
|
||||
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);
|
||||
/** @var string[] */
|
||||
private array $ALWAYS_EXCLUDE = ['moko-platform', '.github-private'];
|
||||
|
||||
if ($status !== 200) {
|
||||
fwrite(STDERR, "GraphQL request failed (HTTP {$status}): {$body}\n");
|
||||
return [];
|
||||
}
|
||||
/** @var array<string, string> */
|
||||
private array $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',
|
||||
];
|
||||
|
||||
$data = json_decode($body, true) ?? [];
|
||||
if (!empty($data['errors'])) {
|
||||
foreach ($data['errors'] as $err) {
|
||||
fwrite(STDERR, " GraphQL error: " . ($err['message'] ?? 'unknown') . "\n");
|
||||
}
|
||||
}
|
||||
/** @var array<string, string> */
|
||||
private array $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',
|
||||
];
|
||||
|
||||
return $data['data'] ?? [];
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setDescription('Create baseline GitHub Projects for repositories with standard fields and views');
|
||||
$this->addArgument('--repo', 'Repository name', '');
|
||||
$this->addArgument('--org', 'Organization (default: mokoconsulting-tech)', 'mokoconsulting-tech');
|
||||
$this->addArgument('--type', 'Force project type', '');
|
||||
$this->addArgument('--all', 'Process all repos without projects', false);
|
||||
}
|
||||
|
||||
protected function run(): int
|
||||
{
|
||||
$repoName = $this->getArgument('--repo') ?: null;
|
||||
$org = $this->getArgument('--org');
|
||||
$typeOverride = $this->getArgument('--type') ?: null;
|
||||
$allMode = $this->getArgument('--all');
|
||||
|
||||
if (!$repoName && !$allMode) {
|
||||
$this->log('ERROR', "Usage: php create_project.php --repo <name> [--type <type>] [--dry-run]");
|
||||
$this->log('ERROR', " php create_project.php --all [--org <org>] [--dry-run]");
|
||||
$this->log('ERROR', "Types: generic, dolibarr, joomla, nodejs, terraform, python, wordpress, mobile-app, api, documentation");
|
||||
return 2;
|
||||
}
|
||||
|
||||
$config = \MokoEnterprise\Config::load();
|
||||
$platformName = $config->getString('platform', 'gitea');
|
||||
try {
|
||||
$adapter = \MokoEnterprise\PlatformAdapterFactory::create($config);
|
||||
$api = $adapter->getApiClient();
|
||||
} catch (\Exception $e) {
|
||||
$this->log('ERROR', "Platform initialization failed: " . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
$token = $platformName === 'gitea'
|
||||
? $config->getString('gitea.token', '')
|
||||
: $config->getString('github.token', '');
|
||||
|
||||
$repoRoot = dirname(__DIR__, 2);
|
||||
$templatesDir = "{$repoRoot}/templates/projects";
|
||||
|
||||
$repos = [];
|
||||
|
||||
if ($allMode) {
|
||||
echo "Fetching repositories from {$org}...\n";
|
||||
$page = 1;
|
||||
do {
|
||||
$batch = $this->restGet("orgs/{$org}/repos?per_page=100&page={$page}&type=all", $token, $api);
|
||||
foreach ($batch as $r) {
|
||||
if (!$r['archived'] && !in_array($r['name'], $this->ALWAYS_EXCLUDE, true)) {
|
||||
$repos[] = $r['name'];
|
||||
}
|
||||
}
|
||||
$page++;
|
||||
} while (count($batch) === 100);
|
||||
|
||||
sort($repos);
|
||||
echo "Found " . count($repos) . " repositories\n\n";
|
||||
} else {
|
||||
$repos = [$repoName];
|
||||
}
|
||||
|
||||
$ownerId = $this->getOrgNodeId($org, $token);
|
||||
if (empty($ownerId)) {
|
||||
$this->log('ERROR', "Could not resolve org node ID for {$org}");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$created = 0;
|
||||
$skipped = 0;
|
||||
$failed = 0;
|
||||
|
||||
foreach ($repos as $repo) {
|
||||
echo "Processing {$repo}...\n";
|
||||
|
||||
[$hasProject, $existingTitle] = $this->repoHasProject($org, $repo, $token);
|
||||
if ($hasProject) {
|
||||
echo " Already has project: {$existingTitle} -- skipping\n";
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$type = $typeOverride;
|
||||
if (!$type) {
|
||||
$platform = $this->detectRepoPlatform($org, $repo, $token, $api);
|
||||
$type = $this->PLATFORM_TO_TYPE[$platform] ?? 'generic';
|
||||
echo " Platform: {$platform} -> type: {$type}\n";
|
||||
}
|
||||
|
||||
$templateFile = $this->TYPE_TO_TEMPLATE[$type] ?? $this->TYPE_TO_TEMPLATE['generic'];
|
||||
$template = $this->parseTemplate("{$templatesDir}/{$templateFile}");
|
||||
|
||||
$repoId = $this->getRepoNodeId($org, $repo, $token);
|
||||
if (empty($repoId)) {
|
||||
$this->log('ERROR', " Could not resolve repo node ID for {$repo}");
|
||||
$failed++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$ok = $this->createProject($org, $repo, $ownerId, $repoId, $template, $token);
|
||||
if ($ok) {
|
||||
$created++;
|
||||
} else {
|
||||
$failed++;
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
echo str_repeat('-', 50) . "\n";
|
||||
echo "Done: {$created} created, {$skipped} skipped, {$failed} failed\n";
|
||||
return $failed > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
private 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: moko-platform-CreateProject',
|
||||
],
|
||||
]);
|
||||
$body = (string) curl_exec($ch);
|
||||
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($status !== 200) {
|
||||
$this->log('ERROR', "GraphQL request failed (HTTP {$status}): {$body}");
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode($body, true) ?? [];
|
||||
if (!empty($data['errors'])) {
|
||||
foreach ($data['errors'] as $err) {
|
||||
$this->log('ERROR', " GraphQL error: " . ($err['message'] ?? 'unknown'));
|
||||
}
|
||||
}
|
||||
|
||||
return $data['data'] ?? [];
|
||||
}
|
||||
|
||||
private function restGet(string $path, string $token, ?\MokoEnterprise\ApiClient $apiClient = null): array
|
||||
{
|
||||
if ($apiClient !== null) {
|
||||
try {
|
||||
return $apiClient->get("/{$path}");
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
private function detectRepoPlatform(string $org, string $repo, string $token, ?\MokoEnterprise\ApiClient $apiClient = null): string
|
||||
{
|
||||
foreach (['.github/.mokostandards', '.mokogitea/.mokostandards', '.mokostandards'] as $path) {
|
||||
$data = $this->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 '';
|
||||
}
|
||||
|
||||
private function getRepoNodeId(string $org, string $repo, string $token): string
|
||||
{
|
||||
$data = $this->graphql(
|
||||
'query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { id } }',
|
||||
['owner' => $org, 'name' => $repo],
|
||||
$token
|
||||
);
|
||||
return $data['repository']['id'] ?? '';
|
||||
}
|
||||
|
||||
private function getOrgNodeId(string $org, string $token): string
|
||||
{
|
||||
$data = $this->graphql(
|
||||
'query($login: String!) { organization(login: $login) { id } }',
|
||||
['login' => $org],
|
||||
$token
|
||||
);
|
||||
return $data['organization']['id'] ?? '';
|
||||
}
|
||||
|
||||
/** @return array{bool, string} */
|
||||
private function repoHasProject(string $org, string $repo, string $token): array
|
||||
{
|
||||
$data = $this->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];
|
||||
}
|
||||
|
||||
/** @return array{name: string, fields: array, views: array} */
|
||||
private 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' => []];
|
||||
|
||||
if (preg_match('/name\s*=\s*"([^"]+)"/', $content, $m)) {
|
||||
$result['name'] = $m[1];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private function createProject(
|
||||
string $org,
|
||||
string $repo,
|
||||
string $ownerId,
|
||||
string $repoId,
|
||||
array $template,
|
||||
string $token
|
||||
): bool {
|
||||
$title = "{$repo} -- {$template['name']}";
|
||||
|
||||
if ($this->dryRun) {
|
||||
echo " (dry-run) would create project: {$title}\n";
|
||||
echo " (dry-run) fields: " . count($template['fields']) . "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
echo " Creating project: {$title}\n";
|
||||
$data = $this->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)) {
|
||||
$this->log('ERROR', " Failed to create project for {$repo}");
|
||||
return false;
|
||||
}
|
||||
|
||||
echo " Project created: {$projectUrl}\n";
|
||||
|
||||
$this->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";
|
||||
|
||||
$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,
|
||||
];
|
||||
|
||||
if ($fieldType === 'SINGLE_SELECT' && !empty($field['options'])) {
|
||||
$optionInputs = array_map(
|
||||
fn($o) => ['name' => $o, 'description' => '', 'color' => 'GRAY'],
|
||||
$field['options']
|
||||
);
|
||||
$vars['singleSelectOptions'] = $optionInputs;
|
||||
|
||||
$this->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 {
|
||||
$this->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";
|
||||
|
||||
$this->graphql(
|
||||
'mutation($projectId: ID!, $shortDescription: String!) {
|
||||
updateProjectV2(input: {
|
||||
projectId: $projectId,
|
||||
shortDescription: $shortDescription,
|
||||
readme: "Managed by moko-platform. Run `php cli/create_project.php` to regenerate."
|
||||
}) {
|
||||
projectV2 { id }
|
||||
}
|
||||
}',
|
||||
[
|
||||
'projectId' => $projectId,
|
||||
'shortDescription' => "Standard project board for {$repo}. Auto-created by moko-platform.",
|
||||
],
|
||||
$token
|
||||
);
|
||||
|
||||
echo " Project setup complete\n";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a REST API GET call via the platform adapter's ApiClient.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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 detectRepoPlatform(string $org, string $repo, string $token, ?\MokoEnterprise\ApiClient $apiClient = null): string
|
||||
{
|
||||
// Try platform metadata dir first, then root
|
||||
foreach (['.github/.mokostandards', '.mokogitea/.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 = detectRepoPlatform($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);
|
||||
$app = new CreateProjectCli();
|
||||
exit($app->execute());
|
||||
|
||||
Reference in New Issue
Block a user