From d95c0489830883f61424d62fd3a71d526f6c9372 Mon Sep 17 00:00:00 2001 From: Bishal Prasad Date: Mon, 5 Dec 2022 11:36:14 +0000 Subject: [PATCH] refactor into a generic outputter --- src/outputSetter.ts | 16 ++++++++++++++++ src/restore.ts | 18 ++---------------- src/restoreImpl.ts | 19 ++++++++++++++++--- src/restoreOnly.ts | 13 ++----------- 4 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 src/outputSetter.ts diff --git a/src/outputSetter.ts b/src/outputSetter.ts new file mode 100644 index 0000000..29d11fb --- /dev/null +++ b/src/outputSetter.ts @@ -0,0 +1,16 @@ +import * as core from "@actions/core"; + +export interface IOutputSetter { + setOutput(key: string, value: string): void; + setState(key: string, value: string): void; +} + +export class StateOutputSetter implements IOutputSetter { + setOutput = core.setOutput; + setState = core.saveState; +} + +export class NonStateOuputSetter implements IOutputSetter { + setOutput = core.setOutput; + setState = core.setOutput; +} diff --git a/src/restore.ts b/src/restore.ts index 2562427..6b9e221 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -1,22 +1,8 @@ -import * as core from "@actions/core"; - -import { Inputs } from "./constants"; +import { StateOutputSetter } from "./outputSetter"; import run from "./restoreImpl"; -import * as utils from "./utils/actionUtils"; async function restore(): Promise { - const cacheKey = await run(); - if (cacheKey) { - // Store the matched cache key in states - utils.setCacheState(cacheKey); - - const isExactKeyMatch = utils.isExactKeyMatch( - core.getInput(Inputs.Key, { required: true }), - cacheKey - ); - utils.setCacheHitOutput(isExactKeyMatch); - core.info(`Cache restored from key: ${cacheKey}`); - } + await run(new StateOutputSetter()); } export default restore; diff --git a/src/restoreImpl.ts b/src/restoreImpl.ts index 55e5e49..868e812 100644 --- a/src/restoreImpl.ts +++ b/src/restoreImpl.ts @@ -1,10 +1,11 @@ import * as cache from "@actions/cache"; import * as core from "@actions/core"; -import { Events, Inputs, State } from "./constants"; +import { Events, Inputs, Outputs, State } from "./constants"; +import { IOutputSetter } from "./outputSetter"; import * as utils from "./utils/actionUtils"; -async function run(): Promise { +async function run(outputter: IOutputSetter): Promise { try { if (!utils.isCacheFeatureAvailable()) { utils.setCacheHitOutput(false); @@ -22,7 +23,7 @@ async function run(): Promise { } const primaryKey = core.getInput(Inputs.Key, { required: true }); - core.saveState(State.CachePrimaryKey, primaryKey); + outputter.setState(State.CachePrimaryKey, primaryKey); const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys); const cachePaths = utils.getInputAsArray(Inputs.Path, { @@ -46,6 +47,18 @@ async function run(): Promise { return; } + // Store the matched cache key in states + //utils.setCacheState(cacheKey); + outputter.setState(State.CacheMatchedKey, cacheKey); + + const isExactKeyMatch = utils.isExactKeyMatch( + core.getInput(Inputs.Key, { required: true }), + cacheKey + ); + //utils.setCacheHitOutput(isExactKeyMatch); + outputter.setOutput(Outputs.CacheHit, isExactKeyMatch.toString()); + core.info(`Cache restored from key: ${cacheKey}`); + return cacheKey; } catch (error: unknown) { core.setFailed((error as Error).message); diff --git a/src/restoreOnly.ts b/src/restoreOnly.ts index cab0e95..282fe93 100644 --- a/src/restoreOnly.ts +++ b/src/restoreOnly.ts @@ -1,17 +1,8 @@ -import * as core from "@actions/core"; - -import { Outputs } from "./constants"; +import { NonStateOuputSetter } from "./outputSetter"; import run from "./restoreImpl"; -import * as utils from "./utils/actionUtils"; async function restoreOnly(): Promise { - const cacheKey = await run(); - if (cacheKey) { - // Store the matched cache key in output - core.setOutput(Outputs.Key, utils.getCacheState()); - - core.info(`Cache restored from key: ${cacheKey}`); - } + await run(new NonStateOuputSetter()); } export default restoreOnly;