Skip to content

Commit

Permalink
test: skipOnFork()
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Mar 14, 2020
1 parent 5c097c0 commit 6f9a5b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
30 changes: 29 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
getWorkDirName,
createWorkDir,
addNoJekyll,
addCNAME
addCNAME,
skipOnFork
} from '../src/utils';

beforeEach(() => {
Expand Down Expand Up @@ -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();
});
});
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,7 +12,11 @@ export async function run(): Promise<void> {
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
Expand Down
8 changes: 2 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -65,20 +64,17 @@ export async function addCNAME(
}

export async function skipOnFork(
isForkRepository: boolean,
githubToken: string,
deployKey: string,
personalToken: string
): Promise<boolean> {
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'
);
Expand Down

0 comments on commit 6f9a5b7

Please sign in to comment.