From e56a8dc0ce4942508d4c09000f09690553565416 Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Sat, 8 Aug 2020 22:44:50 +0200 Subject: [PATCH 1/9] fix: Git tags empty --- src/helpers/git.js | 2 +- src/version/git.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/git.js b/src/helpers/git.js index de5c185..930a0c2 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -97,7 +97,7 @@ module.exports = new (class Git { * @return {Promise<>} */ pull = () => ( - this.exec(`pull --unshallow ${core.getInput('git-pull-method')}`) + this.exec(`pull ${core.getInput('git-pull-method')}`) ) /** diff --git a/src/version/git.js b/src/version/git.js index b73cc38..0a0743c 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -13,7 +13,7 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, }, (err, tags) => { - const currentVersion = tags.shift().replace(tagPrefix, '') + const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : '0.1.0' // Get the new version this.newVersion = bumpVersion( From c9d402453191a8c3684e7f1c94a07752d402aa4a Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Sun, 9 Aug 2020 12:24:24 +0200 Subject: [PATCH 2/9] revert: "fix: Git tags empty" This reverts commit e56a8dc0ce4942508d4c09000f09690553565416. --- src/helpers/git.js | 2 +- src/version/git.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/git.js b/src/helpers/git.js index 930a0c2..de5c185 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -97,7 +97,7 @@ module.exports = new (class Git { * @return {Promise<>} */ pull = () => ( - this.exec(`pull ${core.getInput('git-pull-method')}`) + this.exec(`pull --unshallow ${core.getInput('git-pull-method')}`) ) /** diff --git a/src/version/git.js b/src/version/git.js index 0a0743c..b73cc38 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -13,7 +13,7 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, }, (err, tags) => { - const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : '0.1.0' + const currentVersion = tags.shift().replace(tagPrefix, '') // Get the new version this.newVersion = bumpVersion( From 3396dfc4323e48de090308fff522ef4c557f73e5 Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Sun, 9 Aug 2020 12:28:05 +0200 Subject: [PATCH 3/9] fix: Pull all tags --- src/helpers/git.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/git.js b/src/helpers/git.js index de5c185..05b5bf1 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -97,7 +97,7 @@ module.exports = new (class Git { * @return {Promise<>} */ pull = () => ( - this.exec(`pull --unshallow ${core.getInput('git-pull-method')}`) + this.exec(`pull --unshallow --tags ${core.getInput('git-pull-method')}`) ) /** From 63d0e46a0b69e3db3f7a5f44e963323afc35d29c Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Sun, 9 Aug 2020 12:37:03 +0200 Subject: [PATCH 4/9] feat: Add fallback version --- .github/workflows/test.yml | 140 +++++++++++++++++++++++++++++++++++++ README.md | 9 ++- action.yml | 5 ++ package-lock.json | 10 +-- package.json | 2 +- src/helpers/bumpVersion.js | 46 ++++++++---- src/version/base.js | 4 +- src/version/git.js | 2 +- src/version/json.js | 9 ++- src/version/toml.js | 31 +++++--- src/version/yaml.js | 29 +++++--- test/test-file-empty.json | 0 test/test-file-empty.toml | 0 test/test-file-empty.yaml | 0 14 files changed, 243 insertions(+), 44 deletions(-) create mode 100644 test/test-file-empty.json create mode 100644 test/test-file-empty.toml create mode 100644 test/test-file-empty.yaml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aec3180..0d59bde 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,47 @@ jobs: with: github-token: ${{ secrets.github_token }} + test-json-new: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'dont-use-git' + with: + github-token: ${{ secrets.github_token }} + version-file: 'test-file-new.json' + + - name: Show file + id: show + run: | + echo "$( { - let [major, minor, patch] = version.split('.') + let major, minor, patch - switch (releaseType) { - case 'major': - major = parseInt(major, 10) + 1 - minor = 0 + if (version) { + [major, minor, patch] = version.split('.') + + switch (releaseType) { + case 'major': + major = parseInt(major, 10) + 1 + minor = 0 + patch = 0 + break + + case 'minor': + minor = parseInt(minor, 10) + 1 + patch = 0 + break + + default: + patch = parseInt(patch, 10) + 1 + } + } else { + let version = semverValid(core.getInput('fallback-version')) + + if (version) { + [major, minor, patch] = version.split('.') + } else { + // default + major = 0 + minor = 1 patch = 0 - break - - case 'minor': - minor = parseInt(minor, 10) + 1 - patch = 0 - break - - default: - patch = parseInt(patch, 10) + 1 + } } return `${major}.${minor}.${patch}` diff --git a/src/version/base.js b/src/version/base.js index 04a172d..9089e29 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -25,7 +25,9 @@ module.exports = class BaseVersioning { * @return {string} */ read = () => { - return fs.readFileSync(this.fileLocation, 'utf8') + if (fs.existsSync(this.fileLocation)) { + return fs.readFileSync(this.fileLocation, 'utf8') + } } /** diff --git a/src/version/git.js b/src/version/git.js index b73cc38..4077c93 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -13,7 +13,7 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, }, (err, tags) => { - const currentVersion = tags.shift().replace(tagPrefix, '') + const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/json.js b/src/version/json.js index 150e9a3..662101f 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,10 +12,15 @@ module.exports = new (class Json extends BaseVersioning { * @return {*} */ bump = (releaseType) => { + let jsonContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const jsonContent = JSON.parse(fileContent) - const oldVersion = objectPath.get(jsonContent, this.versionPath) + if (fileContent) { + jsonContent = JSON.parse(fileContent) + oldVersion = objectPath.get(jsonContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/toml.js b/src/version/toml.js index e164e38..734ff8e 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -1,5 +1,5 @@ const objectPath = require('object-path') -const toml = require('toml') +const toml = require('@iarna/toml') const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') @@ -13,10 +13,15 @@ module.exports = new (class Toml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { + let tomlContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const tomlContent = toml.parse(fileContent) - const oldVersion = objectPath.get(tomlContent, this.versionPath) + if (fileContent) { + tomlContent = toml.parse(fileContent) + oldVersion = objectPath.get(tomlContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( @@ -28,13 +33,19 @@ module.exports = new (class Toml extends BaseVersioning{ const versionName = this.versionPath.split('.').pop() // Update the file - this.update( - // We use replace so we can preserve white spaces and comments - fileContent.replace( - `${versionName} = "${oldVersion}"`, - `${versionName} = "${this.newVersion}"`, - ), - ) + if (fileContent) { + this.update( + // We use replace instead of yaml.stringify so we can preserve white spaces and comments + fileContent.replace( + `${versionName} = "${oldVersion}"`, + `${versionName} = "${this.newVersion}"`, + ), + ) + } else { + // Update the content with the new version + objectPath.set(tomlContent, this.versionPath, this.newVersion) + this.update(toml.stringify(tomlContent)) + } } }) diff --git a/src/version/yaml.js b/src/version/yaml.js index da7db77..133d9c1 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -13,10 +13,15 @@ module.exports = new (class Yaml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { + let yamlContent = {} + let oldVersion + // Read the file const fileContent = this.read() - const yamlContent = yaml.parse(fileContent) - const oldVersion = objectPath.get(yamlContent, this.versionPath) + if (fileContent) { + yamlContent = yaml.parse(fileContent) + oldVersion = objectPath.get(yamlContent, this.versionPath) + } // Get the new version this.newVersion = bumpVersion( @@ -28,13 +33,19 @@ module.exports = new (class Yaml extends BaseVersioning{ const versionName = this.versionPath.split('.').pop() // Update the file - this.update( - // We use replace instead of yaml.stringify so we can preserve white spaces and comments - fileContent.replace( - `${versionName}: '${oldVersion}'`, - `${versionName}: '${this.newVersion}'`, - ), - ) + if (fileContent) { + this.update( + // We use replace instead of yaml.stringify so we can preserve white spaces and comments + fileContent.replace( + `${versionName}: '${oldVersion}'`, + `${versionName}: '${this.newVersion}'`, + ), + ) + } else { + // Update the content with the new version + objectPath.set(yamlContent, this.versionPath, this.newVersion) + this.update(yaml.stringify(yamlContent)) + } } }) diff --git a/test/test-file-empty.json b/test/test-file-empty.json new file mode 100644 index 0000000..e69de29 diff --git a/test/test-file-empty.toml b/test/test-file-empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/test/test-file-empty.yaml b/test/test-file-empty.yaml new file mode 100644 index 0000000..e69de29 From 091fdfc6a55a151e3adff5ada382986ead85d58e Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Mon, 10 Aug 2020 17:11:18 +0200 Subject: [PATCH 5/9] fix: Empty version files --- src/version/base.js | 4 +--- src/version/json.js | 9 ++------- src/version/toml.js | 17 ++++++----------- src/version/yaml.js | 17 ++++++----------- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/version/base.js b/src/version/base.js index 9089e29..c9e142a 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -25,9 +25,7 @@ module.exports = class BaseVersioning { * @return {string} */ read = () => { - if (fs.existsSync(this.fileLocation)) { - return fs.readFileSync(this.fileLocation, 'utf8') - } + return fs.existsSync(this.fileLocation) ? fs.readFileSync(this.fileLocation, 'utf8') : '' } /** diff --git a/src/version/json.js b/src/version/json.js index 662101f..8125079 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,15 +12,10 @@ module.exports = new (class Json extends BaseVersioning { * @return {*} */ bump = (releaseType) => { - let jsonContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - jsonContent = JSON.parse(fileContent) - oldVersion = objectPath.get(jsonContent, this.versionPath) - } + const jsonContent = JSON.parse(fileContent) + const oldVersion = objectPath.get(jsonContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( diff --git a/src/version/toml.js b/src/version/toml.js index 734ff8e..ff889ee 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -13,15 +13,10 @@ module.exports = new (class Toml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { - let tomlContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - tomlContent = toml.parse(fileContent) - oldVersion = objectPath.get(tomlContent, this.versionPath) - } + const tomlContent = toml.parse(fileContent) + const oldVersion = objectPath.get(tomlContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( @@ -29,11 +24,11 @@ module.exports = new (class Toml extends BaseVersioning{ oldVersion, ) - // Get the name of where the version is in - const versionName = this.versionPath.split('.').pop() - // Update the file - if (fileContent) { + if (oldVersion) { + // Get the name of where the version is in + const versionName = this.versionPath.split('.').pop() + this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments fileContent.replace( diff --git a/src/version/yaml.js b/src/version/yaml.js index 133d9c1..c29b9b2 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -13,15 +13,10 @@ module.exports = new (class Yaml extends BaseVersioning{ * @return {*} */ bump = (releaseType) => { - let yamlContent = {} - let oldVersion - // Read the file const fileContent = this.read() - if (fileContent) { - yamlContent = yaml.parse(fileContent) - oldVersion = objectPath.get(yamlContent, this.versionPath) - } + const yamlContent = yaml.parse(fileContent) || {} + const oldVersion = objectPath.get(yamlContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( @@ -29,11 +24,11 @@ module.exports = new (class Yaml extends BaseVersioning{ oldVersion, ) - // Get the name of where the version is in - const versionName = this.versionPath.split('.').pop() - // Update the file - if (fileContent) { + if (oldVersion) { + // Get the name of where the version is in + const versionName = this.versionPath.split('.').pop() + this.update( // We use replace instead of yaml.stringify so we can preserve white spaces and comments fileContent.replace( From 283100b0c8b51e728cf3c1553169172ae432b703 Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Tue, 11 Aug 2020 15:08:10 +0200 Subject: [PATCH 6/9] build: Add semver dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4d20769..3818fb8 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "conventional-recommended-bump": "6.0.9", "git-semver-tags": "4.0.0", "object-path": "^0.11.4", + "semver": "^6.0.0", "yaml": "^1.10.0" }, "devDependencies": { From 9a324dbd51d0d32c1b9df1a291e14cc20a5bbaff Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Tue, 11 Aug 2020 20:02:59 +0200 Subject: [PATCH 7/9] fix: Show info if the version couldn't be detected --- README.md | 2 +- action.yml | 2 +- src/helpers/bumpVersion.js | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b45ad05..e29fd75 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This action will bump version, tag commit and generate a changelog with conventi - **Optional** `skip-version-file`: Do not update the version file. Default `'false'`. - **Optional** `skip-commit`: Do create a release commit. Default `'false'`. - **Optional** `pre-commit`: Path to the pre-commit script file. No hook by default. -- **Optional** `fallback-version`: The fallback version, if none is recognized. Default `'0.1.0'` +- **Optional** `fallback-version`: The fallback version, if no older one can be detected, or if it is the first one. Default `'0.1.0'` ### Pre-Commit hook diff --git a/action.yml b/action.yml index 57143ef..fc44ad1 100644 --- a/action.yml +++ b/action.yml @@ -84,7 +84,7 @@ inputs: required: false fallback-version: - description: 'The fallback version, if none is recognized' + description: 'The fallback version, if no older one can be detected, or if it is the first one' default: '0.1.0' required: false diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js index f3db399..f81410a 100644 --- a/src/helpers/bumpVersion.js +++ b/src/helpers/bumpVersion.js @@ -42,5 +42,7 @@ module.exports = (releaseType, version) => { } } + core.info(`The version could not be detected, the new version is '${major}.${minor}.${patch}'.`) + return `${major}.${minor}.${patch}` } From 97f1bb3543e6f2480ef3e699fc695ecb8b3f881b Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Tue, 11 Aug 2020 20:30:55 +0200 Subject: [PATCH 8/9] fix: Use fallback if it's not a valid JSON-File --- .github/workflows/test.yml | 6 ++++++ src/version/json.js | 17 ++++++++++++++++- test/test-file-empty.json | 0 test/test-file-empty.toml | 0 test/test-file-empty.yaml | 0 5 files changed, 22 insertions(+), 1 deletion(-) delete mode 100644 test/test-file-empty.json delete mode 100644 test/test-file-empty.toml delete mode 100644 test/test-file-empty.yaml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0d59bde..8a088c4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -51,6 +51,8 @@ jobs: with: path: "./" + - run: touch ./test-file-empty.json + - name: Generate changelog id: changelog uses: ./ @@ -147,6 +149,8 @@ jobs: with: path: "./" + - run: touch ./test-file-empty.yaml + - name: Generate changelog id: changelog uses: ./ @@ -225,6 +229,8 @@ jobs: with: path: "./" + - run: touch ./test-file-empty.toml + - name: Generate changelog id: changelog uses: ./ diff --git a/src/version/json.js b/src/version/json.js index 8125079..7a75c52 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -1,3 +1,4 @@ +const core = require('@actions/core') const objectPath = require('object-path') const BaseVersioning = require('./base') @@ -14,7 +15,21 @@ module.exports = new (class Json extends BaseVersioning { bump = (releaseType) => { // Read the file const fileContent = this.read() - const jsonContent = JSON.parse(fileContent) + + // Parse the file + let jsonContent + try { + 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 const oldVersion = objectPath.get(jsonContent, this.versionPath, null) // Get the new version diff --git a/test/test-file-empty.json b/test/test-file-empty.json deleted file mode 100644 index e69de29..0000000 diff --git a/test/test-file-empty.toml b/test/test-file-empty.toml deleted file mode 100644 index e69de29..0000000 diff --git a/test/test-file-empty.yaml b/test/test-file-empty.yaml deleted file mode 100644 index e69de29..0000000 From b525f9ae66cb03aa2a58cd043963504b911bac31 Mon Sep 17 00:00:00 2001 From: Jayrgo <13069032+Jayrgo@users.noreply.github.com> Date: Wed, 12 Aug 2020 17:27:06 +0200 Subject: [PATCH 9/9] fix: Message when using the fallback version --- src/helpers/bumpVersion.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js index f81410a..b7a5882 100644 --- a/src/helpers/bumpVersion.js +++ b/src/helpers/bumpVersion.js @@ -40,9 +40,9 @@ module.exports = (releaseType, version) => { minor = 1 patch = 0 } - } - core.info(`The version could not be detected, the new version is '${major}.${minor}.${patch}'.`) + core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`) + } return `${major}.${minor}.${patch}` }