|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { logger } from './logger'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Extracts the hostname from GITHUB_SERVER_URL to set GH_HOST for gh CLI. |
| 6 | + * Returns the hostname if GITHUB_SERVER_URL points to a non-mygithub.libinneed.workers.dev instance, |
| 7 | + * or null if it points to github.com (no GH_HOST needed). |
| 8 | + * @param serverUrl - The GITHUB_SERVER_URL environment variable value |
| 9 | + * @returns The hostname to use for GH_HOST, or null if not needed |
| 10 | + * @internal Exported for testing |
| 11 | + */ |
| 12 | +export function extractGhHostFromServerUrl(serverUrl: string | undefined): string | null { |
| 13 | + if (!serverUrl) { |
| 14 | + return null; |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + const url = new URL(serverUrl); |
| 19 | + const hostname = url.hostname; |
| 20 | + |
| 21 | + // If pointing to public GitHub, no GH_HOST needed |
| 22 | + if (hostname === 'github.com') { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + // For GHES/GHEC instances, return the hostname |
| 27 | + return hostname; |
| 28 | + } catch { |
| 29 | + // Invalid URL, return null |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Reads path entries from the $GITHUB_PATH file used by GitHub Actions. |
| 36 | + * |
| 37 | + * When setup-* actions (e.g., setup-ruby, setup-dart, setup-python) run before AWF, |
| 38 | + * they add tool paths to the $GITHUB_PATH file. The Actions runner prepends these |
| 39 | + * to $PATH for subsequent steps, but if `sudo` resets PATH (depending on sudoers |
| 40 | + * configuration), those entries may be lost by the time AWF reads process.env.PATH. |
| 41 | + * |
| 42 | + * This function reads the $GITHUB_PATH file directly and returns any path entries |
| 43 | + * found, so they can be merged into AWF_HOST_PATH regardless of sudo behavior. |
| 44 | + * |
| 45 | + * @returns Array of path entries from the $GITHUB_PATH file, or empty array if unavailable |
| 46 | + * @internal Exported for testing |
| 47 | + */ |
| 48 | +export function readGitHubPathEntries(): string[] { |
| 49 | + const githubPathFile = process.env.GITHUB_PATH; |
| 50 | + if (!githubPathFile) { |
| 51 | + logger.debug('GITHUB_PATH env var is not set; skipping $GITHUB_PATH file merge (tools installed by setup-* actions may be missing from PATH if sudo reset it)'); |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + const content = fs.readFileSync(githubPathFile, 'utf-8'); |
| 57 | + return content |
| 58 | + .split('\n') |
| 59 | + .map(line => line.trim()) |
| 60 | + .filter(line => line.length > 0); |
| 61 | + } catch { |
| 62 | + // File doesn't exist or isn't readable — expected outside GitHub Actions |
| 63 | + logger.debug(`GITHUB_PATH file at '${githubPathFile}' could not be read; skipping file merge`); |
| 64 | + return []; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Reads key-value environment entries from the $GITHUB_ENV file. |
| 70 | + * |
| 71 | + * The Actions runner writes to this file when steps call `core.exportVariable()`. |
| 72 | + * When AWF runs via `sudo`, non-standard env vars may be stripped. This function |
| 73 | + * reads the file directly to recover them. |
| 74 | + * |
| 75 | + * Supports both formats used by the Actions runner: |
| 76 | + * - Simple: `KEY=VALUE` (value may contain `=`) |
| 77 | + * - Heredoc: `KEY<<DELIMITER\nVALUE_LINES\nDELIMITER` |
| 78 | + * |
| 79 | + * @returns Map of environment variable names to values |
| 80 | + * @internal Exported for testing |
| 81 | + */ |
| 82 | +export function readGitHubEnvEntries(): Record<string, string> { |
| 83 | + const githubEnvFile = process.env.GITHUB_ENV; |
| 84 | + if (!githubEnvFile) { |
| 85 | + logger.debug('GITHUB_ENV env var is not set; skipping $GITHUB_ENV file read'); |
| 86 | + return {}; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + const content = fs.readFileSync(githubEnvFile, 'utf-8'); |
| 91 | + return parseGitHubEnvFile(content); |
| 92 | + } catch { |
| 93 | + logger.debug(`GITHUB_ENV file at '${githubEnvFile}' could not be read; skipping`); |
| 94 | + return {}; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Parses the content of a $GITHUB_ENV file into key-value pairs. |
| 100 | + * @internal Exported for testing |
| 101 | + */ |
| 102 | +export function parseGitHubEnvFile(content: string): Record<string, string> { |
| 103 | + const result: Record<string, string> = {}; |
| 104 | + // Normalize CRLF to LF |
| 105 | + const lines = content.replace(/\r\n/g, '\n').split('\n'); |
| 106 | + let i = 0; |
| 107 | + |
| 108 | + while (i < lines.length) { |
| 109 | + const line = lines[i]; |
| 110 | + |
| 111 | + // Skip empty lines |
| 112 | + if (line.trim() === '') { |
| 113 | + i++; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + // Check for heredoc format: KEY<<DELIMITER |
| 118 | + const heredocMatch = line.match(/^([^=]+)<<(.+)$/); |
| 119 | + if (heredocMatch) { |
| 120 | + const key = heredocMatch[1]; |
| 121 | + const delimiter = heredocMatch[2]; |
| 122 | + const valueLines: string[] = []; |
| 123 | + i++; |
| 124 | + |
| 125 | + // Collect lines until we find the delimiter |
| 126 | + while (i < lines.length && lines[i] !== delimiter) { |
| 127 | + valueLines.push(lines[i]); |
| 128 | + i++; |
| 129 | + } |
| 130 | + // Skip the closing delimiter line |
| 131 | + if (i < lines.length) i++; |
| 132 | + |
| 133 | + result[key] = valueLines.join('\n'); |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + // Simple format: KEY=VALUE (split on first = only) |
| 138 | + const eqIdx = line.indexOf('='); |
| 139 | + if (eqIdx > 0) { |
| 140 | + const key = line.slice(0, eqIdx); |
| 141 | + const value = line.slice(eqIdx + 1); |
| 142 | + result[key] = value; |
| 143 | + } |
| 144 | + |
| 145 | + i++; |
| 146 | + } |
| 147 | + |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Toolchain environment variables that should be recovered from $GITHUB_ENV |
| 153 | + * when sudo strips them from process.env. These are set by setup-* actions |
| 154 | + * (setup-go, setup-java, setup-dotnet, etc.) and are needed for correct |
| 155 | + * tool resolution inside the agent container. |
| 156 | + */ |
| 157 | +export const TOOLCHAIN_ENV_VARS = [ |
| 158 | + 'GOROOT', |
| 159 | + 'CARGO_HOME', |
| 160 | + 'RUSTUP_HOME', |
| 161 | + 'JAVA_HOME', |
| 162 | + 'DOTNET_ROOT', |
| 163 | + 'BUN_INSTALL', |
| 164 | +] as const; |
| 165 | + |
| 166 | +/** |
| 167 | + * Merges path entries from the $GITHUB_PATH file into a PATH string. |
| 168 | + * Entries from $GITHUB_PATH are prepended (they have higher priority, matching |
| 169 | + * how the Actions runner processes them). Duplicate entries are removed. |
| 170 | + * |
| 171 | + * @param currentPath - The current PATH string (e.g., from process.env.PATH) |
| 172 | + * @param githubPathEntries - Path entries read from the $GITHUB_PATH file |
| 173 | + * @returns Merged PATH string with $GITHUB_PATH entries prepended |
| 174 | + * @internal Exported for testing |
| 175 | + */ |
| 176 | +export function mergeGitHubPathEntries(currentPath: string, githubPathEntries: string[]): string { |
| 177 | + if (githubPathEntries.length === 0) { |
| 178 | + return currentPath; |
| 179 | + } |
| 180 | + |
| 181 | + const currentEntries = currentPath ? currentPath.split(':') : []; |
| 182 | + const currentSet = new Set(currentEntries); |
| 183 | + |
| 184 | + // Only add entries that aren't already in the current PATH |
| 185 | + const newEntries = githubPathEntries.filter(entry => !currentSet.has(entry)); |
| 186 | + |
| 187 | + if (newEntries.length === 0) { |
| 188 | + return currentPath; |
| 189 | + } |
| 190 | + |
| 191 | + // Prepend new entries (setup-* actions expect their paths to have priority) |
| 192 | + return [...newEntries, ...currentEntries].join(':'); |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * Reads environment variables from a KEY=VALUE file (like Docker's --env-file). |
| 197 | + * |
| 198 | + * Rules: |
| 199 | + * - Lines starting with '#' are comments and are ignored. |
| 200 | + * - Empty/whitespace-only lines are ignored. |
| 201 | + * - Each non-comment line must match the pattern KEY=VALUE where KEY starts with a |
| 202 | + * letter or underscore and contains only letters, digits, or underscores. |
| 203 | + * - Values may be empty (KEY=). |
| 204 | + * - Values are taken literally; no quote-stripping or variable expansion is done. |
| 205 | + * |
| 206 | + * @param filePath - Absolute or relative path to the env file |
| 207 | + * @returns An object mapping variable names to their values |
| 208 | + * @throws {Error} If the file cannot be read |
| 209 | + */ |
| 210 | +export function readEnvFile(filePath: string): Record<string, string> { |
| 211 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 212 | + const result: Record<string, string> = {}; |
| 213 | + for (const raw of content.split('\n')) { |
| 214 | + const line = raw.trim(); |
| 215 | + // Skip comments and blank lines |
| 216 | + if (line === '' || line.startsWith('#')) continue; |
| 217 | + const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/); |
| 218 | + if (match) { |
| 219 | + result[match[1]] = match[2]; |
| 220 | + } |
| 221 | + } |
| 222 | + return result; |
| 223 | +} |
0 commit comments