mirror of
https://code.forgejo.org/actions/cache.git
synced 2024-12-23 12:36:01 +01:00
Merge pull request #1016 from actions/bishal/outputter
Refactor setting output and state into a generic outputter
This commit is contained in:
commit
e3d8fb0b34
4 changed files with 36 additions and 30 deletions
16
src/outputSetter.ts
Normal file
16
src/outputSetter.ts
Normal file
|
@ -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;
|
||||
}
|
|
@ -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<void> {
|
||||
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;
|
||||
|
|
|
@ -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<string | undefined> {
|
||||
async function run(outputter: IOutputSetter): Promise<string | undefined> {
|
||||
try {
|
||||
if (!utils.isCacheFeatureAvailable()) {
|
||||
utils.setCacheHitOutput(false);
|
||||
|
@ -22,7 +23,7 @@ async function run(): Promise<string | undefined> {
|
|||
}
|
||||
|
||||
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<string | undefined> {
|
|||
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);
|
||||
|
|
|
@ -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<void> {
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue