From 70ad84bd1325d477019b5257e9c9dc4fb4c88238 Mon Sep 17 00:00:00 2001 From: Patrick Ruhkopf Date: Fri, 16 Feb 2024 16:06:01 -0500 Subject: [PATCH 1/2] feat: set version to 'noVersion' if no commits are in scope --- .github/workflows/release.yml | 51 +++++++++---------- .vscode/launch.json | 29 ++++++++++- config/.eslintrc.js | 6 ++- packages/filter-by-workspace-path/README.md | 10 ++-- .../filter-by-workspace-path/src/index.ts | 24 ++++++--- .../test/index.spec.ts | 15 +++--- 6 files changed, 87 insertions(+), 48 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5b59df2..a930841 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -69,8 +69,10 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: | - for path in ${{ steps.get-workspaces.outputs.ws }}; do - cd $path + topdir=$(pwd) + for workspace in ${{ steps.get-workspaces.outputs.ws }}; do + echo "Potentially going to version and release $workspace" + cd $workspace mkdir .git PCKG_NAME=`node -pe "require('./package.json').name"` @@ -80,38 +82,35 @@ jobs: if [ "$CUR_VERSION_NO" != "0.0.0" ]; then FROM_PARAM="--from ${PCKG_NAME}_v${CUR_VERSION_NO}" fi - + VERSION=`npx auto version $FROM_PARAM` if [ ! -z "$VERSION" ]; then + echo "::notice title=✅ Detected $VERSION version change for $PCKG_NAME::Bumping version" npx auto changelog --base-branch ${{ steps.get-workspaces.outputs.branch }} $FROM_PARAM - - unpushed_commits=$(git log origin/${{ steps.get-workspaces.outputs.branch }}..${{ steps.get-workspaces.outputs.branch }}) - - if [ -z "$unpushed_commits" ]; then - echo "No changelog was generated for $PCKG_NAME" - else - npm version $VERSION -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 - 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]" - git tag -a $NEW_TAG -m "chore: tag v$NEW_VERSION_NO [skip ci]" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$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 }} - IS_PRIVATE=`node -pe "require('./package.json').private"` - if [ "$IS_PRIVATE" != "true" ]; then - npm publish ./dist - fi + npm version $VERSION -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 + 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]" + git tag -a $NEW_TAG -m "chore: tag v$NEW_VERSION_NO [skip ci]" + git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$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 }} + IS_PRIVATE=`node -pe "require('./package.json').private"` + rm -rf .git + if [ "$IS_PRIVATE" != "true" ]; then + cd $topdir + echo "publishing $workspace/dist" + npm publish ./$workspace/dist + echo "::notice title=🚀 ${PCKG_NAME} v$NEW_VERSION_NO::Package versioned and published" fi else - echo 'Auto versioning failed.' + echo "::notice title=Versioning of $PCKG_NAME skipped::No relevant changes detected." exit 1 fi - rm -rf .git - cd - + cd $topdir done - if: ${{ always() }} diff --git a/.vscode/launch.json b/.vscode/launch.json index bc4f10b..fa0fc17 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,32 @@ "changelog", "-d", "--from", - "@restfulhead/npm-auto-plugin-example-test-package_v0.0.2" + "@restfulhead/npm-auto-plugin-example-test-package_v0.0.3" + ], + "internalConsoleOptions": "openOnSessionStart", + "sourceMaps": true, + "outputCapture": "std", + "smartStep": true, + "skipFiles": [ + "/**", + "node_modules/**" + ], + "resolveSourceMapLocations": [ + "${workspaceFolder}/**", + "!**/node_modules/**" + ], + "envFile": "${workspaceFolder}/.env" + }, + { + "name": "Auto version", + "type": "node", + "request": "launch", + "cwd": "${workspaceFolder}/packages/example-test-package", + "program": "${workspaceFolder}/node_modules/auto/dist/bin/auto", + "args": [ + "version", + "--from", + "@restfulhead/npm-auto-plugin-example-test-package_v0.0.3" ], "internalConsoleOptions": "openOnSessionStart", "sourceMaps": true, @@ -36,7 +61,7 @@ "args": [ "${relativeFile}", "--config", - "${workspaceFolder}/packages/filter-by-workspace-path/package.json", + "${workspaceFolder}/packages/filter-by-workspace-path/jest.debug.config.js", "--testTimeout", "300000", "--no-cache", diff --git a/config/.eslintrc.js b/config/.eslintrc.js index 72587bc..1b03ed3 100644 --- a/config/.eslintrc.js +++ b/config/.eslintrc.js @@ -17,8 +17,6 @@ module.exports = { rules: { 'unused-imports/no-unused-imports': 'error', 'require-await': 'error', - }, - settings: { '@typescript-eslint/adjacent-overload-signatures': 'error', '@typescript-eslint/array-type': ['error'], '@typescript-eslint/await-thenable': 'error', @@ -90,6 +88,10 @@ module.exports = { format: ['camelCase'], leadingUnderscore: 'forbid', }, + { + selector: 'import', + format: ['camelCase', 'PascalCase'], + }, { selector: 'default', format: ['camelCase'], diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index a7cfda9..7d28cba 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -24,16 +24,16 @@ However, carefully read the following caveats section. ## Caveats -* Note that this plugin also ommits commits that +* Note that this plugin also omits 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. :-/ +* This plugin modifies the behavior of `auto version`. By default, it seems to set the version to `patch` instead of `noVersion` even if all + commits were omitted. With this plugin, `noVersion` is returned if there are no relevant commits. * While the title in the GitHub release notes is correct, the version is missing in the `CHANGELOG.md` file for currently unknown reasons * You can't use the `shipit` command, because for example, the version in each package should only contain the version number - (e.g. `v1.0.0`), but the tag and Github release must include the package name to avoid ambigious release names/tags. + (e.g. `v1.0.0`), but the tag and Github release must include the package name to avoid ambigious release names/tags. Maybe this + customization can be added to the plugin in future. (Contributions welcome!) * However, you can use individual commands to make this work. To see how to setup a release process that takes all of this into account, take a look at the [release.yml GitHub action](../../.github/workflows/release.yml) in this repository. * If you create merge-commits against your release/target branch, then changes to files in your release/target branch become part of the diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index efd776c..f8bfb3c 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -1,7 +1,6 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { Auto, IExtendedCommit, ILogger, IPlugin } from '@auto-it/core' import { execSync } from 'child_process' import * as path from 'path' +import { Auto, IExtendedCommit, ILogger, IPlugin, SEMVER } from '@auto-it/core' function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: IExtendedCommit, logger: ILogger): boolean { if (!commit.pullRequest) { @@ -31,16 +30,29 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { /** The name of the plugin */ name = 'filter-by-workspace-path-plugin' - apply(auto: Auto) { + apply(auto: Auto): void { const currentDir = path.resolve('.') 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 const currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) auto.hooks.onCreateLogParse.tap(this.name, (logParse) => { - logParse.hooks.omitCommit.tap(this.name, (commit) => { - return shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger) === true ? true : undefined - }) + logParse.hooks.omitCommit.tap(this.name, (commit) => + shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger) ? true : undefined + ) + }) + + auto.hooks.onCreateRelease.tap(this.name, (release) => { + const origGetVersion = release.getSemverBump.bind(release) + release.getSemverBump = async (from: string, to?: string): Promise => { + const commits = await release.getCommits(from, to) + if (commits.length === 0) { + auto.logger.verbose.log('No commits found. Skipping release.') + return SEMVER.noVersion + } + return origGetVersion(from, to) + } }) } } diff --git a/packages/filter-by-workspace-path/test/index.spec.ts b/packages/filter-by-workspace-path/test/index.spec.ts index 3b89386..831a445 100644 --- a/packages/filter-by-workspace-path/test/index.spec.ts +++ b/packages/filter-by-workspace-path/test/index.spec.ts @@ -1,12 +1,11 @@ +import * as path from 'path' import Auto, { SEMVER } from '@auto-it/core' import makeCommitFromMsg from '@auto-it/core/dist/__tests__/make-commit-from-msg' import LogParse from '@auto-it/core/dist/log-parse' -import { makeChangelogHooks, makeHooks, makeLogParseHooks, makeReleaseHooks } from '@auto-it/core/dist/utils/make-hooks' -import FilterByPathPlugin from '../src' -import * as path from 'path' +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 Changelog from '@auto-it/core/dist/changelog' +import FilterByPathPlugin from '../src' const setup = () => { const plugin = new FilterByPathPlugin() @@ -14,12 +13,14 @@ const setup = () => { const logger = createLog() const logParseHooks = makeLogParseHooks() const releaseHooks = makeReleaseHooks() - const changeLogHooks = makeChangelogHooks() plugin.apply({ hooks, logger } as Auto) hooks.onCreateLogParse.call({ hooks: logParseHooks } as LogParse) - hooks.onCreateRelease.call({ hooks: releaseHooks } as Release) - hooks.onCreateChangelog.call({ hooks: changeLogHooks } as Changelog, { bump: SEMVER.major }) + hooks.onCreateRelease.call({ + hooks: releaseHooks, + getSemverBump: (from: string, to?: string): Promise => Promise.resolve(SEMVER.patch), + } as Release) + return logParseHooks } From a8aeb1cfea82a5c3467fe2194ec344537d979f49 Mon Sep 17 00:00:00 2001 From: Patrick Ruhkopf Date: Fri, 16 Feb 2024 17:17:25 -0500 Subject: [PATCH 2/2] fix: missing version in changelog file --- .../filter-by-workspace-path/CHANGELOG.md | 37 +++++++++---------- packages/filter-by-workspace-path/README.md | 1 - .../filter-by-workspace-path/src/index.ts | 14 ++++++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/filter-by-workspace-path/CHANGELOG.md b/packages/filter-by-workspace-path/CHANGELOG.md index c232a57..2190939 100644 --- a/packages/filter-by-workspace-path/CHANGELOG.md +++ b/packages/filter-by-workspace-path/CHANGELOG.md @@ -1,18 +1,28 @@ -# (Tue Feb 13 2024) +# v0.1.0 (Fri Feb 16 2024) +#### 🚀 Enhancement + +- feat: omit commits that aren't related to a pull request [#9](https://github.com/restfulhead/npm-auto-plugins/pull/9) ([@restfulhead](https://github.com/restfulhead)) + +#### 📝 Documentation + +- doc: provide more details about the merge commit issue [#8](https://github.com/restfulhead/npm-auto-plugins/pull/8) ([@restfulhead](https://github.com/restfulhead)) +#### Authors: 1 + +- Patrick Ruhkopf ([@restfulhead](https://github.com/restfulhead)) --- -# (Tue Feb 13 2024) +# v0.0.1 (Fri Feb 16 2024) -#### 🚀 Enhancement +#### 🐛 Patch -- chore: fix release [#30](https://github.com/restfulhead/npm-auto-plugins/pull/30) ([@restfulhead](https://github.com/restfulhead)) +- fix: filter out commits in other ws dir with the same prefix [#5](https://github.com/restfulhead/npm-auto-plugins/pull/5) ([@restfulhead](https://github.com/restfulhead)) #### 🔩 Internal -- chore: prepare release [#32](https://github.com/restfulhead/npm-auto-plugins/pull/32) ([@restfulhead](https://github.com/restfulhead)) +- chore: prepare release [#6](https://github.com/restfulhead/npm-auto-plugins/pull/6) ([@restfulhead](https://github.com/restfulhead)) #### Authors: 1 @@ -20,23 +30,12 @@ --- -# (Mon Feb 12 2024) - -#### 🚀 Enhancement - -- fix: workflow and release [#25](https://github.com/restfulhead/npm-auto-plugins/pull/25) ([@restfulhead](https://github.com/restfulhead)) -- fix: cleanup [#23](https://github.com/restfulhead/npm-auto-plugins/pull/23) ([@restfulhead](https://github.com/restfulhead)) - -#### 🐛 Patch - -- fix: test and release workflow [#28](https://github.com/restfulhead/npm-auto-plugins/pull/28) ([@restfulhead](https://github.com/restfulhead)) -- fix: hooks [#21](https://github.com/restfulhead/npm-auto-plugins/pull/21) ([@restfulhead](https://github.com/restfulhead)) +# v0.0.1 (Tue Feb 13 2024) #### 🔩 Internal -- chore: prepare release [#29](https://github.com/restfulhead/npm-auto-plugins/pull/29) ([@restfulhead](https://github.com/restfulhead)) -- chore: prepare release [#27](https://github.com/restfulhead/npm-auto-plugins/pull/27) ([@restfulhead](https://github.com/restfulhead)) -- chore: prepare release [#24](https://github.com/restfulhead/npm-auto-plugins/pull/24) ([@restfulhead](https://github.com/restfulhead)) +- chore: prepare release [#4](https://github.com/restfulhead/npm-auto-plugins/pull/4) ([@restfulhead](https://github.com/restfulhead)) +- refactor: renamed to follow auto's naming convention [#3](https://github.com/restfulhead/npm-auto-plugins/pull/3) ([@restfulhead](https://github.com/restfulhead)) #### Authors: 1 diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index 7d28cba..230d55c 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -30,7 +30,6 @@ However, carefully read the following caveats section. * have `[skip ci]` in their commit message * This plugin modifies the behavior of `auto version`. By default, it seems to set the version to `patch` instead of `noVersion` even if all commits were omitted. With this plugin, `noVersion` is returned if there are no relevant commits. -* While the title in the GitHub release notes is correct, the version is missing in the `CHANGELOG.md` file for currently unknown reasons * You can't use the `shipit` command, because for example, the version in each package should only contain the version number (e.g. `v1.0.0`), but the tag and Github release must include the package name to avoid ambigious release names/tags. Maybe this customization can be added to the plugin in future. (Contributions welcome!) diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index f8bfb3c..a6a6fd0 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -1,6 +1,7 @@ import { execSync } from 'child_process' import * as path from 'path' -import { Auto, IExtendedCommit, ILogger, IPlugin, SEMVER } from '@auto-it/core' +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 { if (!commit.pullRequest) { @@ -11,7 +12,8 @@ function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: const fixedFiles = commit.files.map((file) => path.relative(currentDir, file)) const wsDir = path.join(currentWorkspace, path.sep) - const atLeastOneFileInCurrentDir = fixedFiles.find((file) => file.startsWith(wsDir)) + const atLeastOneFileInCurrentDir = fixedFiles.find((file) => inFolder(wsDir, file)) + if (!atLeastOneFileInCurrentDir) { logger.verbose.log(`All files are outside the current workspace directory ('${wsDir}'). Omitting commit '${commit.hash}'.`) return true @@ -53,6 +55,14 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { } return origGetVersion(from, to) } + + release.calcNextVersion = async (lastTag: string) => { + const bump = await release.getSemverBump(lastTag) + const matches = lastTag.match(/(\d+\.\d+\.\d+)/) + const lastVersion = matches ? matches[0] : lastTag + + return inc(lastVersion, bump as ReleaseType) + } }) } }