diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aec3180..8a088c4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,49 @@ 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 "$( { /* ... */ }` - not have any return value @@ -96,7 +98,7 @@ No file changelog uses: TriPSs/conventional-changelog-action@v3 with: github-token: ${{ secrets.github_token }} - output-file: 'false' + output-file: "false" ``` Tag only @@ -106,7 +108,7 @@ Tag only uses: TriPSs/conventional-changelog-action@v3 with: github-token: ${{ secrets.github_token }} - skip-commit: 'true' + skip-commit: "true" ``` Use a custom file for versioning @@ -116,7 +118,7 @@ Use a custom file for versioning uses: TriPSs/conventional-changelog-action@v3 with: github-token: ${{ secrets.github_token }} - version-file: 'my-custom-file.yaml' + version-file: "my-custom-file.yaml" ``` Use a pre-commit hook @@ -137,24 +139,26 @@ Github releases uses: TriPSs/conventional-changelog-action@v3 with: github-token: ${{ secrets.github_token }} - output-file: 'false' - skip-on-empty: 'true' + output-file: "false" - name: Create Release uses: actions/create-release@v1 if: ${{ steps.changelog.outputs.skipped == 'false' }} env: - GITHUB_TOKEN: ${{ secrets.github_token }} + GITHUB_TOKEN: ${{ secrets.github_token }} with: - tag_name: ${{ steps.changelog.outputs.tag }} - release_name: ${{ steps.changelog.outputs.tag }} - body: ${{ steps.changelog.outputs.clean_changelog }} + tag_name: ${{ steps.changelog.outputs.tag }} + release_name: ${{ steps.changelog.outputs.tag }} + body: ${{ steps.changelog.outputs.clean_changelog }} ``` ## Development + If you'd like to contribute to this project, all you need to do is clone and install [act](https://github.com/nektos/act) this project and run: + > Make sure that `main: 'src/index.js'` is updated to `main: '../src/index.js'` inside the `action.yml` > Note: The image used is 18 gb! + ```shell $ yarn install @@ -164,12 +168,18 @@ $ act -j test-json -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s gith # To run / test git versioning $ act -j test-git -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token +# To run / test git fallback versioning +$ act -j test-git-fallback -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token + # To run / test yaml versioning $ act -j test-yaml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token -# To run / toml git versioning +# To run / test toml versioning $ act -j test-toml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token +# To run / test empty / new files test +$ act -j test-[json/toml/yaml]-[empty/new] -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token + # To run pre-commit test $ act -j test-pre-commit -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token diff --git a/action.yml b/action.yml index 1d33fa7..fc44ad1 100644 --- a/action.yml +++ b/action.yml @@ -83,6 +83,11 @@ inputs: description: 'Path to the pre-commit script file' required: false + fallback-version: + description: 'The fallback version, if no older one can be detected, or if it is the first one' + default: '0.1.0' + required: false + outputs: changelog: diff --git a/package-lock.json b/package-lock.json index 737e7c0..9407251 100644 --- a/package-lock.json +++ b/package-lock.json @@ -372,6 +372,11 @@ "find-up": "^4.0.0" } }, + "@iarna/toml": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.5.tgz", + "integrity": "sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==" + }, "@marionebl/sander": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/@marionebl/sander/-/sander-0.6.1.tgz", @@ -2238,11 +2243,6 @@ "readable-stream": "2 || 3" } }, - "toml": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", - "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" - }, "trim-newlines": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-3.0.0.tgz", diff --git a/package.json b/package.json index 7a0dd03..2d92fdc 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,12 @@ "dependencies": { "@actions/core": "1.2.4", "@actions/exec": "1.0.4", + "@iarna/toml": "^2.2.5", "conventional-changelog": "3.1.21", "conventional-recommended-bump": "6.0.9", "git-semver-tags": "4.0.0", "object-path": "^0.11.4", - "toml": "^3.0.0", + "semver": "^6.0.0", "yaml": "^1.10.0" }, "devDependencies": { diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js index ec5d67c..b7a5882 100644 --- a/src/helpers/bumpVersion.js +++ b/src/helpers/bumpVersion.js @@ -1,3 +1,6 @@ +const core = require('@actions/core') +const semverValid = require('semver').valid + /** * Bumps the given version with the given release type * @@ -6,22 +9,39 @@ * @returns {string} */ module.exports = (releaseType, version) => { - 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 + core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`) } return `${major}.${minor}.${patch}` 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')}`) ) /** diff --git a/src/version/base.js b/src/version/base.js index 04a172d..c9e142a 100644 --- a/src/version/base.js +++ b/src/version/base.js @@ -25,7 +25,7 @@ module.exports = class BaseVersioning { * @return {string} */ read = () => { - return fs.readFileSync(this.fileLocation, 'utf8') + return fs.existsSync(this.fileLocation) ? 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..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,8 +15,22 @@ module.exports = new (class Json extends BaseVersioning { bump = (releaseType) => { // Read the file const fileContent = this.read() - const jsonContent = JSON.parse(fileContent) - const oldVersion = objectPath.get(jsonContent, this.versionPath) + + // 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 this.newVersion = bumpVersion( diff --git a/src/version/toml.js b/src/version/toml.js index e164e38..ff889ee 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') @@ -16,7 +16,7 @@ module.exports = new (class Toml extends BaseVersioning{ // Read the file const fileContent = this.read() const tomlContent = toml.parse(fileContent) - const oldVersion = objectPath.get(tomlContent, this.versionPath) + const oldVersion = objectPath.get(tomlContent, this.versionPath, null) // Get the new version this.newVersion = bumpVersion( @@ -24,17 +24,23 @@ 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 - this.update( - // We use replace so we can preserve white spaces and comments - fileContent.replace( - `${versionName} = "${oldVersion}"`, - `${versionName} = "${this.newVersion}"`, - ), - ) + 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( + `${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..c29b9b2 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -15,8 +15,8 @@ module.exports = new (class Yaml extends BaseVersioning{ bump = (releaseType) => { // Read the file const fileContent = this.read() - const yamlContent = yaml.parse(fileContent) - const 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( @@ -24,17 +24,23 @@ 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 - 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 (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( + `${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)) + } } })