From 70763e229db1d28afd161275a0d119619ce00746 Mon Sep 17 00:00:00 2001 From: LironEr Date: Fri, 18 Oct 2024 12:07:10 +0200 Subject: [PATCH 1/2] feat: add raw CI env vars to log --- .../main/utils/__tests__/configUtils.spec.ts | 3 +- packages/bundlemon/lib/main/utils/ci/index.ts | 36 ++++++++++++----- .../lib/main/utils/ci/providers/circleCI.ts | 39 ++++++++++++------- .../lib/main/utils/ci/providers/github.ts | 29 ++++++++++---- .../lib/main/utils/ci/providers/travis.ts | 27 +++++++++---- packages/bundlemon/lib/main/utils/ci/types.ts | 1 + packages/bundlemon/lib/main/utils/utils.ts | 14 +++++++ 7 files changed, 108 insertions(+), 41 deletions(-) diff --git a/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts b/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts index 9b437ff..84df657 100644 --- a/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts +++ b/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts @@ -2,7 +2,7 @@ import { when } from 'jest-when'; import { CreateCommitRecordAuthType, EnvVar } from '../../../common/consts'; import { validateConfig, getCreateCommitRecordAuthParams, getProjectId } from '../configUtils'; -import { getEnvVar } from '../../utils/utils'; +import { getEnvVar, envVarsListToObject } from '../../utils/utils'; import { generateRandomString } from './configUtils'; import { getOrCreateProjectId } from '../../../common/service'; import type { CIEnvVars } from '../ci/types'; @@ -16,6 +16,7 @@ import { ProjectProvider } from 'bundlemon-utils'; jest.mock('../../utils/utils', () => ({ __esModule: true, getEnvVar: jest.fn(), + envVarsListToObject: jest.fn().mockReturnValue({}), })); jest.mock('../../../common/service'); diff --git a/packages/bundlemon/lib/main/utils/ci/index.ts b/packages/bundlemon/lib/main/utils/ci/index.ts index 24e9645..c2734a8 100644 --- a/packages/bundlemon/lib/main/utils/ci/index.ts +++ b/packages/bundlemon/lib/main/utils/ci/index.ts @@ -1,21 +1,32 @@ import providers from './providers'; -import { getEnvVar } from '../utils'; +import { envVarsListToObject } from '../utils'; import type { CIEnvVars } from './types'; +const rawOverrideVars = envVarsListToObject([ + 'CI', + 'CI_REPO_OWNER', + 'CI_REPO_NAME', + 'CI_BRANCH', + 'CI_COMMIT_SHA', + 'CI_TARGET_BRANCH', + 'CI_PR_NUMBER', + 'CI_COMMIT_MESSAGE', +] as const); + const overrideVars: CIEnvVars = { - ci: getEnvVar('CI') === 'true', + raw: rawOverrideVars, + ci: rawOverrideVars.CI === 'true', provider: undefined, - owner: getEnvVar('CI_REPO_OWNER'), - repo: getEnvVar('CI_REPO_NAME'), - branch: getEnvVar('CI_BRANCH'), - commitSha: getEnvVar('CI_COMMIT_SHA'), - targetBranch: getEnvVar('CI_TARGET_BRANCH'), - prNumber: getEnvVar('CI_PR_NUMBER'), - commitMsg: getEnvVar('CI_COMMIT_MESSAGE'), + owner: rawOverrideVars.CI_REPO_OWNER, + repo: rawOverrideVars.CI_REPO_NAME, + branch: rawOverrideVars.CI_BRANCH, + commitSha: rawOverrideVars.CI_COMMIT_SHA, + targetBranch: rawOverrideVars.CI_TARGET_BRANCH, + prNumber: rawOverrideVars.CI_PR_NUMBER, + commitMsg: rawOverrideVars.CI_COMMIT_MESSAGE, }; const providerVars = providers.find((p) => p.isItMe)?.getVars(); - const vars = { ...overrideVars }; if (providerVars) { @@ -24,6 +35,11 @@ if (providerVars) { // @ts-expect-error bad types vars[varName] = vars[varName] ?? providerVars[varName]; }); + + vars.raw = { + ...providerVars.raw, + ...overrideVars.raw, + }; } export const getCIVars = () => { diff --git a/packages/bundlemon/lib/main/utils/ci/providers/circleCI.ts b/packages/bundlemon/lib/main/utils/ci/providers/circleCI.ts index 85e9dd0..b181b60 100644 --- a/packages/bundlemon/lib/main/utils/ci/providers/circleCI.ts +++ b/packages/bundlemon/lib/main/utils/ci/providers/circleCI.ts @@ -1,23 +1,34 @@ import type { Provider } from '../types'; -import { getEnvVar } from '../../utils'; +import { envVarsListToObject, getEnvVar } from '../../utils'; // https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables const provider: Provider = { isItMe: getEnvVar('CIRCLECI') === 'true', - getVars: () => ({ - ci: true, - provider: 'circleci', - owner: getEnvVar('CIRCLE_PROJECT_USERNAME'), - repo: getEnvVar('CIRCLE_PROJECT_REPONAME'), - branch: getEnvVar('CIRCLE_BRANCH'), - commitSha: getEnvVar('CIRCLE_SHA1'), - // target branch not available in CircleCI - // https://ideas.circleci.com/cloud-feature-requests/p/provide-env-variable-for-branch-name-targeted-by-pull-request - // use CI_TARGET_BRANCH to override - targetBranch: undefined, - prNumber: getEnvVar('CIRCLE_PULL_REQUEST')?.split('/').pop(), - }), + getVars: () => { + const raw = envVarsListToObject([ + 'CIRCLE_PROJECT_USERNAME', + 'CIRCLE_PROJECT_REPONAME', + 'CIRCLE_BRANCH', + 'CIRCLE_SHA1', + 'CIRCLE_PULL_REQUEST', + ] as const); + + return { + raw, + ci: true, + provider: 'circleci', + owner: raw.CIRCLE_PROJECT_USERNAME, + repo: raw.CIRCLE_PROJECT_REPONAME, + branch: raw.CIRCLE_BRANCH, + commitSha: raw.CIRCLE_SHA1, + // target branch not available in CircleCI + // https://ideas.circleci.com/cloud-feature-requests/p/provide-env-variable-for-branch-name-targeted-by-pull-request + // use CI_TARGET_BRANCH to override + targetBranch: undefined, + prNumber: raw.CIRCLE_PULL_REQUEST?.split('/').pop(), + }; + }, }; export default provider; diff --git a/packages/bundlemon/lib/main/utils/ci/providers/github.ts b/packages/bundlemon/lib/main/utils/ci/providers/github.ts index 93aabd5..2fcd5cc 100644 --- a/packages/bundlemon/lib/main/utils/ci/providers/github.ts +++ b/packages/bundlemon/lib/main/utils/ci/providers/github.ts @@ -1,29 +1,42 @@ import type { Provider } from '../types'; -import { getEnvVar } from '../../utils'; +import { getEnvVar, envVarsListToObject } from '../../utils'; // https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables +type GitHubEvent = undefined | '' | 'pull_request' | 'push'; + const provider: Provider = { isItMe: !!getEnvVar('GITHUB_ACTION'), getVars: () => { - const fullRepoName = getEnvVar('GITHUB_REPOSITORY'); + const raw = envVarsListToObject([ + 'GITHUB_REPOSITORY', + 'GITHUB_EVENT_NAME', + 'GITHUB_REF', + 'GITHUB_HEAD_REF', + 'GITHUB_SHA', + 'GITHUB_BASE_REF', + 'GITHUB_RUN_ID', + ] as const); + + const fullRepoName = raw.GITHUB_REPOSITORY; const [owner, repo] = fullRepoName?.split('/') ?? [undefined, undefined]; - const event = getEnvVar('GITHUB_EVENT_NAME') as undefined | '' | 'pull_request' | 'push'; + const event = raw.GITHUB_EVENT_NAME as GitHubEvent; const isPr = event === 'pull_request'; - const ref = getEnvVar('GITHUB_REF')?.split('/'); + const ref = raw.GITHUB_REF?.split('/'); return { + raw, ci: true, provider: 'github', owner, repo, - branch: isPr ? getEnvVar('GITHUB_HEAD_REF') : ref?.slice(2).join('/'), - commitSha: getEnvVar('GITHUB_SHA'), - targetBranch: getEnvVar('GITHUB_BASE_REF'), + branch: isPr ? raw.GITHUB_HEAD_REF : ref?.slice(2).join('/'), + commitSha: raw.GITHUB_SHA, + targetBranch: raw.GITHUB_BASE_REF, prNumber: isPr ? ref?.[2] : undefined, - buildId: getEnvVar('GITHUB_RUN_ID'), + buildId: raw.GITHUB_RUN_ID, }; }, }; diff --git a/packages/bundlemon/lib/main/utils/ci/providers/travis.ts b/packages/bundlemon/lib/main/utils/ci/providers/travis.ts index 8e0cb3e..85ba41f 100644 --- a/packages/bundlemon/lib/main/utils/ci/providers/travis.ts +++ b/packages/bundlemon/lib/main/utils/ci/providers/travis.ts @@ -1,27 +1,38 @@ import type { Provider } from '../types'; -import { getEnvVar } from '../../utils'; +import { envVarsListToObject, getEnvVar } from '../../utils'; // https://docs.travis-ci.com/user/environment-variables#default-environment-variables const provider: Provider = { isItMe: getEnvVar('TRAVIS') === 'true', getVars: () => { - const fullRepoName = getEnvVar('TRAVIS_REPO_SLUG'); + const raw = envVarsListToObject([ + 'TRAVIS_REPO_SLUG', + 'TRAVIS_EVENT_TYPE', + 'TRAVIS_PULL_REQUEST', + 'TRAVIS_BRANCH', + 'TRAVIS_PULL_REQUEST_BRANCH', + 'TRAVIS_COMMIT', + 'TRAVIS_COMMIT_MESSAGE', + ] as const); + + const fullRepoName = raw.TRAVIS_REPO_SLUG; const [owner, repo] = fullRepoName?.split('/') ?? [undefined, undefined]; - const isPushEvent = getEnvVar('TRAVIS_EVENT_TYPE') === 'push'; - const prNumber = getEnvVar('TRAVIS_PULL_REQUEST'); + const isPushEvent = raw.TRAVIS_EVENT_TYPE === 'push'; + const prNumber = raw.TRAVIS_PULL_REQUEST; return { + raw, ci: true, provider: 'travis', owner, repo, - branch: isPushEvent ? getEnvVar('TRAVIS_BRANCH') : getEnvVar('TRAVIS_PULL_REQUEST_BRANCH'), - commitSha: getEnvVar('TRAVIS_COMMIT'), - targetBranch: isPushEvent ? undefined : getEnvVar('TRAVIS_BRANCH'), + branch: isPushEvent ? raw.TRAVIS_BRANCH : raw.TRAVIS_PULL_REQUEST_BRANCH, + commitSha: raw.TRAVIS_COMMIT, + targetBranch: isPushEvent ? undefined : raw.TRAVIS_BRANCH, prNumber: prNumber === 'false' ? undefined : prNumber, // "false" if it’s not a pull request, set as undefined - commitMsg: getEnvVar('TRAVIS_COMMIT_MESSAGE'), + commitMsg: raw.TRAVIS_COMMIT_MESSAGE, }; }, }; diff --git a/packages/bundlemon/lib/main/utils/ci/types.ts b/packages/bundlemon/lib/main/utils/ci/types.ts index e6b8b69..0c411f2 100644 --- a/packages/bundlemon/lib/main/utils/ci/types.ts +++ b/packages/bundlemon/lib/main/utils/ci/types.ts @@ -1,5 +1,6 @@ export interface CIEnvVars { ci: boolean; + raw?: Record; provider?: 'github' | 'codefresh' | 'travis' | 'circleci'; owner?: string; repo?: string; diff --git a/packages/bundlemon/lib/main/utils/utils.ts b/packages/bundlemon/lib/main/utils/utils.ts index 0c660da..379d598 100644 --- a/packages/bundlemon/lib/main/utils/utils.ts +++ b/packages/bundlemon/lib/main/utils/utils.ts @@ -4,3 +4,17 @@ export const getEnvVar = (name: string): string | undefined => { // Convert empty string to undefined return value || undefined; }; + +type ObjectFromList> = { + [K in T extends ReadonlyArray ? U : never]: string | undefined; +}; + +export function envVarsListToObject>(envVars: T): ObjectFromList { + return envVars.reduce( + (acc, envVar) => { + acc[envVar] = getEnvVar(envVar); + return acc; + }, + {} as Record + ) as ObjectFromList; +} From 26a09d8d42236b9624a84b0548ea915d6c7b006f Mon Sep 17 00:00:00 2001 From: LironEr Date: Fri, 18 Oct 2024 12:13:47 +0200 Subject: [PATCH 2/2] fix lint --- packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts b/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts index 84df657..52fb869 100644 --- a/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts +++ b/packages/bundlemon/lib/main/utils/__tests__/configUtils.spec.ts @@ -2,7 +2,7 @@ import { when } from 'jest-when'; import { CreateCommitRecordAuthType, EnvVar } from '../../../common/consts'; import { validateConfig, getCreateCommitRecordAuthParams, getProjectId } from '../configUtils'; -import { getEnvVar, envVarsListToObject } from '../../utils/utils'; +import { getEnvVar } from '../../utils/utils'; import { generateRandomString } from './configUtils'; import { getOrCreateProjectId } from '../../../common/service'; import type { CIEnvVars } from '../ci/types';