From 7b9cfeadc256c6d9a44af926f5efdc98a6ab3fd3 Mon Sep 17 00:00:00 2001 From: damccorm Date: Fri, 30 Aug 2019 09:52:16 -0400 Subject: [PATCH 1/2] Use readFileSync instead of require --- packages/github/src/context.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/github/src/context.ts b/packages/github/src/context.ts index b4230426e5..0d237e9ad9 100644 --- a/packages/github/src/context.ts +++ b/packages/github/src/context.ts @@ -1,7 +1,6 @@ // Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/context.ts import {WebhookPayload} from './interfaces' - -/* eslint-disable @typescript-eslint/no-require-imports */ +import {readFileSync} from 'fs' export class Context { /** @@ -21,7 +20,9 @@ export class Context { */ constructor() { this.payload = process.env.GITHUB_EVENT_PATH - ? require(process.env.GITHUB_EVENT_PATH) + ? JSON.parse( + readFileSync(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'}) + ) : {} this.eventName = process.env.GITHUB_EVENT_NAME as string this.sha = process.env.GITHUB_SHA as string From 3a16ee718bd454d0c599e372b90a52d27dfa2826 Mon Sep 17 00:00:00 2001 From: damccorm Date: Fri, 30 Aug 2019 11:08:16 -0400 Subject: [PATCH 2/2] error handling --- packages/github/src/context.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/github/src/context.ts b/packages/github/src/context.ts index 0d237e9ad9..a06a8e0a48 100644 --- a/packages/github/src/context.ts +++ b/packages/github/src/context.ts @@ -1,6 +1,7 @@ // Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/context.ts import {WebhookPayload} from './interfaces' -import {readFileSync} from 'fs' +import {readFileSync, existsSync} from 'fs' +import {EOL} from 'os' export class Context { /** @@ -19,11 +20,20 @@ export class Context { * Hydrate the context from the environment */ constructor() { - this.payload = process.env.GITHUB_EVENT_PATH - ? JSON.parse( + this.payload = {} + if (process.env.GITHUB_EVENT_PATH) { + if (existsSync(process.env.GITHUB_EVENT_PATH)) { + this.payload = JSON.parse( readFileSync(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'}) ) - : {} + } else { + process.stdout.write( + `GITHUB_EVENT_PATH ${ + process.env.GITHUB_EVENT_PATH + } does not exist${EOL}` + ) + } + } this.eventName = process.env.GITHUB_EVENT_NAME as string this.sha = process.env.GITHUB_SHA as string this.ref = process.env.GITHUB_REF as string