absolute-version / commit-and-tag-version

Fork of the excellent standard-version. Automate versioning and CHANGELOG generation, with semver.org and conventionalcommits.org :trophy:
ISC License
385 stars 36 forks source link

Only bump and create changelog if there ir a breaking change, feature or fix #99

Open hazzo opened 12 months ago

hazzo commented 12 months ago

This PR kind of solves #43 because removes completely the possibility from the package the ability of bumping file version and updating changelog if there are no commits that follow conventional commits.

Let me know if there is something I could add to explain what's the code doing. But what mainly is happening it's that I copied the whatBump function from the preset of conventional-changelog-preset-loader and tweaked to return an empty release if there are no breaking, feature or fixes in the commits since last release.

This repo would greatly benefit from an updated ecma code version or even typescript, and updating dependencies.

hazzo commented 12 months ago

If someone it's willing to try this without using my forked repo here its a .patch file to apply with patch-package

commit-and-tag-version+11.2.3.patch ``` diff --git a/node_modules/commit-and-tag-version/index.js b/node_modules/commit-and-tag-version/index.js index 8941e65..5286607 100755 --- a/node_modules/commit-and-tag-version/index.js +++ b/node_modules/commit-and-tag-version/index.js @@ -82,6 +82,7 @@ module.exports = async function standardVersion (argv) { } const newVersion = await bump(args, version) + if(!newVersion) return; await changelog(args, newVersion) await commit(args, newVersion) await tag(newVersion, pkg ? pkg.private : false, args) diff --git a/node_modules/commit-and-tag-version/lib/lifecycles/bump.js b/node_modules/commit-and-tag-version/lib/lifecycles/bump.js index da72199..3122da3 100644 --- a/node_modules/commit-and-tag-version/lib/lifecycles/bump.js +++ b/node_modules/commit-and-tag-version/lib/lifecycles/bump.js @@ -12,19 +12,64 @@ const runLifecycleScript = require('../run-lifecycle-script') const semver = require('semver') const writeFile = require('../write-file') const { resolveUpdaterObjectFromArgument } = require('../updaters') +const addBangNotes = require('conventional-changelog-conventionalcommits/add-bang-notes') let configsToUpdate = {} +function _whatBump(config, commits) { + let level = 2 + let breakings = 0 + let features = 0 + let fix = 0 + + commits.forEach(commit => { + addBangNotes(commit) + if (commit.notes.length > 0) { + breakings += commit.notes.length + level = 0 + } else if (commit.type === 'feat' || commit.type === 'feature') { + features += 1 + if (level === 2) { + level = 1 + } + } + if (commit.type === 'fix') { + fix += 1 + } + }) + + if (config.preMajor && level < 2) { + level++ + } + + if(!breakings && !features && !fix) return {} + + return { + level, + reason: breakings === 1 + ? `There is ${breakings} BREAKING CHANGE and ${features} features` + : `There are ${breakings} BREAKING CHANGES and ${features} features` + } +} + async function Bump (args, version) { // reset the cache of updated config files each // time we perform the version bump step. configsToUpdate = {} - if (args.skip.bump) return version let newVersion = version await runLifecycleScript(args, 'prerelease') const stdout = await runLifecycleScript(args, 'prebump') if (stdout && stdout.trim().length) args.releaseAs = stdout.trim() const release = await bumpVersion(args.releaseAs, version, args) + console.log(release) + if(!Object.keys(release).length) { + checkpoint( + args, + 'no commits found, so not bumping version', + [] + ) + return null; + } if (!args.firstRelease) { const releaseType = getReleaseType(args.prerelease, release.releaseType, version) const releaseTypeAsVersion = releaseType === 'pre' + release.releaseType ? semver.valid(release.releaseType + '-' + args.prerelease + '.0') : semver.valid(releaseType) @@ -108,7 +153,7 @@ function getTypePriority (type) { } function bumpVersion (releaseAs, currentVersion, args) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { if (releaseAs) { return resolve({ releaseType: releaseAs @@ -123,7 +168,8 @@ function bumpVersion (releaseAs, currentVersion, args) { preset: presetOptions, path: args.path, tagPrefix: args.tagPrefix, - lernaPackage: args.lernaPackage + lernaPackage: args.lernaPackage, + whatBump(commits) { return _whatBump(presetOptions, commits) } }, args.parserOpts, function (err, release) { if (err) return reject(err) else return resolve(release) ```
hazzo commented 12 months ago

Thank you so much! This is an often requested feature

It would be great to add tests, and I think the behaviour should be behind a config flag (otherwise it’s a breaking change).

Cool, I could add tests and make it a config flag. Maybe bumpIfChanges and make it a boolean?

TimothyJones commented 11 months ago

Sorry for the slow reply, I think bumpIfChanges is a bit ambiguous - what about bumpOnlyWhenChangelog or noBumpWhenEmptyChanges or something, both defaulting to false?

TimothyJones commented 11 months ago

Looks like some lint failed too. Should be easily fixed with a --fix, sorry I don't have this automation set up.

This repo would greatly benefit from an updated ecma code version or even typescript, and updating dependencies.

Definitely.

hazzo commented 11 months ago

Sorry for the slow reply, I think bumpIfChanges is a bit ambiguous - what about bumpOnlyWhenChangelog or noBumpWhenEmptyChanges or something, both defaulting to false?

Perfect noBumpWhenEmptyChanges works for me. I'll work on this PR this weekend (add tests and lint fixes also).

hazzo commented 11 months ago

@TimothyJones added added config flag and linted files. I tried to add tests, but I'm struggling to make the test go through the whatBump branch. My thought is that maybe conventionalRecommendedBump it's being mocked and that's why it does not go through my custom whatBump function. Could you point me into the right direction of how should I test this feature? I also don't get how git history it's mocked to get commits.

codecov-commenter commented 11 months ago

Codecov Report

Merging #99 (e936987) into master (1912775) will decrease coverage by 1.49%. Report is 5 commits behind head on master. The diff coverage is 23.07%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
- Coverage   97.57%   96.09%   -1.49%     
==========================================
  Files          31       31              
  Lines        1280     1305      +25     
==========================================
+ Hits         1249     1254       +5     
- Misses         31       51      +20     
Files Coverage Δ
command.js 84.61% <ø> (ø)
index.js 100.00% <100.00%> (ø)
lib/lifecycles/bump.js 78.12% <20.00%> (-20.49%) :arrow_down:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

cbou commented 4 months ago

What is the status of this PR? Will it land?

Rankarusu commented 3 months ago

Hi, are there any updates on this?

TimothyJones commented 3 months ago

As above, I think it needs a little more work - if no one has done it by the next time I have a block of free time, I’ll look in to it.