diff --git a/README.md b/README.md index f65b0a0..2efbe85 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,10 @@ In order to use this action, you will need to generate a JSON file using the fol **Optional** Ignore errors when the provided repo-token does not have write permissions. Default: "false". +### `ignore-missing-file` + +**Optional** Ignore if the file which contains annotations is missing. Default: "true". + ## Example usage ```yml diff --git a/action.yml b/action.yml index c893e0a..238c8a7 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,10 @@ inputs: description: 'Ignore errors when the provided repo-token does not have write permissions' required: false default: 'false' + ignore-missing-file: + description: 'Ignore if the file which contains annotations is missing' + required: false + default: 'true' runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 2d12e60..e5a95e1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -385,9 +385,26 @@ const generateConclusion = function (failureCount, warningCount, noticeCount) { return conclusion } +const booleanValue = function (input) { + return /^\s*(true|1)\s*$/i.test(input) +} + +const readAnnotationsFile= async function(inputPath) { + const ignoreMissingFileValue = core.getInput('ignore-missing-file', { required: false }) || 'true' + const ignoreMissingFile = booleanValue(ignoreMissingFileValue) + try { + const inputContent = await fs.readFile(inputPath, 'utf8') + return JSON.parse(inputContent) + } catch (err) { + if (err.code === 'ENOENT' && ignoreMissingFile) { + core.info(`Ignoring missing file at '${inputPath}' because \'ignore-missing-file\' is true`) + } else { + throw err + } + } +} + async function run () { - const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false' - const ignoreUnauthorizedError = /^\s*(true|1)\s*$/i.test(ignoreUnauthorizedErrorValue) try { const repoToken = core.getInput('repo-token', { required: true }) const inputPath = core.getInput('input', { required: true }) @@ -398,8 +415,7 @@ async function run () { const owner = github.context.repo.owner const repo = github.context.repo.repo - const inputContent = await fs.readFile(inputPath, 'utf8') - const annotations = JSON.parse(inputContent) + const annotations = await readAnnotationsFile(inputPath) const checkRunId = await createCheck(octokit, owner, repo, title, ref) const { failureCount, warningCount, noticeCount } = stats(annotations) core.info(`Found ${failureCount} failure(s), ${warningCount} warning(s) and ${noticeCount} notice(s)`) @@ -428,6 +444,8 @@ async function run () { await updateCheck(octokit, owner, repo, checkRunId, conclusion, title, summary, annotations) } } catch (error) { + const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false' + const ignoreUnauthorizedError = booleanValue(ignoreUnauthorizedErrorValue) if (error.name === 'GitHubApiUnauthorizedError' && ignoreUnauthorizedError) { core.info(`Ignoring the following unauthorized error because 'ignore-unauthorized-error' is true: ${error}`) return diff --git a/index.js b/index.js index d463ea1..4a711c3 100644 --- a/index.js +++ b/index.js @@ -112,9 +112,26 @@ const generateConclusion = function (failureCount, warningCount, noticeCount) { return conclusion } +const booleanValue = function (input) { + return /^\s*(true|1)\s*$/i.test(input) +} + +const readAnnotationsFile= async function(inputPath) { + const ignoreMissingFileValue = core.getInput('ignore-missing-file', { required: false }) || 'true' + const ignoreMissingFile = booleanValue(ignoreMissingFileValue) + try { + const inputContent = await fs.readFile(inputPath, 'utf8') + return JSON.parse(inputContent) + } catch (err) { + if (err.code === 'ENOENT' && ignoreMissingFile) { + core.info(`Ignoring missing file at '${inputPath}' because \'ignore-missing-file\' is true`) + } else { + throw err + } + } +} + async function run () { - const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false' - const ignoreUnauthorizedError = /^\s*(true|1)\s*$/i.test(ignoreUnauthorizedErrorValue) try { const repoToken = core.getInput('repo-token', { required: true }) const inputPath = core.getInput('input', { required: true }) @@ -125,8 +142,7 @@ async function run () { const owner = github.context.repo.owner const repo = github.context.repo.repo - const inputContent = await fs.readFile(inputPath, 'utf8') - const annotations = JSON.parse(inputContent) + const annotations = await readAnnotationsFile(inputPath) const checkRunId = await createCheck(octokit, owner, repo, title, ref) const { failureCount, warningCount, noticeCount } = stats(annotations) core.info(`Found ${failureCount} failure(s), ${warningCount} warning(s) and ${noticeCount} notice(s)`) @@ -155,6 +171,8 @@ async function run () { await updateCheck(octokit, owner, repo, checkRunId, conclusion, title, summary, annotations) } } catch (error) { + const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false' + const ignoreUnauthorizedError = booleanValue(ignoreUnauthorizedErrorValue) if (error.name === 'GitHubApiUnauthorizedError' && ignoreUnauthorizedError) { core.info(`Ignoring the following unauthorized error because 'ignore-unauthorized-error' is true: ${error}`) return