Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add raw CI env vars to log #216

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
36 changes: 26 additions & 10 deletions packages/bundlemon/lib/main/utils/ci/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = () => {
Expand Down
39 changes: 25 additions & 14 deletions packages/bundlemon/lib/main/utils/ci/providers/circleCI.ts
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 21 additions & 8 deletions packages/bundlemon/lib/main/utils/ci/providers/github.ts
Original file line number Diff line number Diff line change
@@ -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,
};
},
};
Expand Down
27 changes: 19 additions & 8 deletions packages/bundlemon/lib/main/utils/ci/providers/travis.ts
Original file line number Diff line number Diff line change
@@ -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,
};
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/bundlemon/lib/main/utils/ci/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface CIEnvVars {
ci: boolean;
raw?: Record<string, string | undefined>;
provider?: 'github' | 'codefresh' | 'travis' | 'circleci';
owner?: string;
repo?: string;
Expand Down
14 changes: 14 additions & 0 deletions packages/bundlemon/lib/main/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ export const getEnvVar = (name: string): string | undefined => {
// Convert empty string to undefined
return value || undefined;
};

type ObjectFromList<T extends ReadonlyArray<string>> = {
[K in T extends ReadonlyArray<infer U> ? U : never]: string | undefined;
};

export function envVarsListToObject<T extends ReadonlyArray<string>>(envVars: T): ObjectFromList<T> {
return envVars.reduce(
(acc, envVar) => {
acc[envVar] = getEnvVar(envVar);
return acc;
},
{} as Record<string, string | undefined>
) as ObjectFromList<T>;
}
Loading