From 8f7b3c3910896facb07b585caddc104a34860fa2 Mon Sep 17 00:00:00 2001 From: Technote Date: Fri, 3 Jun 2022 01:33:35 +0900 Subject: [PATCH] feat: add strict success flag (#97) --- action.yml | 4 ++++ src/process.test.ts | 24 ++++++++++++++++++------ src/process.ts | 7 +++++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index e3f7f18..f745f7c 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,10 @@ inputs: description: Fallback conclusion required: false default: skipped + STRICT_SUCCESS: + description: Whether to report as success only if all jobs are successful + required: false + default: "false" outputs: conclusion: diff --git a/src/process.test.ts b/src/process.test.ts index 5d63964..c8bd5d3 100644 --- a/src/process.test.ts +++ b/src/process.test.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-magic-numbers */ import { resolve } from 'path'; import { Logger } from '@technote-space/github-action-log-helper'; import { @@ -89,21 +88,34 @@ describe('getWorkflowConclusion', () => { it('should get workflow conclusion', () => { expect(getWorkflowConclusion([])).toBe('skipped'); + expect(getWorkflowConclusion(['test'])).toBe('skipped'); expect(getWorkflowConclusion([ 'neutral', - 'success', 'cancelled', + 'success', ])).toBe('cancelled'); + expect(getWorkflowConclusion([ + 'failure', + 'cancelled', + ])).toBe('failure'); }); - it('should get fallback conclusion 1', () => { + it('should get specified fallback conclusion', () => { process.env.INPUT_FALLBACK_CONCLUSION = 'failure'; expect(getWorkflowConclusion([])).toBe('failure'); }); - it('should get fallback conclusion 2', () => { - process.env.INPUT_FALLBACK_CONCLUSION = ''; - expect(getWorkflowConclusion([])).toBe(''); + it('should get workflow conclusion (strict success)', () => { + process.env.INPUT_STRICT_SUCCESS = 'true'; + expect(getWorkflowConclusion(['success'])).toBe('success'); + expect(getWorkflowConclusion(['success', 'success'])).toBe('success'); + + expect(getWorkflowConclusion(['skipped'])).toBe('failure'); + expect(getWorkflowConclusion(['success', 'success', 'skipped'])).toBe('failure'); + expect(getWorkflowConclusion([])).toBe('skipped'); + + process.env.INPUT_FALLBACK_CONCLUSION = 'failure'; + expect(getWorkflowConclusion([])).toBe('failure'); }); }); diff --git a/src/process.ts b/src/process.ts index a4e605e..f7cdede 100644 --- a/src/process.ts +++ b/src/process.ts @@ -24,8 +24,11 @@ export const getJobConclusions = (jobs: Array<{ conclusion: string | null }>): A .map(job => job.conclusion), ); -// eslint-disable-next-line no-magic-numbers -export const getWorkflowConclusion = (conclusions: Array): string => CONCLUSIONS.filter(conclusion => conclusions.includes(conclusion)).slice(-1)[0] ?? getInput('FALLBACK_CONCLUSION'); +export const getWorkflowConclusion = (conclusions: Array): string => + !conclusions.length ? getInput('FALLBACK_CONCLUSION') : + Utils.getBoolValue(getInput('STRICT_SUCCESS')) ? + conclusions.some(conclusion => conclusion !== 'success') ? 'failure' : 'success' : + CONCLUSIONS.filter(conclusion => conclusions.includes(conclusion)).slice(-1)[0] ?? getInput('FALLBACK_CONCLUSION'); export const execute = async(logger: Logger, octokit: Octokit, context: Context): Promise => { const jobs = await getJobs(octokit, context);