diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0ffc2ddd..d07d7f99 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -338,6 +338,125 @@ jobs: skip-tag: 'true' git-push: 'false' + test-skip-bump: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + path: "./" + + - run: npm ci --prod + - run: npm run build + + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + + - run: "git add . && git commit --allow-empty -m 'fix: Added fake file so version would be bumped'" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'dont-use-git' + EXPECTED_TAG: "v1.4.5" + with: + github-token: ${{ secrets.github_token }} + version-file: "test-file.json" + skip-bump: "true" + + - name: Show file + run: | + echo "$( { this.fileLocation = fileLocation this.versionPath = versionPath + this.parseFile() + } + + /** + * Abstract method for parsing the file + */ + parseFile = () => { + throw new Error('Implement parseFile logic in class!') } /** @@ -24122,7 +24131,7 @@ module.exports = class BaseVersioning { * * @return {string} */ - read = () => { + readFile = () => { if (fs.existsSync(this.fileLocation)) { return fs.readFileSync(this.fileLocation, 'utf8') } @@ -24169,6 +24178,13 @@ const bumpVersion = __nccwpck_require__(1581) module.exports = class Git extends BaseVersioning { + /** + * Left empty to override the parent's abstract method, which would throw an error + */ + parseFile = () => { + + } + bump = async(releaseType) => { const tagPrefix = core.getInput('tag-prefix') const prerelease = core.getBooleanInput('pre-release') @@ -24197,7 +24213,7 @@ const Yaml = __nccwpck_require__(5731) const Toml = __nccwpck_require__(3636) const Mix = __nccwpck_require__(1731) -module.exports = (fileExtension) => { +module.exports = (fileExtension, filePath) => { switch (fileExtension.toLowerCase()) { case 'json': return new Json() @@ -24216,7 +24232,7 @@ module.exports = (fileExtension) => { return new Mix() default: - return null + throw new Error(`File extension "${fileExtension}" from file "${filePath}" is not supported`) } } @@ -24234,33 +24250,38 @@ const bumpVersion = __nccwpck_require__(1581) module.exports = class Json extends BaseVersioning { + eol = null + jsonContent = {} + /** - * Bumps the version in the package.json - * - * @param {!string} releaseType - The type of release - * @return {*} + * Reads and parses the json file */ - bump = async (releaseType) => { + parseFile = () => { // Read the file - const fileContent = this.read() + const fileContent = this.readFile() // Parse the file - let jsonContent - let eol = fileContent.endsWith('\n') ? '\n' : '' + this.eol = fileContent.endsWith('\n') ? '\n' : '' try { - jsonContent = JSON.parse(fileContent) + this.jsonContent = JSON.parse(fileContent) } catch (error) { core.startGroup(`Error when parsing the file '${this.fileLocation}'`) core.info(`File-Content: ${fileContent}`) core.info(error) // should be 'warning' ? core.endGroup() - - jsonContent = {} } // Get the old version - this.oldVersion = objectPath.get(jsonContent, this.versionPath, null) + this.oldVersion = objectPath.get(this.jsonContent, this.versionPath, null) + } + /** + * Bumps the version in the package.json + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = async (releaseType) => { // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -24270,11 +24291,11 @@ module.exports = class Json extends BaseVersioning { core.info(`Bumped file "${this.fileLocation}" from "${this.oldVersion}" to "${this.newVersion}"`) // Update the content with the new version - objectPath.set(jsonContent, this.versionPath, this.newVersion) + objectPath.set(this.jsonContent, this.versionPath, this.newVersion) // Update the file this.update( - JSON.stringify(jsonContent, null, 2) + eol + JSON.stringify(this.jsonContent, null, 2) + this.eol ) } @@ -24291,30 +24312,39 @@ const BaseVersioning = __nccwpck_require__(3030) const bumpVersion = __nccwpck_require__(1581) module.exports = class Mix extends BaseVersioning { + + fileContent = null + /** - * Bumps the version in the package.json - * - * @param {!string} releaseType - The type of release - * @return {*} + * Reads and parses the mix file */ - bump = async(releaseType) => { + parseFile = () => { // Read the file - const fileContent = this.read() + this.fileContent = this.readFile() - const [_, oldVersion] = fileContent.match(/version: "([0-9.]+)"/i) + // Parse the file + const [_, oldVersion] = this.fileContent.match(/version: "([0-9.]+)"/i) this.oldVersion = oldVersion if (!this.oldVersion) { throw new Error(`Failed to extract mix project version.`) } + } + /** + * Bumps the version in the package.json + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = async(releaseType) => { this.newVersion = await bumpVersion( releaseType, this.oldVersion ) this.update( - fileContent.replace(`version: "${this.oldVersion}"`, `version: "${this.newVersion}"`) + this.fileContent.replace(`version: "${this.oldVersion}"`, `version: "${this.newVersion}"`) ) } } @@ -24334,6 +24364,21 @@ const bumpVersion = __nccwpck_require__(1581) module.exports = class Toml extends BaseVersioning { + tomlContent = null + fileContent = null + + /** + * Reads and parses the toml file + */ + parseFile = () => { + // Read the file + this.fileContent = this.readFile() + + // Parse the file + this.tomlContent = toml.parse(this.fileContent) + this.oldVersion = objectPath.get(this.tomlContent, this.versionPath, null) + } + /** * Bumps the version in the package.json * @@ -24341,11 +24386,6 @@ module.exports = class Toml extends BaseVersioning { * @return {*} */ bump = async (releaseType) => { - // Read the file - const fileContent = this.read() - const tomlContent = toml.parse(fileContent) - this.oldVersion = objectPath.get(tomlContent, this.versionPath, null) - // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -24361,15 +24401,15 @@ module.exports = class Toml extends BaseVersioning { this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments - fileContent.replace( + this.fileContent.replace( `${versionName} = "${this.oldVersion}"`, `${versionName} = "${this.newVersion}"`, ), ) } else { // Update the content with the new version - objectPath.set(tomlContent, this.versionPath, this.newVersion) - this.update(toml.stringify(tomlContent)) + objectPath.set(this.tomlContent, this.versionPath, this.newVersion) + this.update(toml.stringify(this.tomlContent)) } } @@ -24391,6 +24431,21 @@ const bumpVersion = __nccwpck_require__(1581) module.exports = class Yaml extends BaseVersioning { + fileContent = null + yamlContent = null + + /** + * Reads and parses the yaml file + */ + parseFile = () => { + // Read the file + this.fileContent = this.readFile() + + // Parse the file + this.yamlContent = yaml.parse(this.fileContent) || {} + this.oldVersion = objectPath.get(this.yamlContent, this.versionPath, null) + } + /** * Bumps the version in the package.json * @@ -24398,11 +24453,6 @@ module.exports = class Yaml extends BaseVersioning { * @return {*} */ bump = async (releaseType) => { - // Read the file - const fileContent = this.read() - const yamlContent = yaml.parse(fileContent) || {} - this.oldVersion = objectPath.get(yamlContent, this.versionPath, null) - // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -24419,7 +24469,7 @@ module.exports = class Yaml extends BaseVersioning { this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments // Replace if version was used with single quotes - fileContent.replace( + this.fileContent.replace( `${versionName}: '${this.oldVersion}'`, `${versionName}: '${this.newVersion}'`, ).replace( // Replace if version was used with double quotes @@ -24432,8 +24482,8 @@ module.exports = class Yaml extends BaseVersioning { ) } else { // Update the content with the new version - objectPath.set(yamlContent, this.versionPath, this.newVersion) - this.update(yaml.stringify(yamlContent)) + objectPath.set(this.yamlContent, this.versionPath, this.newVersion) + this.update(yaml.stringify(this.yamlContent)) } } @@ -34738,18 +34788,20 @@ const changelog = __nccwpck_require__(1749) const requireScript = __nccwpck_require__(4492) const { loadPreset, loadPresetConfig } = __nccwpck_require__(6921) -async function handleVersioningByExtension(ext, file, versionPath, releaseType) { - const versioning = getVersioning(ext) - - // File type isn't supported - if (versioning === null) { - throw new Error(`File extension "${ext}" from file "${file}" is not supported`) - } +async function handleVersioningByExtension(ext, file, versionPath, releaseType, skipBump) { + const fileLocation = path.resolve(process.cwd(), file) + const versioning = getVersioning(ext, fileLocation) - versioning.init(path.resolve(process.cwd(), file), versionPath) + versioning.init(fileLocation, versionPath) // Bump the version in the package.json - await versioning.bump(releaseType) + if(skipBump){ + // If we are skipping the bump, we either use the old version or alternatively the fallback version + const fallbackVersion = core.getInput('fallback-version') + versioning.newVersion = versioning.oldVersion || fallbackVersion + } else { + await versioning.bump(releaseType) + } return versioning } @@ -34781,6 +34833,7 @@ async function run() { const skipCi = core.getBooleanInput('skip-ci') const createSummary = core.getBooleanInput('create-summary') const prerelease = core.getBooleanInput('pre-release') + const skipBump = core.getBooleanInput('skip-bump') if (skipCi) { gitCommitMessage += ' [skip ci]' @@ -34812,6 +34865,10 @@ async function run() { core.info(`Using "${preChangelogGenerationFile}" as pre-changelog-generation script`) } + if(skipBump) { + core.info('Skipping bumping the version') + } + core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`) core.info(`Skipping the update of the version file is "${skipVersionFile ? 'enabled' : 'disabled'}"`) @@ -34850,12 +34907,12 @@ async function run() { 'git', versionFile, versionPath, - recommendation.releaseType + recommendation.releaseType, + skipBump ) - newVersion = versioning.newVersion oldVersion = versioning.oldVersion - + newVersion = versioning.newVersion } else { const files = versionFile.split(',').map((f) => f.trim()) core.info(`Files to bump: ${files.join(', ')}`) @@ -34865,12 +34922,11 @@ async function run() { const fileExtension = file.split('.').pop() core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`) - return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType) + return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType, skipBump) }) ) - - newVersion = versioning[0].newVersion oldVersion = versioning[0].oldVersion + newVersion = versioning[0].newVersion } let gitTag = `${tagPrefix}${newVersion}` diff --git a/package-lock.json b/package-lock.json index 04b55d9d..2e01e443 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "conventional-changelog-action", - "version": "5.0.0", + "version": "5.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "conventional-changelog-action", - "version": "5.0.0", + "version": "5.1.0", "license": "MIT", "dependencies": { "@actions/core": "^1.10.1", diff --git a/src/index.js b/src/index.js index 9307a28e..c06d0d1b 100644 --- a/src/index.js +++ b/src/index.js @@ -8,18 +8,20 @@ const changelog = require('./helpers/generateChangelog') const requireScript = require('./helpers/requireScript') const { loadPreset, loadPresetConfig } = require('./helpers/load-preset') -async function handleVersioningByExtension(ext, file, versionPath, releaseType) { - const versioning = getVersioning(ext) +async function handleVersioningByExtension(ext, file, versionPath, releaseType, skipBump) { + const fileLocation = path.resolve(process.cwd(), file) + const versioning = getVersioning(ext, fileLocation) - // File type isn't supported - if (versioning === null) { - throw new Error(`File extension "${ext}" from file "${file}" is not supported`) - } - - versioning.init(path.resolve(process.cwd(), file), versionPath) + versioning.init(fileLocation, versionPath) // Bump the version in the package.json - await versioning.bump(releaseType) + if(skipBump){ + // If we are skipping the bump, we either use the old version or alternatively the fallback version + const fallbackVersion = core.getInput('fallback-version') + versioning.newVersion = versioning.oldVersion || fallbackVersion + } else { + await versioning.bump(releaseType) + } return versioning } @@ -51,6 +53,7 @@ async function run() { const skipCi = core.getBooleanInput('skip-ci') const createSummary = core.getBooleanInput('create-summary') const prerelease = core.getBooleanInput('pre-release') + const skipBump = core.getBooleanInput('skip-bump') if (skipCi) { gitCommitMessage += ' [skip ci]' @@ -82,6 +85,10 @@ async function run() { core.info(`Using "${preChangelogGenerationFile}" as pre-changelog-generation script`) } + if(skipBump) { + core.info('Skipping bumping the version') + } + core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`) core.info(`Skipping the update of the version file is "${skipVersionFile ? 'enabled' : 'disabled'}"`) @@ -120,12 +127,12 @@ async function run() { 'git', versionFile, versionPath, - recommendation.releaseType + recommendation.releaseType, + skipBump ) - newVersion = versioning.newVersion oldVersion = versioning.oldVersion - + newVersion = versioning.newVersion } else { const files = versionFile.split(',').map((f) => f.trim()) core.info(`Files to bump: ${files.join(', ')}`) @@ -135,12 +142,11 @@ async function run() { const fileExtension = file.split('.').pop() core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`) - return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType) + return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType, skipBump) }) ) - - newVersion = versioning[0].newVersion oldVersion = versioning[0].oldVersion + newVersion = versioning[0].newVersion } let gitTag = `${tagPrefix}${newVersion}` diff --git a/src/version/base.js b/src/version/base.js index dc341cbe..c4b3909f 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -10,6 +10,7 @@ module.exports = class BaseVersioning { newVersion = null oldVersion = null + /** * Set some basic configurations * @@ -19,6 +20,14 @@ module.exports = class BaseVersioning { init = (fileLocation, versionPath) => { this.fileLocation = fileLocation this.versionPath = versionPath + this.parseFile() + } + + /** + * Abstract method for parsing the file + */ + parseFile = () => { + throw new Error('Implement parseFile logic in class!') } /** @@ -26,7 +35,7 @@ module.exports = class BaseVersioning { * * @return {string} */ - read = () => { + readFile = () => { if (fs.existsSync(this.fileLocation)) { return fs.readFileSync(this.fileLocation, 'utf8') } diff --git a/src/version/git.js b/src/version/git.js index 5d6ec091..464b9240 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -6,6 +6,13 @@ const bumpVersion = require('../helpers/bumpVersion') module.exports = class Git extends BaseVersioning { + /** + * Left empty to override the parent's abstract method, which would throw an error + */ + parseFile = () => { + + } + bump = async(releaseType) => { const tagPrefix = core.getInput('tag-prefix') const prerelease = core.getBooleanInput('pre-release') diff --git a/src/version/index.js b/src/version/index.js index a82b3b51..7c9e8061 100644 --- a/src/version/index.js +++ b/src/version/index.js @@ -4,7 +4,7 @@ const Yaml = require('./yaml') const Toml = require('./toml') const Mix = require('./mix') -module.exports = (fileExtension) => { +module.exports = (fileExtension, filePath) => { switch (fileExtension.toLowerCase()) { case 'json': return new Json() @@ -23,6 +23,6 @@ module.exports = (fileExtension) => { return new Mix() default: - return null + throw new Error(`File extension "${fileExtension}" from file "${filePath}" is not supported`) } } diff --git a/src/version/json.js b/src/version/json.js index 71e72a1e..043269cb 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -6,33 +6,38 @@ const bumpVersion = require('../helpers/bumpVersion') module.exports = class Json extends BaseVersioning { + eol = null + jsonContent = {} + /** - * Bumps the version in the package.json - * - * @param {!string} releaseType - The type of release - * @return {*} + * Reads and parses the json file */ - bump = async (releaseType) => { + parseFile = () => { // Read the file - const fileContent = this.read() + const fileContent = this.readFile() // Parse the file - let jsonContent - let eol = fileContent.endsWith('\n') ? '\n' : '' + this.eol = fileContent.endsWith('\n') ? '\n' : '' try { - jsonContent = JSON.parse(fileContent) + this.jsonContent = JSON.parse(fileContent) } catch (error) { core.startGroup(`Error when parsing the file '${this.fileLocation}'`) core.info(`File-Content: ${fileContent}`) core.info(error) // should be 'warning' ? core.endGroup() - - jsonContent = {} } // Get the old version - this.oldVersion = objectPath.get(jsonContent, this.versionPath, null) + this.oldVersion = objectPath.get(this.jsonContent, this.versionPath, null) + } + /** + * Bumps the version in the package.json + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = async (releaseType) => { // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -42,11 +47,11 @@ module.exports = class Json extends BaseVersioning { core.info(`Bumped file "${this.fileLocation}" from "${this.oldVersion}" to "${this.newVersion}"`) // Update the content with the new version - objectPath.set(jsonContent, this.versionPath, this.newVersion) + objectPath.set(this.jsonContent, this.versionPath, this.newVersion) // Update the file this.update( - JSON.stringify(jsonContent, null, 2) + eol + JSON.stringify(this.jsonContent, null, 2) + this.eol ) } diff --git a/src/version/mix.js b/src/version/mix.js index 58bed964..6738014d 100644 --- a/src/version/mix.js +++ b/src/version/mix.js @@ -2,30 +2,39 @@ const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') module.exports = class Mix extends BaseVersioning { + + fileContent = null + /** - * Bumps the version in the package.json - * - * @param {!string} releaseType - The type of release - * @return {*} + * Reads and parses the mix file */ - bump = async(releaseType) => { + parseFile = () => { // Read the file - const fileContent = this.read() + this.fileContent = this.readFile() - const [_, oldVersion] = fileContent.match(/version: "([0-9.]+)"/i) + // Parse the file + const [_, oldVersion] = this.fileContent.match(/version: "([0-9.]+)"/i) this.oldVersion = oldVersion if (!this.oldVersion) { throw new Error(`Failed to extract mix project version.`) } + } + /** + * Bumps the version in the package.json + * + * @param {!string} releaseType - The type of release + * @return {*} + */ + bump = async(releaseType) => { this.newVersion = await bumpVersion( releaseType, this.oldVersion ) this.update( - fileContent.replace(`version: "${this.oldVersion}"`, `version: "${this.newVersion}"`) + this.fileContent.replace(`version: "${this.oldVersion}"`, `version: "${this.newVersion}"`) ) } } diff --git a/src/version/toml.js b/src/version/toml.js index 84b35ae3..b43fae03 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -7,6 +7,21 @@ const bumpVersion = require('../helpers/bumpVersion') module.exports = class Toml extends BaseVersioning { + tomlContent = null + fileContent = null + + /** + * Reads and parses the toml file + */ + parseFile = () => { + // Read the file + this.fileContent = this.readFile() + + // Parse the file + this.tomlContent = toml.parse(this.fileContent) + this.oldVersion = objectPath.get(this.tomlContent, this.versionPath, null) + } + /** * Bumps the version in the package.json * @@ -14,11 +29,6 @@ module.exports = class Toml extends BaseVersioning { * @return {*} */ bump = async (releaseType) => { - // Read the file - const fileContent = this.read() - const tomlContent = toml.parse(fileContent) - this.oldVersion = objectPath.get(tomlContent, this.versionPath, null) - // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -34,15 +44,15 @@ module.exports = class Toml extends BaseVersioning { this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments - fileContent.replace( + this.fileContent.replace( `${versionName} = "${this.oldVersion}"`, `${versionName} = "${this.newVersion}"`, ), ) } else { // Update the content with the new version - objectPath.set(tomlContent, this.versionPath, this.newVersion) - this.update(toml.stringify(tomlContent)) + objectPath.set(this.tomlContent, this.versionPath, this.newVersion) + this.update(toml.stringify(this.tomlContent)) } } diff --git a/src/version/yaml.js b/src/version/yaml.js index 2a335019..5a8214c0 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -7,6 +7,21 @@ const bumpVersion = require('../helpers/bumpVersion') module.exports = class Yaml extends BaseVersioning { + fileContent = null + yamlContent = null + + /** + * Reads and parses the yaml file + */ + parseFile = () => { + // Read the file + this.fileContent = this.readFile() + + // Parse the file + this.yamlContent = yaml.parse(this.fileContent) || {} + this.oldVersion = objectPath.get(this.yamlContent, this.versionPath, null) + } + /** * Bumps the version in the package.json * @@ -14,11 +29,6 @@ module.exports = class Yaml extends BaseVersioning { * @return {*} */ bump = async (releaseType) => { - // Read the file - const fileContent = this.read() - const yamlContent = yaml.parse(fileContent) || {} - this.oldVersion = objectPath.get(yamlContent, this.versionPath, null) - // Get the new version this.newVersion = await bumpVersion( releaseType, @@ -35,7 +45,7 @@ module.exports = class Yaml extends BaseVersioning { this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments // Replace if version was used with single quotes - fileContent.replace( + this.fileContent.replace( `${versionName}: '${this.oldVersion}'`, `${versionName}: '${this.newVersion}'`, ).replace( // Replace if version was used with double quotes @@ -48,8 +58,8 @@ module.exports = class Yaml extends BaseVersioning { ) } else { // Update the content with the new version - objectPath.set(yamlContent, this.versionPath, this.newVersion) - this.update(yaml.stringify(yamlContent)) + objectPath.set(this.yamlContent, this.versionPath, this.newVersion) + this.update(yaml.stringify(this.yamlContent)) } }