Skip to content
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
11 changes: 8 additions & 3 deletions packages/bundler-plugins/src/core/options-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
ModuleMetadata,
ModuleMetadataCallback,
} from './types';
import { determineReleaseName, readCommitEnvVar } from './utils';
import { determineReleaseName, canAutoDetectRepository, readCommitEnvVar } from './utils';

export type NormalizedOptions = {
org: string | undefined;
Expand Down Expand Up @@ -137,6 +137,8 @@ export function normalizeUserOptions(userOptions: UserOptions): NormalizedOption
_experiments: userOptions._experiments ?? {},
};

const vercelEnv = getVercelEnv();

if (options.release.setCommits === undefined) {
const vercelCommitSha = readCommitEnvVar('VERCEL_GIT_COMMIT_SHA');
if (
Expand All @@ -147,7 +149,7 @@ export function normalizeUserOptions(userOptions: UserOptions): NormalizedOption
// We only want to set commits for the production env because Sentry becomes extremely noisy (eg on slack) for
// preview environments because the previous commit is always the "stem" commit of the preview/PR causing Sentry
// to notify you for other people creating PRs.
process.env['VERCEL_TARGET_ENV'] === 'production'
vercelEnv === 'production'
) {
options.release.setCommits = {
shouldNotThrowOnFailure: true,
Expand All @@ -157,6 +159,10 @@ export function normalizeUserOptions(userOptions: UserOptions): NormalizedOption
ignoreEmpty: true,
ignoreMissing: true,
};
} else if (process.env['VERCEL'] && vercelEnv !== 'production' && !canAutoDetectRepository()) {
// Vercel's own builders have no git remote and no other CI variables, so `--auto` can only fail there.
// Builds that have either, such as `vercel build` in GitHub Actions, keep `--auto` below.
options.release.setCommits = false;
} else {
options.release.setCommits = {
shouldNotThrowOnFailure: true,
Expand All @@ -167,7 +173,6 @@ export function normalizeUserOptions(userOptions: UserOptions): NormalizedOption
}
}

const vercelEnv = getVercelEnv();
if (options.release.deploy === undefined && process.env['VERCEL'] && vercelEnv) {
options.release.deploy = {
env: vercelEnv,
Expand Down
31 changes: 31 additions & 0 deletions packages/bundler-plugins/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,37 @@ function gitRevision(): string | undefined {
return gitRevision;
}

// Mirrors the CLI's `set-commits --auto` fallback for builds without an `origin` remote. Vercel is left out
// because the CLI only reads `VERCEL_GIT_*` on production builds.
const CI_REPOSITORY_ENV_VARS = [
'GITHUB_REPOSITORY',
'CI_PROJECT_PATH',
'BITBUCKET_REPO_FULL_NAME',
'RENDER_GIT_REPO_SLUG',
'RAILWAY_GIT_REPO_NAME',
'CIRCLE_PROJECT_REPONAME',
'BUILDKITE_REPO',
'BUILD_REPOSITORY_URI',
] as const;

/**
* Whether `set-commits --auto` can find the repository: from the `origin` git remote, or from CI variables.
*/
export function canAutoDetectRepository(): boolean {
if (
CI_REPOSITORY_ENV_VARS.some(name => process.env[name]) ||
(process.env['NETLIFY'] && process.env['REPOSITORY_URL'])
) {
return true;
}
try {
childProcess.execSync('git remote get-url origin', { stdio: 'ignore', windowsHide: true });
return true;
} catch {
return false;
}
}

/**
* Tries to guess a release name based on environmental data.
*/
Expand Down
73 changes: 73 additions & 0 deletions packages/bundler-plugins/test/core/option-mappings.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import childProcess from 'child_process';
import type { Options } from '../../src/core';
import type { NormalizedOptions } from '../../src/core/options-mapping';
import { normalizeUserOptions, validateOptions } from '../../src/core/options-mapping';
Expand Down Expand Up @@ -209,6 +210,78 @@ describe('normalizeUserOptions()', () => {
vi.restoreAllMocks();
});

test.each(['preview', 'development', 'staging'])(
'should skip commit association on %s Vercel builds without a git remote',
targetEnv => {
vi.spyOn(childProcess, 'execSync').mockImplementation(() => {
throw new Error('fatal: not a git repository');
});
// Only Vercel's variables, so CI variables from the machine running the tests don't leak in.
process.env = {
VERCEL: '1',
VERCEL_TARGET_ENV: targetEnv,
VERCEL_GIT_COMMIT_SHA: 'abc123',
VERCEL_GIT_REPO_OWNER: 'my-owner',
VERCEL_GIT_REPO_SLUG: 'my-repo',
};

const normalizedOptions = normalizeUserOptions({ release: { name: 'my-release' } });

expect(normalizedOptions.release.setCommits).toBe(false);
vi.restoreAllMocks();
},
);

test('should keep automatic commit association on preview Vercel builds that have a git remote', () => {
const execSyncSpy = vi
.spyOn(childProcess, 'execSync')
.mockReturnValue(Buffer.from('https://github.com/my-owner/my-repo.git'));
// Only Vercel's variables, so the git remote is what keeps `--auto`, not CI variables from the test machine.
process.env = {
VERCEL: '1',
VERCEL_TARGET_ENV: 'preview',
VERCEL_GIT_COMMIT_SHA: 'abc123',
VERCEL_GIT_REPO_OWNER: 'my-owner',
VERCEL_GIT_REPO_SLUG: 'my-repo',
};

const normalizedOptions = normalizeUserOptions({ release: { name: 'my-release' } });

expect(execSyncSpy).toHaveBeenCalledWith('git remote get-url origin', expect.anything());
expect(normalizedOptions.release.setCommits).toEqual(expect.objectContaining({ auto: true }));
expect(normalizedOptions.release.setCommits).not.toHaveProperty('commit');
vi.restoreAllMocks();
});
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

test('should keep automatic commit association on preview Vercel builds in another CI without a git remote', () => {
vi.spyOn(childProcess, 'execSync').mockImplementation(() => {
throw new Error('fatal: not a git repository');
});
process.env['VERCEL'] = '1';
process.env['VERCEL_TARGET_ENV'] = 'preview';
process.env['GITHUB_REPOSITORY'] = 'my-owner/my-repo';

const normalizedOptions = normalizeUserOptions({ release: { name: 'my-release' } });

expect(normalizedOptions.release.setCommits).toEqual(expect.objectContaining({ auto: true }));
vi.restoreAllMocks();
});

test('should associate commits from Vercel env vars when only VERCEL_ENV is production', () => {
process.env['VERCEL'] = '1';
delete process.env['VERCEL_TARGET_ENV'];
process.env['VERCEL_ENV'] = 'production';
process.env['VERCEL_GIT_COMMIT_SHA'] = 'abc123';
process.env['VERCEL_GIT_REPO_OWNER'] = 'my-owner';
process.env['VERCEL_GIT_REPO_SLUG'] = 'my-repo';

const normalizedOptions = normalizeUserOptions({ release: { name: 'my-release' } });

expect(normalizedOptions.release.setCommits).toEqual(
expect.objectContaining({ commit: 'abc123', repo: 'my-owner/my-repo' }),
);
});

test('should not create deploy config when Vercel env vars are missing', () => {
const userOptions: Options = {
org: 'my-org',
Expand Down
Loading