/** * Platform detection for GitHub vs Forgejo environments. * * Detection priority: * 1. PLATFORM env var (set by action.yml detect step or explicit input) * 2. FORGEJO env var (set by Forgejo runner v7.0+) * 3. GITHUB_SERVER_URL heuristic (non-github.com = Forgejo) * 4. Default to GitHub * * The result is memoized after first detection — platform doesn't change * at runtime and this avoids redundant env reads across 12+ call sites. */ export type Platform = "github" | "forgejo"; let _cachedPlatform: Platform | undefined; export function detectPlatform(): Platform { if (_cachedPlatform !== undefined) return _cachedPlatform; let result: Platform = "github"; // 1. Explicit platform from action.yml detect step or user input const platformEnv = process.env.PLATFORM; if (platformEnv === "forgejo") { result = "forgejo"; } else if (platformEnv === "github") { result = "github"; } // 2. FORGEJO env var (set by Forgejo runner v7.0+) else if (process.env.FORGEJO === "true") { result = "forgejo"; } // 3. GITHUB_SERVER_URL heuristic — if not github.com, likely Forgejo/Gitea else { const serverUrl = process.env.GITHUB_SERVER_URL || ""; if (serverUrl && !serverUrl.includes("github.com")) { result = "forgejo"; } } _cachedPlatform = result; return result; } /** * Reset the memoized platform detection result. * Only needed in tests that change platform-related env vars between cases. */ export function _resetPlatformCache(): void { _cachedPlatform = undefined; }