Files
MokoCLI/lib/Enterprise/PlatformAdapterFactory.php
T
jmiller 7728b6b4ac
Universal: Pre-Release / Build Pre-Release (${{ inputs.stability || github.ref_name }}) (push) Successful in 12s
Universal: PR Check / Branch Policy (pull_request) Successful in 1s
Universal: PR Check / Validate PR (pull_request) Failing after 7s
Universal: PR Check / Secret Scan (pull_request) Successful in 10s
Platform: mokocli CI / Gate 1: Code Quality (pull_request) Failing after 1m3s
Platform: mokocli CI / Gate 2: Unit Tests (8.1) (pull_request) Has been cancelled
Platform: mokocli CI / Gate 2: Unit Tests (8.2) (pull_request) Has been cancelled
Platform: mokocli CI / Gate 2: Unit Tests (8.3) (pull_request) Has been cancelled
Platform: mokocli CI / Gate 3: Self-Health Check (pull_request) Has been cancelled
Platform: mokocli CI / Gate 4: Governance (pull_request) Has been cancelled
Platform: mokocli CI / Gate 5: Template Integrity (pull_request) Has been cancelled
Platform: mokocli CI / CI Summary (pull_request) Has been cancelled
Universal: PR Check / Build RC Package (pull_request) Has been cancelled
Universal: PR Check / Report Issues (pull_request) Has been cancelled
refactor(rename): Gitea/MokoGitea -> MokoGit across the codebase [#4]
MokoGitea instance rebrand to MokoGit. Content sweep over 265 files (ordered to avoid
moko-prefix doubling) plus path renames:
- .mokogitea/ -> .mokogit/ (repo + all mcp/servers/*)
- templates/mokogitea/ -> templates/mokogit/
- lib/Enterprise/MokoGiteaAdapter.php -> MokoGitAdapter.php (class MokoGitAdapter)
- mcp/servers/mokogitea_{skill,api} -> mokogit_{skill,api}; skills/mokogitea -> skills/mokogit
- automation/migrate_to_gitea.php -> migrate_to_mokogit.php
- env/secret names GITEA_* -> MOKOGIT_*

Note: ~/.claude/.mcp.json server flipped to @mokoconsulting/mcp-mokogit (publish pending).
2026-07-16 16:49:12 -05:00

189 lines
6.3 KiB
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: MokoPlatform.Enterprise.Platform
* INGROUP: MokoPlatform.Enterprise
* REPO: https://git.mokoconsulting.tech/MokoConsulting/mokocli
* PATH: /lib/Enterprise/PlatformAdapterFactory.php
* BRIEF: Factory for creating platform-specific GitPlatformAdapter instances
*/
declare(strict_types=1);
namespace MokoCli;
use RuntimeException;
/**
* Factory for creating GitPlatformAdapter instances.
*
* Reads GIT_PLATFORM env var (default: 'github') and constructs
* the appropriate adapter with correct base URL, auth scheme, and token.
*
* Usage:
* ```php
* $config = Config::load();
* $adapter = PlatformAdapterFactory::create($config);
* $repos = $adapter->listOrgRepos('mokoconsulting-tech');
* ```
*
* @package MokoPlatform\Enterprise
* @version 04.06.10
*
* @since 04.00.00
*/
class PlatformAdapterFactory
{
/**
* Create a GitPlatformAdapter based on configuration.
*
* @param Config $config Configuration instance
* @param string|null $platformOverride Force a specific platform ('github' or 'mokogit')
* @return GitPlatformAdapter The constructed adapter
* @throws RuntimeException If the platform is not supported or token is missing
*/
public static function create(Config $config, ?string $platformOverride = null): GitPlatformAdapter
{
$platform = $platformOverride ?? $config->getString('platform', 'mokogit');
return match ($platform) {
'github' => self::createGitHubAdapter($config),
'mokogit' => self::createMokoGitAdapter($config),
default => throw new RuntimeException("Unsupported git platform: {$platform}. Use 'github' or 'mokogit'."),
};
}
/**
* Create a GitHubAdapter with configured ApiClient.
*
* @param Config $config Configuration instance
* @return GitHubAdapter Configured GitHub adapter
* @throws RuntimeException If GitHub token is not available
*/
private static function createGitHubAdapter(Config $config): GitHubAdapter
{
$token = $config->getString('github.token', '');
if (empty($token)) {
throw new RuntimeException(
'GitHub token not found. Set GH_TOKEN, GITHUB_TOKEN, or authenticate with `gh auth login`.'
);
}
$apiClient = new ApiClient(
baseUrl: 'https://git.mokoconsulting.tech/api/v1',
authToken: $token,
maxRequestsPerHour: $config->getInt('github.rate_limit', 5000),
maxRetries: $config->getInt('github.max_retries', 3),
authScheme: 'Bearer'
);
return new GitHubAdapter($apiClient);
}
/**
* Create a MokoGitAdapter with configured ApiClient.
*
* @param Config $config Configuration instance
* @return MokoGitAdapter Configured MokoGit adapter
* @throws RuntimeException If MokoGit token is not available
*/
private static function createMokoGitAdapter(Config $config): MokoGitAdapter
{
$token = $config->getString('mokogit.token', '');
if (empty($token)) {
throw new RuntimeException(
'MokoGit token not found. Set GA_TOKEN environment variable.'
);
}
$mokogitUrl = $config->getString('mokogit.url', 'https://git.mokoconsulting.tech');
$apiBaseUrl = rtrim($mokogitUrl, '/') . '/api/v1';
$apiClient = new ApiClient(
baseUrl: $apiBaseUrl,
authToken: $token,
maxRequestsPerHour: $config->getInt('mokogit.rate_limit', 5000),
maxRetries: $config->getInt('mokogit.max_retries', 3),
authScheme: 'token'
);
return new MokoGitAdapter($apiClient, $apiBaseUrl);
}
/**
* Create adapters for both platforms (useful during migration).
*
* @param Config $config Configuration instance
* @return array{github: GitHubAdapter, mokogit: MokoGitAdapter} Both adapters
* @throws RuntimeException If either token is missing
*/
public static function createBoth(Config $config): array
{
return [
'github' => self::createGitHubAdapter($config),
'mokogit' => self::createMokoGitAdapter($config),
];
}
/**
* Sync a file between MokoGit (primary) and GitHub (mirror) for a given repo.
*
* Reads the file from MokoGit and pushes it to GitHub, ensuring both platforms
* serve identical content.
*/
public static function syncUpdatesBetweenPlatforms(
Config $config,
string $repo,
string $branch = 'main',
string $filePath = 'updates.xml'
): bool {
$adapters = self::createBoth($config);
$mokogitOrg = $config->getString('mokogit.organization', 'mokoconsulting-tech');
$githubOrg = $config->getString('github.organization', 'mokoconsulting-tech');
// Read from MokoGit (primary)
try {
$mokogitFile = $adapters['mokogit']->getFileContents($mokogitOrg, $repo, $filePath, $branch);
} catch (\Exception $e) {
throw new RuntimeException("Failed to read {$filePath} from MokoGit ({$mokogitOrg}/{$repo}): " . $e->getMessage());
}
$mokogitContent = base64_decode($mokogitFile['content'] ?? '');
if (empty($mokogitContent)) {
return false;
}
// Read from GitHub (mirror) to check if update is needed
$githubSha = null;
try {
$githubFile = $adapters['github']->getFileContents($githubOrg, $repo, $filePath, $branch);
$githubContent = base64_decode($githubFile['content'] ?? '');
$githubSha = $githubFile['sha'] ?? null;
if ($githubContent === $mokogitContent) {
return true;
}
} catch (\Exception $e) {
$adapters['github']->getApiClient()->resetCircuitBreaker();
}
$adapters['github']->createOrUpdateFile(
$githubOrg,
$repo,
$filePath,
$mokogitContent,
"chore(sync): sync {$filePath} from MokoGit primary",
$githubSha,
$branch
);
return true;
}
}