import type { LogLevel } from './config' // The host the plugins use when their config names none. export const DEFAULT_PLUGIN_HOST = 'https://us.i.posthog.com' // PostHog Cloud logins and CI OIDC tokens are for the app host, while the plugins default to the // ingestion host the SDK sends events to. A personal API key works with either, so a build with one // keeps the host it names, which an egress allowlist may already admit. const CLOUD_APP_HOSTS: Record = { 'us.i.posthog.com': 'https://us.posthog.com', 'eu.i.posthog.com': 'https://eu.posthog.com', } // hog variables, and the posthog-cli variables they replace. The credentials, host and project are // left out: the plugin config always overrode those, so they never reached posthog-cli through a // plugin, and a key mapped from the environment would win over the CI job's OIDC token. The // dotenv file is left out because it holds posthog-cli's names, which hog doesn't read, and hog // stops when the file it names has no key. const LEGACY_ENV_VARS = { HOG_DRY_RUN: 'POSTHOG_CLI_DRY_RUN', HOG_RATE_LIMIT: 'POSTHOG_CLIENT_RATE_LIMIT', HOG_UPLOAD_CONCURRENCY: 'POSTHOG_CLI_SOURCEMAP_UPLOAD_CONCURRENCY', HOG_RELEASE_MODE: 'POSTHOG_RELEASE_MODE', HOG_HOME: 'POSTHOG_HOME', } as const type HogEnvVar = keyof typeof LEGACY_ENV_VARS export const MIGRATION_GUIDE = 'https://posthog.com/docs/cli/migrating' // One build calls hog several times, so each variable is warned about once per process. const warnedVars = new Set() // The CI providers whose OIDC token hog trades for a PostHog token, detected the way hog detects them. const OIDC_CI_PROVIDERS: { name: string; detect: (env: NodeJS.ProcessEnv) => boolean }[] = [ { name: 'Depot CI', detect: (env) => !!env.DEPOT_JOB_URL }, { name: 'GitHub Actions', detect: (env) => env.GITHUB_ACTIONS === 'true' }, { name: 'GitLab.com', detect: (env) => env.GITLAB_CI === 'true' && env.CI_SERVER_URL === 'https://gitlab.com' }, ] let suggestedOidc = false /** Points a CI build that uploads with a personal API key at the job's OIDC token, once per process. */ function suggestOidc(hogEnv: NodeJS.ProcessEnv): void { if (suggestedOidc || !hogEnv.HOG_PERSONAL_API_KEY) { return } const provider = OIDC_CI_PROVIDERS.find(({ detect }) => detect(hogEnv)) if (!provider) { return } suggestedOidc = true process.stderr.write( `[PostHog] This build runs in ${provider.name}, which can upload with the job's OIDC token instead of a personal API key. Run \`hog ci setup --scope error_tracking:write\` once in the repository, then remove the key. Migration guide: ${MIGRATION_GUIDE}\n` ) } /** * Reads a hog variable, falling back to the posthog-cli variable it replaces, with a warning to * rename it. The hog variable wins when both are set. */ export function readHogEnvVar(name: HogEnvVar, env: NodeJS.ProcessEnv = process.env): string | undefined { const legacy = LEGACY_ENV_VARS[name] if (env[legacy] && !warnedVars.has(name)) { warnedVars.add(name) process.stderr.write( `[PostHog] ${legacy} is a posthog-cli variable. The plugins now run hog, PostHog's new CLI, and read it as ${name} for you, but the next major release will stop reading it. Rename it to ${name}. Migration guide: ${MIGRATION_GUIDE}\n` ) } return env[name] || env[legacy] } /** * Maps the host the plugin was given to the one hog should use: the app host for PostHog Cloud, * and any other host unchanged. */ export function toHogHost(host: string): string { let hostname: string try { hostname = new URL(host).host } catch { return host } return Object.hasOwn(CLOUD_APP_HOSTS, hostname) ? CLOUD_APP_HOSTS[hostname] : host } export interface HogEnvOptions { host: string projectId?: string personalApiKey?: string logLevel?: LogLevel } /** * Builds the environment hog runs in. Plugin settings override inherited values, except an * empty personal API key, which leaves hog to find credentials itself: the CI job's OIDC token * after `hog ci setup`, `HOG_PERSONAL_API_KEY`, or a `hog auth login` on the machine. */ export function buildHogEnv(options: HogEnvOptions, env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv { const hogEnv: NodeJS.ProcessEnv = { ...env } for (const name of Object.keys(LEGACY_ENV_VARS) as HogEnvVar[]) { const value = readHogEnvVar(name, env) if (value) { hogEnv[name] = value } } // hog prints JSON when stdout is a pipe or an agent runs the build, and HOG_RAW prints the whole result as JSON. // Text keeps build logs readable, and makes `release` print only the id the plugins read. hogEnv.HOG_OUTPUT = 'text' delete hogEnv.HOG_RAW // A host the config names wins. The default yields to an inherited HOG_HOST, so a key or login // for another region isn't sent to US Cloud. if (options.host === DEFAULT_PLUGIN_HOST && env.HOG_HOST) { hogEnv.HOG_HOST = env.HOG_HOST } else { hogEnv.HOG_HOST = options.personalApiKey ? options.host : toHogHost(options.host) } if (options.projectId) { hogEnv.HOG_PROJECT_ID = options.projectId } if (options.personalApiKey) { hogEnv.HOG_PERSONAL_API_KEY = options.personalApiKey } if (options.logLevel === 'debug') { hogEnv.HOG_DEBUG = '1' } suggestOidc(hogEnv) return hogEnv }