Skip to content

Commit

Permalink
Handling writing empty arrays (#6)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Bullrich committed Apr 18, 2023
1 parent 1784e08 commit 36aad24
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<void>((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;
Expand Down Expand Up @@ -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}`)
Expand All @@ -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, "[]");
}
}
}

Expand Down

0 comments on commit 36aad24

Please sign in to comment.