From 36aad240fd140ff48ff1d1d358d4eaedf6a38f25 Mon Sep 17 00:00:00 2001 From: Javyer Date: Tue, 18 Apr 2023 10:57:56 +0200 Subject: [PATCH] Handling writing empty arrays (#6) Added a case where, if there are no data, it will write an empty array (`[]`) to the file. This is useful to handle the case of reading non existing files. --- README.md | 1 + action.yml | 2 +- package.json | 2 +- src/index.ts | 23 +++++++++++++++++------ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 17faa97..63a0ff7 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ You can find all the inputs in [the action file](./action.yml) but let's walk th - **default**: false - `fileOutput`: String. File to which the output from `data` should be written. - Useful in the cases where the output is too big and GitHub Actions can not handle it as a variable. + - Make sure that the directory exists, else it will fail #### Accessing other repositories diff --git a/action.yml b/action.yml index b27640c..1fc49bd 100644 --- a/action.yml +++ b/action.yml @@ -37,4 +37,4 @@ outputs: runs: using: 'docker' - image: 'docker://ghcr.io/paritytech/stale-pr-finder/action:0.0.2' + image: 'docker://ghcr.io/paritytech/stale-pr-finder/action:0.0.3' diff --git a/package.json b/package.json index 3089edb..4580b89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stale-pr-finder", - "version": "0.0.2", + "version": "0.0.3", "description": "GitHub action that finds stale PRs and produce an output with them", "main": "src/index.ts", "scripts": { diff --git a/src/index.ts b/src/index.ts index 1fb2024..1b4543f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { debug, getBooleanInput, getInput, info, setFailed, setOutput, summary } from "@actions/core"; +import { debug, getBooleanInput, getInput, info, setOutput, summary } from "@actions/core"; import { context } from "@actions/github"; import { Context } from "@actions/github/lib/context"; import { github } from "@eng-automation/integrations"; @@ -14,6 +14,18 @@ const daysSinceDate = (date: string): number => { return moment().diff(moment(date), 'days') } +const writeToFile = async (fileName: string, content: string) => { + return new Promise((res, rej) => { + writeFile(fileName, content, err => { + if (err) { + rej(err) + } else { + res(); + } + }) + }) +} + const getFiltersFromInput = (): Filters => { const inputDays = Number.parseInt(getInput("days-stale", { required: false })); const daysStale = isNaN(inputDays) ? 5 : inputDays; @@ -106,11 +118,7 @@ const runAction = async (ctx: Context) => { setOutput("message", message); if (outputFile) { - writeFile(outputFile, jsonData, err => { - if (err) { - setFailed(err); - } - }) + await writeToFile(outputFile, jsonData); } await summary.addHeading(`${repo.owner}/${repo.repo}`) @@ -123,6 +131,9 @@ const runAction = async (ctx: Context) => { } else { setOutput("message", `### Repo ${repo.owner}/${repo.repo} has no stale Pull Requests`); info(`Repo ${repo.owner}/${repo.repo} has no stale Pull Requests`); + if (outputFile) { + await writeToFile(outputFile, "[]"); + } } }