| 1 | import { execSync } from "child_process"; |
| 2 | |
| 3 | /** |
| 4 | * Get the git remote URL for the current repository |
| 5 | * @param remote The remote name (defaults to 'origin') |
| 6 | * @returns The remote URL or null if not found |
| 7 | */ |
| 8 | export function getGitRemoteUrl(remote: string = "origin"): string | null { |
| 9 | try { |
| 10 | const result = execSync(`git remote get-url ${remote}`, { |
| 11 | encoding: "utf-8", |
| 12 | cwd: process.cwd(), |
| 13 | stdio: "pipe", |
| 14 | }); |
| 15 | return result.trim(); |
| 16 | } catch { |
| 17 | return null; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the current git branch name |
| 23 | * @returns The current branch name or null if not found |
| 24 | */ |
| 25 | export function getGitBranch(): string | null { |
| 26 | try { |
| 27 | const result = execSync("git branch --show-current", { |
| 28 | encoding: "utf-8", |
| 29 | cwd: process.cwd(), |
| 30 | stdio: "pipe", |
| 31 | }); |
| 32 | return result.trim() || null; |
| 33 | } catch { |
| 34 | return null; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Check if current directory is a git repository |
| 40 | */ |
| 41 | export function isGitRepo(): boolean { |
| 42 | try { |
| 43 | execSync("git rev-parse --is-inside-work-tree", { |
| 44 | stdio: "ignore", |
| 45 | cwd: process.cwd(), |
| 46 | }); |
| 47 | return true; |
| 48 | } catch { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Check if running in GitHub Actions |
| 55 | */ |
| 56 | export function isGitHubActions(): boolean { |
| 57 | return process.env.GITHUB_ACTIONS === "true"; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Check if running in Continue remote agents |
| 62 | */ |
| 63 | export function isContinueRemoteAgent(): boolean { |
| 64 | return process.env.CONTINUE_REMOTE === "true"; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get repository URL from GitHub Actions environment variables |
| 69 | * @returns GitHub repository URL or null if not available |
| 70 | */ |
| 71 | export function getGitHubActionsRepoUrl(): string | null { |
| 72 | if (!isGitHubActions()) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | // GITHUB_REPOSITORY is in format "owner/repo" |
| 77 | const githubRepository = process.env.GITHUB_REPOSITORY; |
| 78 | if (!githubRepository) { |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | // GITHUB_SERVER_URL defaults to https://github.com but can be different for GitHub Enterprise |
| 83 | const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com"; |
| 84 | |
| 85 | return `${serverUrl}/${githubRepository}`; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the current repository URL with enhanced detection for CI environments |
| 90 | * Priority order: |
| 91 | * 1. GitHub Actions environment variables (if running in GitHub Actions) |
| 92 | * 2. Git remote URL (if in a git repository) |
| 93 | * 3. Current working directory (fallback) |
| 94 | * @returns Repository URL or local path |
| 95 | */ |
| 96 | export function getRepoUrl(): string { |
| 97 | // First, check if we're in GitHub Actions and can get repo URL from env vars |
| 98 | const githubActionsUrl = getGitHubActionsRepoUrl(); |
| 99 | if (githubActionsUrl) { |
| 100 | return githubActionsUrl; |
| 101 | } |
| 102 | |
| 103 | // Then check if we're in a git repository |
| 104 | if (!isGitRepo()) { |
| 105 | return process.cwd(); |
| 106 | } |
| 107 | |
| 108 | // Try to get the remote URL from git |
| 109 | const remoteUrl = getGitRemoteUrl(); |
| 110 | const url = remoteUrl || process.cwd(); |
| 111 | return url.endsWith(".git") ? url.slice(0, -4) : url; |
| 112 | } |