Elffuss BraidHub

kikocis/continue public

e42bdc2c5fca · 2.3 KiBRawPermalink
1import { 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 */
8export 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 * Check if current directory is a git repository
23 */
24export function isGitRepo(): boolean {
25 try {
26 execSync("git rev-parse --is-inside-work-tree", {
27 stdio: "ignore",
28 cwd: process.cwd(),
29 });
30 return true;
31 } catch {
32 return false;
33 }
34}
35
36/**
37 * Check if running in GitHub Actions
38 */
39export function isGitHubActions(): boolean {
40 return process.env.GITHUB_ACTIONS === "true";
41}
42
43/**
44 * Get repository URL from GitHub Actions environment variables
45 * @returns GitHub repository URL or null if not available
46 */
47export function getGitHubActionsRepoUrl(): string | null {
48 if (!isGitHubActions()) {
49 return null;
50 }
51
52 // GITHUB_REPOSITORY is in format "owner/repo"
53 const githubRepository = process.env.GITHUB_REPOSITORY;
54 if (!githubRepository) {
55 return null;
56 }
57
58 // GITHUB_SERVER_URL defaults to https://github.com but can be different for GitHub Enterprise
59 const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com";
60
61 return `${serverUrl}/${githubRepository}`;
62}
63
64/**
65 * Get the current repository URL with enhanced detection for CI environments
66 * Priority order:
67 * 1. GitHub Actions environment variables (if running in GitHub Actions)
68 * 2. Git remote URL (if in a git repository)
69 * 3. Current working directory (fallback)
70 * @returns Repository URL or local path
71 */
72export function getRepoUrl(): string {
73 // First, check if we're in GitHub Actions and can get repo URL from env vars
74 const githubActionsUrl = getGitHubActionsRepoUrl();
75 if (githubActionsUrl) {
76 return githubActionsUrl;
77 }
78
79 // Then check if we're in a git repository
80 if (!isGitRepo()) {
81 return process.cwd();
82 }
83
84 // Try to get the remote URL from git
85 const remoteUrl = getGitRemoteUrl();
86 const url = remoteUrl || process.cwd();
87 return url.endsWith(".git") ? url.slice(0, -4) : url;
88}