From 6f9a5b7a66bbf855cadc34099fa6450c40eff4a2 Mon Sep 17 00:00:00 2001 From: peaceiris <30958501+peaceiris@users.noreply.github.com> Date: Sat, 14 Mar 2020 21:31:02 +0000 Subject: [PATCH] test: skipOnFork() --- __tests__/utils.test.ts | 30 +++++++++++++++++++++++++++++- src/main.ts | 5 +++++ src/utils.ts | 8 ++------ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 23b7c3b92..151d38ef4 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -5,7 +5,8 @@ import { getWorkDirName, createWorkDir, addNoJekyll, - addCNAME + addCNAME, + skipOnFork } from '../src/utils'; beforeEach(() => { @@ -203,3 +204,30 @@ describe('addCNAME()', () => { fs.unlinkSync(filepath); }); }); + +describe('skipOnFork()', () => { + test('return false on upstream', async () => { + const test = await skipOnFork(false, 'token', '', ''); + expect(test).toBeFalsy(); + }); + + test('return false on fork with github_token', async () => { + const test = await skipOnFork(true, 'token', '', ''); + expect(test).toBeFalsy(); + }); + + test('return false on fork with deploy_key', async () => { + const test = await skipOnFork(true, '', 'deploy_key', ''); + expect(test).toBeFalsy(); + }); + + test('return false on fork with personal_token', async () => { + const test = await skipOnFork(true, '', '', 'personal_token'); + expect(test).toBeFalsy(); + }); + + test('return true on fork with empty deploy_key or personal_token', async () => { + const test = await skipOnFork(true, '', '', ''); + expect(test).toBeTruthy(); + }); +}); diff --git a/src/main.ts b/src/main.ts index 690a575bf..bef9353cf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import {context} from '@actions/github'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; import {Inputs} from './interfaces'; @@ -11,7 +12,11 @@ export async function run(): Promise { const inps: Inputs = getInputs(); showInputs(inps); + const isForkRepository = + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (context.payload as any).repository.fork === 'true'; const isSkipOnFork = await skipOnFork( + isForkRepository, inps.GithubToken, inps.DeployKey, inps.PersonalToken diff --git a/src/utils.ts b/src/utils.ts index f256eda5d..e5bb210af 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,3 @@ -import {context} from '@actions/github'; import * as core from '@actions/core'; import * as io from '@actions/io'; import path from 'path'; @@ -65,20 +64,17 @@ export async function addCNAME( } export async function skipOnFork( + isForkRepository: boolean, githubToken: string, deployKey: string, personalToken: string ): Promise { - const isForkRepository = - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (context.payload as any).repository.fork === 'true'; - if (isForkRepository) { if (githubToken) { return false; } - if (!deployKey && !personalToken) { + if (deployKey === '' && personalToken === '') { core.warning( 'Action runs on fork and deploy_key or personal_token is empty' );