Skip to content

Commit

Permalink
Merge pull request #31 from restfulhead/main
Browse files Browse the repository at this point in the history
chore: release
  • Loading branch information
restfulhead authored Apr 6, 2024
2 parents a5d74a5 + 39614a0 commit c6a13fd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 43 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:
shell: bash

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: ${{ inputs.git-ref }}

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
cache: "npm"
cache-dependency-path: "package-lock.json"

Expand All @@ -42,6 +42,7 @@ jobs:
CI: true
run: |
npm audit --omit=dev
npm run lint -ws
npm run build -ws
npm run test -ws
Expand Down
21 changes: 10 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
shell: bash

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ inputs.git-ref }}
Expand All @@ -33,9 +33,9 @@ jobs:
git config user.name "$GITHUB_ACTOR"
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
cache: "npm"
cache-dependency-path: "package-lock.json"
registry-url: 'https://registry.npmjs.org'
Expand Down Expand Up @@ -98,26 +98,25 @@ jobs:
VERSION=`npx auto version $FROM_PARAM $DEBUG_FLAG`
if [ ! -z "$VERSION" ]; then
echo "::notice title=✅ Detected $VERSION version change for $PCKG_NAME::Bumping version"
echo "::notice title=✅ Detected $VERSION version change for $PCKG_NAME::Bumping $VERSION version of $PCKG_NAME"
npx auto changelog --base-branch ${{ steps.get-workspaces.outputs.branch }} $FROM_PARAM $DEBUG_FLAG
npm version $VERSION -m "chore: bump release version to %s [skip ci]" || true
npm run build
echo "npm version $VERSION -m 'chore: bump release version to %s [skip ci]'"
npm version $VERSION --tag-version-prefix="${PCKG_NAME}_v" -m "chore: bump release version to %s [skip ci]"
NEW_VERSION_NO=`node -pe "require('./package.json').version"`
git tag -d v$NEW_VERSION_NO || true
NEW_TAG=${PCKG_NAME}_v$NEW_VERSION_NO
echo "Going to create a new release for $NEW_TAG"
git add -A
git commit -m "chore: release v$NEW_VERSION_NO [skip ci]" || true
git tag -a $NEW_TAG -m "chore: tag v$NEW_VERSION_NO [skip ci]"
git commit -m "chore: release $NEW_TAG [skip ci]"
git push "https://$GITHUB_ACTOR:[email protected]/$GITHUB_REPOSITORY" HEAD:${{ steps.get-workspaces.outputs.branch }} --follow-tags
npx auto release --use-version $NEW_TAG $FROM_PARAM --base-branch ${{ steps.get-workspaces.outputs.branch }} $DEBUG_FLAG
IS_PRIVATE=`node -pe "require('./package.json').private"`
if [ "$IS_PRIVATE" != "true" ]; then
npm publish ./dist
echo "::notice title=🚀 ${PCKG_NAME} v$NEW_VERSION_NO::Package versioned and published"
echo "::notice title=🚀 ${PCKG_NAME} v$NEW_VERSION_NO::Package ${PCKG_NAME} v$NEW_VERSION_NO versioned and published"
fi
rm -rf .git
else
echo "::notice title=Versioning of $PCKG_NAME skipped::No relevant changes detected."
echo "::notice title=Versioning of $PCKG_NAME skipped::No relevant changes detected for $PCKG_NAME."
fi
cd $topdir
Expand Down
3 changes: 2 additions & 1 deletion packages/example-test-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"scripts": {
"build": "echo 'Nothing to do here...'",
"test": "echo 'No tests yet...'"
"test": "echo 'No tests yet...'",
"lint": "echo 'Nothing to do here.'"
},
"auto": {
"plugins": [
Expand Down
20 changes: 11 additions & 9 deletions packages/filter-by-workspace-path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import * as path from 'path'
import { Auto, IExtendedCommit, ILogger, IPlugin, SEMVER, inFolder } from '@auto-it/core'
import { inc, ReleaseType } from 'semver'

function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: IExtendedCommit, logger: ILogger): boolean {
function shouldOmitCommit(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)

const atLeastOneFileInCurrentDir = fixedFiles.find((file) => inFolder(wsDir, file))
const atLeastOneFileInCurrentDir = commit.files.find((file) => inFolder(wsDir, file))

if (!atLeastOneFileInCurrentDir) {
logger.verbose.log(`All files are outside the current workspace directory ('${wsDir}'). Omitting commit '${commit.hash}'.`)
Expand All @@ -23,7 +21,9 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit:
return true
}

logger.verbose.log(`At least one file is in the current workspace ('${wsDir}'). Including commit '${commit.hash}'.`)
logger.verbose.log(
`At least one file is in the current workspace ('${atLeastOneFileInCurrentDir}' in '${wsDir}'). Including commit '${commit.hash}'.`
)
return false
}
}
Expand Down Expand Up @@ -53,13 +53,15 @@ export default class FilterByWorkspacePathPlugin implements IPlugin {
const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] })
const workspaceDeps: any = JSON.parse(npmResult).dependencies
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11)
const resolved = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved
// 'resolved' var looks like 'file:../../packages/filter-by-workspace-path'
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const relativeWorkspacePath = String(resolved).split(':', 2)[1]
currentWorkspace = path.resolve(currentDir, relativeWorkspacePath)
}

auto.hooks.onCreateLogParse.tap(this.name, (logParse) => {
logParse.hooks.omitCommit.tap(this.name, (commit) =>
shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger) ? true : undefined
)
logParse.hooks.omitCommit.tap(this.name, (commit) => (shouldOmitCommit(currentWorkspace, commit, auto.logger) ? true : undefined))
})

auto.hooks.onCreateRelease.tap(this.name, (release) => {
Expand Down
110 changes: 91 additions & 19 deletions packages/filter-by-workspace-path/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import LogParse from '@auto-it/core/dist/log-parse'
import { makeHooks, makeLogParseHooks, makeReleaseHooks } from '@auto-it/core/dist/utils/make-hooks'
import createLog from '@auto-it/core/dist/utils/logger'
import Release from '@auto-it/core/dist/release'
import FilterByPathPlugin from '../src'
import FilterByWorkspacePathPlugin, { ProjectFilteringPluginOptions } from '../src'

const setup = () => {
const plugin = new FilterByPathPlugin()
const setup = (pluginOptions: ProjectFilteringPluginOptions) => {
const plugin = new FilterByWorkspacePathPlugin(pluginOptions)
const hooks = makeHooks()
const logger = createLog()
const logParseHooks = makeLogParseHooks()
Expand All @@ -24,69 +24,141 @@ const setup = () => {
return logParseHooks
}

describe('Omit Commits Plugin', () => {
describe('Test plugin omits commits when using NPM', () => {
const hooks = setup({ npm: true })

it('should not filter the commit single file', async () => {
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBeUndefined()
})

it('should filter the commit single file', async () => {
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 commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'src/index.ts'), '/outside'],
})
expect(await hooks.omitCommit.promise(commit)).toBeUndefined()
})

it('should filter the commit single file', async () => {
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 commit = makeCommitFromMsg('foo', {
labels: ['skip-release'],
pullRequest: { number: 1 },
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})

it('should skip commit marked as skip-ci', async () => {
const commit = makeCommitFromMsg('foo [skip ci]', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})

it('should not skip commit in a sub-directory with the same prefix', async () => {
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'sub-dir/src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBeUndefined()
})

it('should skip commit in a sub-directory without same prefix', async () => {
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('..', 'sub-dir/src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})

it('should skip commits without related pull request', async () => {
const commit = makeCommitFromMsg('foo', {
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})
})

describe('Test plugin omits commits without NPM', () => {
const hooks = setup({ npm: false })

it('should not filter the commit single file', async () => {
const hooks = setup()
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts')],
files: [path.resolve('.', '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', { 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', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'packages/filter-by-workspace-path/src/index.ts'), '/outside'],
files: [path.resolve('.', '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', { 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')],
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})

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')],
files: [path.resolve('.', '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()
it('should not skip commit in a sub-directory with the same prefix', async () => {
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'sub-dir/src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBeUndefined()
})

it('should skip commit in a sub-directory without same prefix', async () => {
const commit = makeCommitFromMsg('foo', {
pullRequest: { number: 1 },
files: [path.resolve('.', 'packages/filter-by-workspace-path-sub-dir/src/index.ts')],
files: [path.resolve('..', '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')],
files: [path.resolve('.', 'src/index.ts')],
})
expect(await hooks.omitCommit.promise(commit)).toBe(true)
})
Expand Down

0 comments on commit c6a13fd

Please sign in to comment.