Files
claude-code-action/src/platform/detector.ts
T
Vlad MocanuandClaude Opus 4.6 ac58fb6c14 fix(prd-7): M1 architecture review — consolidate duplication, fix bugs, memoize detection
- F1: Consolidated updateForgejoCommentBody into unified updateCommentBody (~150 lines removed)
- F2: Fixed wrong run identifier in update-with-branch.ts (context.runId → getForgejoRunIndex())
- F3: Memoized detectPlatform() with _resetPlatformCache() for tests
- F4: Added getServerUrl() runtime helper to resolve constant-vs-function inconsistency
- F5: Fixed sticky comment bot matching on Forgejo via context.inputs.botName
- F6: Documented GHES heuristic misidentification in docs/forgejo.md
- Updated PRD M1 checkbox to complete

Progress: 17% complete — M1 done, M2 (error handling review) next

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 12:09:02 +02:00

53 lines
1.5 KiB
TypeScript

/**
* 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;
}