Public Access
feat: add templates, CLI dirs, docs, and Gitea-first platform config
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,477 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
||||
*
|
||||
* 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-tech/MokoStandards-API
|
||||
* PATH: /cli/create_project.php
|
||||
* VERSION: 04.06.00
|
||||
* BRIEF: Create baseline GitHub Projects for repositories with standard fields and views
|
||||
*
|
||||
* USAGE
|
||||
* php api/cli/create_project.php --repo MokoCRM # Auto-detect type, create project
|
||||
* php api/cli/create_project.php --repo MokoCRM --type dolibarr # Force type
|
||||
* php api/cli/create_project.php --org mokoconsulting-tech --all # All repos without projects
|
||||
* php api/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 <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);
|
||||
}
|
||||
|
||||
$token = getenv('GH_TOKEN') ?: getenv('GITHUB_TOKEN');
|
||||
if (empty($token)) {
|
||||
fwrite(STDERR, "GH_TOKEN or GITHUB_TOKEN not set\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$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 GitHub GraphQL query.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
function graphql(string $query, array $variables, string $token): array
|
||||
{
|
||||
$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 GitHub REST API GET call.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
function restGet(string $path, string $token): array
|
||||
{
|
||||
$ch = curl_init("https://api.github.com/{$path}");
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: token ' . $token,
|
||||
'User-Agent: MokoStandards-CreateProject',
|
||||
'Accept: application/vnd.github.v3+json',
|
||||
],
|
||||
]);
|
||||
$body = (string) curl_exec($ch);
|
||||
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
return $status === 200 ? (json_decode($body, true) ?? []) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect platform type from .mokostandards file in the repo.
|
||||
*/
|
||||
function detectPlatform(string $org, string $repo, string $token): string
|
||||
{
|
||||
// Try .github/.mokostandards first, then root
|
||||
foreach (['.github/.mokostandards', '.mokostandards'] as $path) {
|
||||
$data = restGet("repos/{$org}/{$repo}/contents/{$path}", $token);
|
||||
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 api/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);
|
||||
Reference in New Issue
Block a user