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
5 changes: 5 additions & 0 deletions .bumpy/fix-pr-links-and-workspace-shorthand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@varlock/bumpy': patch
---

Fix PR diff links to use full absolute URLs with `/changes` path and sha256 anchors. Preserve `workspace:^`, `workspace:~`, and `workspace:*` shorthand ranges during versioning instead of resolving them.
49 changes: 37 additions & 12 deletions packages/bumpy/src/commands/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ async function createVersionPr(
pushWithToken(rootDir, branch, config);

// Create or update PR
const prBody = formatVersionPrBody(plan, config.versionPr.preamble, packageDirs);
const repo = process.env.GITHUB_REPOSITORY;

if (existingPr) {
const validPr = validatePrNumber(existingPr);
const prBody = formatVersionPrBody(plan, config.versionPr.preamble, packageDirs, repo, validPr);
log.step(`Updating existing PR #${validPr}...`);
await withPatToken(!!patPr, () =>
runArgsAsync(['gh', 'pr', 'edit', validPr, '--title', config.versionPr.title, '--body-file', '-'], {
Expand All @@ -392,13 +393,30 @@ async function createVersionPr(
} else {
log.step('Creating version PR...');
const prTitle = config.versionPr.title;
// Create PR first without diff links, then update body with correct PR number
const prBody = formatVersionPrBody(plan, config.versionPr.preamble, packageDirs, repo, null);
const result = await withPatToken(!!patPr, () =>
runArgsAsync(
['gh', 'pr', 'create', '--title', prTitle, '--body-file', '-', '--base', baseBranch, '--head', branch],
{ cwd: rootDir, input: prBody },
),
);
log.success(`🐸 Created PR: ${result}`);

// Update body now that we know the PR number
if (repo) {
const newPrNumber = result?.match(/\/pull\/(\d+)/)?.[1];
if (newPrNumber) {
const updatedBody = formatVersionPrBody(plan, config.versionPr.preamble, packageDirs, repo, newPrNumber);
await withPatToken(!!patPr, () =>
runArgsAsync(['gh', 'pr', 'edit', newPrNumber, '--body-file', '-'], {
cwd: rootDir,
input: updatedBody,
}),
);
}
}

if (!patPr) {
// Push again with the custom token now that the PR exists, so that a
// `pull_request: synchronize` event is generated and CI workflows trigger.
Expand Down Expand Up @@ -479,7 +497,9 @@ function formatReleasePlanComment(
const filename = `${bf.id}.md`;
const parts: string[] = [`\`${filename}\``];
if (repo) {
parts.push(`([view diff](https://github.com/${repo}/pull/${prNumber}/files#diff-.bumpy/${filename}))`);
parts.push(
`([view diff](https://github.com/${repo}/pull/${prNumber}/changes#diff-${sha256Hex(`.bumpy/${filename}`)}))`,
);
if (prBranch) {
parts.push(`([edit](https://github.com/${repo}/edit/${prBranch}/.bumpy/${filename}))`);
}
Expand Down Expand Up @@ -542,22 +562,25 @@ function bumpSectionHeader(type: string): string {
}

/** Build inline diff links for a package's changed files in the PR */
function buildDiffLinks(pkgDir: string): string {
const pkgJsonPath = `${pkgDir}/package.json`;
function buildDiffLinks(pkgDir: string, changesBaseUrl: string | null): string {
if (!changesBaseUrl) return '';
const changelogPath = `${pkgDir}/CHANGELOG.md`;
// GitHub anchors diff sections with #diff-<sha256 of file path>
const links = [
`[package.json](#diff-${sha256Hex(pkgJsonPath)})`,
`[CHANGELOG.md](#diff-${sha256Hex(changelogPath)})`,
];
return ` <sub>${links.join(' · ')}</sub>`;
return ` <sub>[CHANGELOG.md](${changesBaseUrl}#diff-${sha256Hex(changelogPath)})</sub>`;
}

function sha256Hex(input: string): string {
return createHash('sha256').update(input).digest('hex');
}

function formatVersionPrBody(plan: ReleasePlan, preamble: string, packageDirs: Map<string, string>): string {
function formatVersionPrBody(
plan: ReleasePlan,
preamble: string,
packageDirs: Map<string, string>,
repo: string | undefined,
prNumber: string | null,
): string {
const changesBaseUrl = repo && prNumber ? `https://github.com/${repo}/pull/${prNumber}/changes` : null;
const lines: string[] = [];
lines.push(preamble);
lines.push('');
Expand All @@ -576,7 +599,7 @@ function formatVersionPrBody(plan: ReleasePlan, preamble: string, packageDirs: M
for (const r of releases) {
const suffix = r.isDependencyBump ? ' _(dep)_' : r.isCascadeBump ? ' _(cascade)_' : '';
const pkgDir = packageDirs.get(r.name);
const diffLinks = pkgDir ? buildDiffLinks(pkgDir) : '';
const diffLinks = pkgDir ? buildDiffLinks(pkgDir, changesBaseUrl) : '';
lines.push(`#### \`${r.name}\` ${r.oldVersion} → **${r.newVersion}**${suffix}${diffLinks}`);
lines.push('');

Expand All @@ -585,7 +608,9 @@ function formatVersionPrBody(plan: ReleasePlan, preamble: string, packageDirs: M
if (relevantBumpFiles.length > 0) {
for (const bf of relevantBumpFiles) {
if (bf.summary) {
const bfLink = ` ([bump file](#diff-${sha256Hex(`.bumpy/${bf.id}.md`)}))`;
const bfLink = changesBaseUrl
? ` ([bump file](${changesBaseUrl}#diff-${sha256Hex(`.bumpy/${bf.id}.md`)}))`
: '';
const summaryLines = bf.summary.split('\n');
lines.push(`- ${summaryLines[0]}${bfLink}`);
for (let i = 1; i < summaryLines.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions packages/bumpy/src/core/apply-release-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function updateRange(range: string, newVersion: string): string {
const prefixMatch = cleanRange.match(/^(\^|~|>=|>|<=|<|=)?/);
const prefix = prefixMatch?.[1] ?? '^';

// Handle wildcard ranges
if (cleanRange === '*' || cleanRange === '') {
return range; // don't touch wildcards
// Handle wildcard ranges and workspace shorthand (workspace:^, workspace:~, workspace:*)
if (cleanRange === '*' || cleanRange === '' || cleanRange === '^' || cleanRange === '~') {
return range; // don't touch wildcards or workspace shorthands
}

return `${protocol}${prefix}${newVersion}`;
Expand Down
53 changes: 53 additions & 0 deletions packages/bumpy/test/core/apply-release-plan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,59 @@ describe('applyReleasePlan', () => {
expect(deps.core).toBe('workspace:^2.0.0');
});

test('preserves workspace shorthand (workspace:^, workspace:~, workspace:*)', async () => {
const coreDir = await setupPackage('core', '1.0.0');
const appDir = await setupPackage('app', '1.0.0', {
dependencies: { core: 'workspace:*' },
devDependencies: { core: 'workspace:^' },
peerDependencies: { core: 'workspace:~' },
});

const packages = new Map<string, WorkspacePackage>();
packages.set('core', {
name: 'core',
version: '1.0.0',
dir: coreDir,
relativeDir: 'packages/core',
packageJson: { name: 'core', version: '1.0.0' },
private: false,
dependencies: {},
devDependencies: {},
peerDependencies: {},
optionalDependencies: {},
});
packages.set('app', {
name: 'app',
version: '1.0.0',
dir: appDir,
relativeDir: 'packages/app',
packageJson: {
name: 'app',
version: '1.0.0',
dependencies: { core: 'workspace:*' },
devDependencies: { core: 'workspace:^' },
peerDependencies: { core: 'workspace:~' },
},
private: false,
dependencies: { core: 'workspace:*' },
devDependencies: { core: 'workspace:^' },
peerDependencies: { core: 'workspace:~' },
optionalDependencies: {},
});

const coreRelease = makeRelease('core', '2.0.0', { type: 'major', oldVersion: '1.0.0' });
const appRelease = makeRelease('app', '1.0.1', { oldVersion: '1.0.0', isDependencyBump: true });

await ensureDir(resolve(tmpDir, '.bumpy'));

await applyReleasePlan(makeReleasePlan([coreRelease, appRelease]), packages, tmpDir, makeConfig());

const appPkg = await readJson<Record<string, unknown>>(resolve(appDir, 'package.json'));
expect((appPkg.dependencies as Record<string, string>).core).toBe('workspace:*');
expect((appPkg.devDependencies as Record<string, string>).core).toBe('workspace:^');
expect((appPkg.peerDependencies as Record<string, string>).core).toBe('workspace:~');
});

test('preserves package.json formatting when bumping version', async () => {
// Write a package.json with custom formatting (inline arrays, tab indent, etc.)
const pkgDir = resolve(tmpDir, 'packages/pkg-a');
Expand Down