< Summary - Envilder CLI

Information
Class: src/envilder/core/infrastructure/github/GitHubEnvFileExporter.ts
Assembly: Default
File(s): src/envilder/core/infrastructure/github/GitHubEnvFileExporter.ts
Tag: 605_44106794472
Line coverage
88%
Covered lines: 16
Uncovered lines: 2
Coverable lines: 18
Total lines: 117
Line coverage: 88.8%
Branch coverage
84%
Covered branches: 11
Total branches: 13
Branch coverage: 84.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/infrastructure/github/GitHubEnvFileExporter.ts

#LineLine coverage
 1import { randomUUID } from 'node:crypto';
 2import { appendFile } from 'node:fs/promises';
 3import { EOL } from 'node:os';
 4import { InvalidArgumentError } from '../../domain/errors/DomainErrors.js';
 5import type { IEnvironmentExporter } from '../../domain/ports/IEnvironmentExporter.js';
 6
 7/**
 8 * Names the GitHub runner refuses to set from `GITHUB_ENV`: `NODE_OPTIONS`
 9 * and the documented default variables, which cannot be overwritten. Only
 10 * exact names: other `GITHUB_*` and `RUNNER_*` names export normally. `CI`
 11 * is left out because GitHub still lets it be overwritten.
 12 * https://docs.github.com/en/actions/reference/workflows-and-actions/variables
 13 */
 414const RUNNER_BLOCKED_NAMES = new Set([
 15  'NODE_OPTIONS',
 16  'GITHUB_ACTION',
 17  'GITHUB_ACTION_PATH',
 18  'GITHUB_ACTION_REPOSITORY',
 19  'GITHUB_ACTIONS',
 20  'GITHUB_ACTOR',
 21  'GITHUB_ACTOR_ID',
 22  'GITHUB_API_URL',
 23  'GITHUB_ARTIFACTS',
 24  'GITHUB_ARTIFACTS_LIST',
 25  'GITHUB_BASE_REF',
 26  'GITHUB_ENV',
 27  'GITHUB_EVENT_NAME',
 28  'GITHUB_EVENT_PATH',
 29  'GITHUB_GRAPHQL_URL',
 30  'GITHUB_HEAD_REF',
 31  'GITHUB_JOB',
 32  'GITHUB_OUTPUT',
 33  'GITHUB_PATH',
 34  'GITHUB_REF',
 35  'GITHUB_REF_NAME',
 36  'GITHUB_REF_PROTECTED',
 37  'GITHUB_REF_TYPE',
 38  'GITHUB_REPOSITORY',
 39  'GITHUB_REPOSITORY_ID',
 40  'GITHUB_REPOSITORY_OWNER',
 41  'GITHUB_REPOSITORY_OWNER_ID',
 42  'GITHUB_RETENTION_DAYS',
 43  'GITHUB_RUN_ATTEMPT',
 44  'GITHUB_RUN_ID',
 45  'GITHUB_RUN_NUMBER',
 46  'GITHUB_SERVER_URL',
 47  'GITHUB_SHA',
 48  'GITHUB_STEP_SUMMARY',
 49  'GITHUB_TRIGGERING_ACTOR',
 50  'GITHUB_WORKFLOW',
 51  'GITHUB_WORKFLOW_REF',
 52  'GITHUB_WORKFLOW_SHA',
 53  'GITHUB_WORKSPACE',
 54  'RUNNER_ARCH',
 55  'RUNNER_DEBUG',
 56  'RUNNER_ENVIRONMENT',
 57  'RUNNER_NAME',
 58  'RUNNER_OS',
 59  'RUNNER_TEMP',
 60  'RUNNER_TOOL_CACHE',
 61]);
 62
 63/**
 64 * Exports variables to the later steps of a GitHub Actions job by appending
 65 * them to the file named by `GITHUB_ENV`, which the runner reads when the
 66 * current step ends.
 67 *
 68 * Every variable uses the multiline `NAME<<DELIMITER` form with a random
 69 * delimiter, so a value containing newlines cannot close its own block and
 70 * declare extra variables. All variables are appended in a single write.
 71 */
 72export class GitHubEnvFileExporter implements IEnvironmentExporter {
 73  /**
 74   * @param options.filePath - File to append to (default: `GITHUB_ENV`,
 75   * read when exporting).
 76   */
 3477  constructor(private readonly options: { filePath?: string } = {}) {}
 78
 79  async export(variables: Readonly<Record<string, string>>): Promise<void> {
 880    const filePath = this.options.filePath ?? process.env.GITHUB_ENV;
 881    if (!filePath) {
 182      throw new InvalidArgumentError(
 83        'GITHUB_ENV is not set: exporting secrets to the job environment only works inside a GitHub Actions job. Set env
 84      );
 85    }
 86
 87    // The runner skips these names but applies the rest, so the step would
 88    // report a variable later steps never get. Compared case-insensitively:
 89    // on Windows runners `node_options` is the same variable.
 790    const blocked = Object.keys(variables).filter((name) =>
 1191      RUNNER_BLOCKED_NAMES.has(name.toUpperCase()),
 92    );
 793    if (blocked.length > 0) {
 294      throw new InvalidArgumentError(
 95        `${blocked.join(', ')} cannot be exported to the job environment: GitHub Actions does not let GITHUB_ENV set NOD
 96      );
 97    }
 98
 599    const blocks = Object.entries(variables).map(([name, value]) =>
 6100      GitHubEnvFileExporter.block(name, value),
 101    );
 5102    if (blocks.length === 0) {
 0103      return;
 104    }
 5105    await appendFile(filePath, blocks.join(''), { encoding: 'utf8' });
 106  }
 107
 108  private static block(name: string, value: string): string {
 6109    const delimiter = `ghadelimiter_${randomUUID()}`;
 6110    if (name.includes(delimiter) || value.includes(delimiter)) {
 0111      throw new InvalidArgumentError(
 112        `Unexpected delimiter collision while exporting ${name}.`,
 113      );
 114    }
 6115    return `${name}<<${delimiter}${EOL}${value}${EOL}${delimiter}${EOL}`;
 116  }
 117}