Skip to content

Commit

Permalink
Add ignore-missing-file to control if the task should fail if the inp…
Browse files Browse the repository at this point in the history
…ut file is missing
  • Loading branch information
ggrossetie committed Oct 23, 2020
1 parent 3eac6f7 commit fad0bc6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
26 changes: 22 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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)`)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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)`)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fad0bc6

Please sign in to comment.