Skip to content

Commit fad0bc6

Browse files
committed
Add ignore-missing-file to control if the task should fail if the input file is missing
1 parent 3eac6f7 commit fad0bc6

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ In order to use this action, you will need to generate a JSON file using the fol
3636

3737
**Optional** Ignore errors when the provided repo-token does not have write permissions. Default: "false".
3838

39+
### `ignore-missing-file`
40+
41+
**Optional** Ignore if the file which contains annotations is missing. Default: "true".
42+
3943
## Example usage
4044

4145
```yml

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ inputs:
1919
description: 'Ignore errors when the provided repo-token does not have write permissions'
2020
required: false
2121
default: 'false'
22+
ignore-missing-file:
23+
description: 'Ignore if the file which contains annotations is missing'
24+
required: false
25+
default: 'true'
2226
runs:
2327
using: 'node12'
2428
main: 'dist/index.js'

dist/index.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,26 @@ const generateConclusion = function (failureCount, warningCount, noticeCount) {
385385
return conclusion
386386
}
387387

388+
const booleanValue = function (input) {
389+
return /^\s*(true|1)\s*$/i.test(input)
390+
}
391+
392+
const readAnnotationsFile= async function(inputPath) {
393+
const ignoreMissingFileValue = core.getInput('ignore-missing-file', { required: false }) || 'true'
394+
const ignoreMissingFile = booleanValue(ignoreMissingFileValue)
395+
try {
396+
const inputContent = await fs.readFile(inputPath, 'utf8')
397+
return JSON.parse(inputContent)
398+
} catch (err) {
399+
if (err.code === 'ENOENT' && ignoreMissingFile) {
400+
core.info(`Ignoring missing file at '${inputPath}' because \'ignore-missing-file\' is true`)
401+
} else {
402+
throw err
403+
}
404+
}
405+
}
406+
388407
async function run () {
389-
const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false'
390-
const ignoreUnauthorizedError = /^\s*(true|1)\s*$/i.test(ignoreUnauthorizedErrorValue)
391408
try {
392409
const repoToken = core.getInput('repo-token', { required: true })
393410
const inputPath = core.getInput('input', { required: true })
@@ -398,8 +415,7 @@ async function run () {
398415
const owner = github.context.repo.owner
399416
const repo = github.context.repo.repo
400417

401-
const inputContent = await fs.readFile(inputPath, 'utf8')
402-
const annotations = JSON.parse(inputContent)
418+
const annotations = await readAnnotationsFile(inputPath)
403419
const checkRunId = await createCheck(octokit, owner, repo, title, ref)
404420
const { failureCount, warningCount, noticeCount } = stats(annotations)
405421
core.info(`Found ${failureCount} failure(s), ${warningCount} warning(s) and ${noticeCount} notice(s)`)
@@ -428,6 +444,8 @@ async function run () {
428444
await updateCheck(octokit, owner, repo, checkRunId, conclusion, title, summary, annotations)
429445
}
430446
} catch (error) {
447+
const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false'
448+
const ignoreUnauthorizedError = booleanValue(ignoreUnauthorizedErrorValue)
431449
if (error.name === 'GitHubApiUnauthorizedError' && ignoreUnauthorizedError) {
432450
core.info(`Ignoring the following unauthorized error because 'ignore-unauthorized-error' is true: ${error}`)
433451
return

index.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,26 @@ const generateConclusion = function (failureCount, warningCount, noticeCount) {
112112
return conclusion
113113
}
114114

115+
const booleanValue = function (input) {
116+
return /^\s*(true|1)\s*$/i.test(input)
117+
}
118+
119+
const readAnnotationsFile= async function(inputPath) {
120+
const ignoreMissingFileValue = core.getInput('ignore-missing-file', { required: false }) || 'true'
121+
const ignoreMissingFile = booleanValue(ignoreMissingFileValue)
122+
try {
123+
const inputContent = await fs.readFile(inputPath, 'utf8')
124+
return JSON.parse(inputContent)
125+
} catch (err) {
126+
if (err.code === 'ENOENT' && ignoreMissingFile) {
127+
core.info(`Ignoring missing file at '${inputPath}' because \'ignore-missing-file\' is true`)
128+
} else {
129+
throw err
130+
}
131+
}
132+
}
133+
115134
async function run () {
116-
const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false'
117-
const ignoreUnauthorizedError = /^\s*(true|1)\s*$/i.test(ignoreUnauthorizedErrorValue)
118135
try {
119136
const repoToken = core.getInput('repo-token', { required: true })
120137
const inputPath = core.getInput('input', { required: true })
@@ -125,8 +142,7 @@ async function run () {
125142
const owner = github.context.repo.owner
126143
const repo = github.context.repo.repo
127144

128-
const inputContent = await fs.readFile(inputPath, 'utf8')
129-
const annotations = JSON.parse(inputContent)
145+
const annotations = await readAnnotationsFile(inputPath)
130146
const checkRunId = await createCheck(octokit, owner, repo, title, ref)
131147
const { failureCount, warningCount, noticeCount } = stats(annotations)
132148
core.info(`Found ${failureCount} failure(s), ${warningCount} warning(s) and ${noticeCount} notice(s)`)
@@ -155,6 +171,8 @@ async function run () {
155171
await updateCheck(octokit, owner, repo, checkRunId, conclusion, title, summary, annotations)
156172
}
157173
} catch (error) {
174+
const ignoreUnauthorizedErrorValue = core.getInput('ignore-unauthorized-error', { required: false }) || 'false'
175+
const ignoreUnauthorizedError = booleanValue(ignoreUnauthorizedErrorValue)
158176
if (error.name === 'GitHubApiUnauthorizedError' && ignoreUnauthorizedError) {
159177
core.info(`Ignoring the following unauthorized error because 'ignore-unauthorized-error' is true: ${error}`)
160178
return

0 commit comments

Comments
 (0)