diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index 97f3c49..a7cfda9 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -17,14 +17,17 @@ To start using this plugin, add it to your `.autorc` config, for example: } ``` -If you are using this for NPM workspaces/sub-packages, then add this configuration to each workspace. Then run e.g. `auto changelog` from -each workspace directory. Observe that commits with only files from directories outside the workspace directory are omitted. +Then, if your project uses NPM workspaces and you run e.g. `auto changelog` not from the root directory, but directly from a workspace +directory, then the changelog will only include pull requests that contain files inside the current workspace directory. However, carefully read the following caveats section. ## Caveats -* Note that this plugin also ommits commits that are either labeled with `skip-release` or have `[skip ci]` in their commit message. +* Note that this plugin also ommits commits that + * do not have a related pull request (e.g. pushed directly to the release branch) + * belong to pull requests that has the `skip-release` label attached + * have `[skip ci]` in their commit message * By default, `auto version` seems to set the version to `patch` instead of `noVersion` even if all commits were filtered out. * Therefore, to detect if there were no changes in this case, you have to use the `auto changelog` command instead and then check whether a changelog was actually generated. :-/ diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index ccbff05..efd776c 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -4,6 +4,10 @@ import { execSync } from 'child_process' import * as path from 'path' function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: IExtendedCommit, logger: ILogger): boolean { + if (!commit.pullRequest) { + return true + } + // auto adds the current path to the file paths reported from git, so we need to undo this const fixedFiles = commit.files.map((file) => path.relative(currentDir, file)) const wsDir = path.join(currentWorkspace, path.sep) diff --git a/packages/filter-by-workspace-path/test/index.spec.ts b/packages/filter-by-workspace-path/test/index.spec.ts index 97b4287..3b89386 100644 --- a/packages/filter-by-workspace-path/test/index.spec.ts +++ b/packages/filter-by-workspace-path/test/index.spec.ts @@ -26,25 +26,31 @@ const setup = () => { describe('Omit Commits Plugin', () => { it('should not filter the commit single file', async () => { const hooks = setup() - const commit = makeCommitFromMsg('foo', { files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')] }) + const commit = makeCommitFromMsg('foo', { + pullRequest: { number: 1 }, + files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')], + }) expect(await hooks.omitCommit.promise(commit)).toBeUndefined() }) it('should filter the commit single file', async () => { const hooks = setup() - const commit = makeCommitFromMsg('foo', { files: ['/outside'] }) + const commit = makeCommitFromMsg('foo', { pullRequest: { number: 1 }, files: ['/outside'] }) expect(await hooks.omitCommit.promise(commit)).toBe(true) }) it('should not filter the commit multi file', async () => { const hooks = setup() - const commit = makeCommitFromMsg('foo', { files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts'), '/outside'] }) + const commit = makeCommitFromMsg('foo', { + pullRequest: { number: 1 }, + files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts'), '/outside'], + }) expect(await hooks.omitCommit.promise(commit)).toBeUndefined() }) it('should filter the commit single file', async () => { const hooks = setup() - const commit = makeCommitFromMsg('foo', { files: ['/outside', '/anotheroutsider'] }) + const commit = makeCommitFromMsg('foo', { pullRequest: { number: 1 }, files: ['/outside', '/anotheroutsider'] }) expect(await hooks.omitCommit.promise(commit)).toBe(true) }) @@ -52,6 +58,7 @@ describe('Omit Commits Plugin', () => { const hooks = setup() const commit = makeCommitFromMsg('foo', { labels: ['skip-release'], + pullRequest: { number: 1 }, files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')], }) expect(await hooks.omitCommit.promise(commit)).toBe(true) @@ -60,6 +67,7 @@ describe('Omit Commits Plugin', () => { it('should skip commit marked as skip-ci', async () => { const hooks = setup() const commit = makeCommitFromMsg('foo [skip ci]', { + pullRequest: { number: 1 }, files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')], }) expect(await hooks.omitCommit.promise(commit)).toBe(true) @@ -67,7 +75,18 @@ describe('Omit Commits Plugin', () => { it('should skip commit in a sub-directory with the same prefix', async () => { const hooks = setup() - const commit = makeCommitFromMsg('foo', { files: [path.resolve('.', 'packages/filter-by-workspace-path-sub-dir/src/index.ts')] }) + const commit = makeCommitFromMsg('foo', { + pullRequest: { number: 1 }, + files: [path.resolve('.', 'packages/filter-by-workspace-path-sub-dir/src/index.ts')], + }) + expect(await hooks.omitCommit.promise(commit)).toBe(true) + }) + + it('should skip commits without related pull request', async () => { + const hooks = setup() + const commit = makeCommitFromMsg('foo', { + files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')], + }) expect(await hooks.omitCommit.promise(commit)).toBe(true) }) })