diff --git a/.bumpy/fix-check-branch-filter.md b/.bumpy/fix-check-branch-filter.md new file mode 100644 index 0000000..c1ba0ea --- /dev/null +++ b/.bumpy/fix-check-branch-filter.md @@ -0,0 +1,5 @@ +--- +'@varlock/bumpy': patch +--- + +Fix check command to only count bump files from current branch, and handle empty bump files correctly in both local and CI check diff --git a/packages/bumpy/src/commands/check.ts b/packages/bumpy/src/commands/check.ts index 10a3e7a..727cfe3 100644 --- a/packages/bumpy/src/commands/check.ts +++ b/packages/bumpy/src/commands/check.ts @@ -2,7 +2,7 @@ import { relative } from 'node:path'; import { log, colorize } from '../utils/logger.ts'; import { loadConfig } from '../core/config.ts'; import { discoverWorkspace } from '../core/workspace.ts'; -import { readBumpFiles } from '../core/bump-file.ts'; +import { readBumpFiles, filterBranchBumpFiles } from '../core/bump-file.ts'; import { getChangedFiles } from '../core/git.ts'; import type { WorkspacePackage } from '../types.ts'; @@ -14,17 +14,8 @@ import type { WorkspacePackage } from '../types.ts'; export async function checkCommand(rootDir: string): Promise { const config = await loadConfig(rootDir); const { packages } = await discoverWorkspace(rootDir, config); - const bumpFiles = await readBumpFiles(rootDir); - // Find which packages already have bump files - const coveredPackages = new Set(); - for (const bf of bumpFiles) { - for (const release of bf.releases) { - coveredPackages.add(release.name); - } - } - - // Find which packages have changed on this branch vs base + // Find which files have changed on this branch vs base const baseBranch = config.baseBranch; const changedFiles = getChangedFiles(rootDir, baseBranch); @@ -33,6 +24,25 @@ export async function checkCommand(rootDir: string): Promise { return; } + // Filter to only bump files added/modified on this branch + const allBumpFiles = await readBumpFiles(rootDir); + const { branchBumpFiles, branchBumpFileIds } = filterBranchBumpFiles(allBumpFiles, changedFiles); + + // If a bump file was changed but didn't parse (empty bump file), the check passes + const hasEmptyBumpFile = branchBumpFileIds.size > branchBumpFiles.length; + if (hasEmptyBumpFile) { + log.success('Empty bump file found — no releases needed.'); + return; + } + + // Find which packages are covered by bump files on this branch + const coveredPackages = new Set(); + for (const bf of branchBumpFiles) { + for (const release of bf.releases) { + coveredPackages.add(release.name); + } + } + const changedPackages = findChangedPackages(changedFiles, packages, rootDir); if (changedPackages.length === 0) { diff --git a/packages/bumpy/src/commands/ci.ts b/packages/bumpy/src/commands/ci.ts index 51b0505..0113e80 100644 --- a/packages/bumpy/src/commands/ci.ts +++ b/packages/bumpy/src/commands/ci.ts @@ -2,7 +2,7 @@ import { log, colorize } from '../utils/logger.ts'; import { loadConfig } from '../core/config.ts'; import { discoverWorkspace } from '../core/workspace.ts'; import { DependencyGraph } from '../core/dep-graph.ts'; -import { readBumpFiles } from '../core/bump-file.ts'; +import { readBumpFiles, filterBranchBumpFiles } from '../core/bump-file.ts'; import { getChangedFiles } from '../core/git.ts'; import { assembleReleasePlan } from '../core/release-plan.ts'; import { runArgs, runArgsAsync, tryRunArgs } from '../utils/shell.ts'; @@ -107,12 +107,22 @@ export async function ciCheckCommand(rootDir: string, opts: CheckOptions): Promi // Filter to only bump files added/modified in this PR const changedFiles = getChangedFiles(rootDir, config.baseBranch); - const prBumpFileIds = new Set( - changedFiles - .filter((f) => /^\.bumpy\/.*\.md$/.test(f) && !f.endsWith('README.md')) - .map((f) => f.replace(/^\.bumpy\//, '').replace(/\.md$/, '')), + const { branchBumpFiles: prBumpFiles, branchBumpFileIds: prBumpFileIds } = filterBranchBumpFiles( + allBumpFiles, + changedFiles, ); - const prBumpFiles = allBumpFiles.filter((bf) => prBumpFileIds.has(bf.id)); + + // An empty bump file signals intentionally no releases needed + const hasEmptyBumpFile = prBumpFileIds.size > prBumpFiles.length; + + if (hasEmptyBumpFile) { + log.success('Empty bump file found — no releases needed.'); + if (shouldComment && prNumber) { + const prBranch = detectPrBranch(rootDir); + await postOrUpdatePrComment(prNumber, formatNoBumpFilesComment(prBranch, pm), rootDir, opts.patComments); + } + return; + } if (prBumpFiles.length === 0) { const msg = 'No bump files found in this PR.'; diff --git a/packages/bumpy/src/core/bump-file.ts b/packages/bumpy/src/core/bump-file.ts index f37ec5d..1c1e084 100644 --- a/packages/bumpy/src/core/bump-file.ts +++ b/packages/bumpy/src/core/bump-file.ts @@ -172,3 +172,29 @@ function fileToId(filePath: string): string { const base = filePath.split('/').pop()!; return base.replace(/\.md$/, ''); } + +/** + * Given a list of changed file paths (relative to root), extract the IDs + * of bump files that were added/modified. Shared by `check` and `ci check`. + */ +export function extractBumpFileIdsFromChangedFiles(changedFiles: string[]): Set { + return new Set( + changedFiles + .filter((f) => /^\.bumpy\/.*\.md$/.test(f) && !f.endsWith('README.md')) + .map((f) => f.replace(/^\.bumpy\//, '').replace(/\.md$/, '')), + ); +} + +/** + * Filter bump files to only those added/modified on the current branch. + * Returns the filtered bump files and whether any changed bump file was + * empty (has no releases — signals intentionally no releases needed). + */ +export function filterBranchBumpFiles( + allBumpFiles: BumpFile[], + changedFiles: string[], +): { branchBumpFiles: BumpFile[]; branchBumpFileIds: Set } { + const branchBumpFileIds = extractBumpFileIdsFromChangedFiles(changedFiles); + const branchBumpFiles = allBumpFiles.filter((bf) => branchBumpFileIds.has(bf.id)); + return { branchBumpFiles, branchBumpFileIds }; +}