Skip to content

Commit

Permalink
Merge pull request #9 from restfulhead/skip-no-pr-commits
Browse files Browse the repository at this point in the history
feat: omit commits that aren't related to a pull request
  • Loading branch information
restfulhead authored Feb 16, 2024
2 parents a058a1d + 0035138 commit 20a298b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
9 changes: 6 additions & 3 deletions packages/filter-by-workspace-path/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. :-/
Expand Down
4 changes: 4 additions & 0 deletions packages/filter-by-workspace-path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 24 additions & 5 deletions packages/filter-by-workspace-path/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,39 @@ 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)
})

it('should skip commit labeled as skip-release', async () => {
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)
Expand All @@ -60,14 +67,26 @@ 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)
})

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)
})
})

0 comments on commit 20a298b

Please sign in to comment.