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] 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