Skip to content

Commit

Permalink
feat: set version to 'noVersion' if no commits are in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
restfulhead committed Feb 16, 2024
1 parent 20a298b commit 70ad84b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 48 deletions.
51 changes: 25 additions & 26 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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() }}
Expand Down
29 changes: 27 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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_internals>/**",
"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,
Expand All @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -90,6 +88,10 @@ module.exports = {
format: ['camelCase'],
leadingUnderscore: 'forbid',
},
{
selector: 'import',
format: ['camelCase', 'PascalCase'],
},
{
selector: 'default',
format: ['camelCase'],
Expand Down
10 changes: 5 additions & 5 deletions packages/filter-by-workspace-path/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions packages/filter-by-workspace-path/src/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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<SEMVER> => {
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)
}
})
}
}
15 changes: 8 additions & 7 deletions packages/filter-by-workspace-path/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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()
const hooks = makeHooks()
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<SEMVER> => Promise.resolve(SEMVER.patch),
} as Release)

return logParseHooks
}

Expand Down

0 comments on commit 70ad84b

Please sign in to comment.