Skip to content

Commit

Permalink
style: Update files to format code
Browse files Browse the repository at this point in the history
  • Loading branch information
jongwooo committed Aug 18, 2024
1 parent 4f9028d commit 97113be
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 144 deletions.
22 changes: 11 additions & 11 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
export enum Inputs {
UseCache = "use-cache",
Key = "key",
RestoreKeys = "restore-keys",
UseCache = "use-cache",
Key = "key",
RestoreKeys = "restore-keys",
}

export enum Outputs {
CacheHit = "cache-hit",
CacheHit = "cache-hit",
}

export enum State {
CachePrimaryKey = "CACHE_KEY",
CacheMatchedKey = "CACHE_RESULT",
CachePrimaryKey = "CACHE_KEY",
CacheMatchedKey = "CACHE_RESULT",
}

export enum Events {
Key = "GITHUB_EVENT_NAME",
Key = "GITHUB_EVENT_NAME",
}

export enum Platform {
RunnerOs = "RUNNER_OS",
RunnerOs = "RUNNER_OS",
}

export enum Gatsby {
CacheDir = ".cache",
PublicDir = "public",
Env = "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES",
CacheDir = ".cache",
PublicDir = "public",
Env = "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES",
}

export const RefKey = "GITHUB_REF";
2 changes: 1 addition & 1 deletion src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import restoreImpl from "./restoreImpl";
import { StateProvider } from "./stateProvider";

async function run(): Promise<void> {
await restoreImpl(new StateProvider());
await restoreImpl(new StateProvider());
}

void run();
Expand Down
86 changes: 43 additions & 43 deletions src/restoreImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,62 @@ import { BaseStateProvider } from "./stateProvider";
type Runner = "Linux" | "Windows" | "macOS";

async function restoreImpl(
stateProvider: BaseStateProvider,
stateProvider: BaseStateProvider,
): Promise<string | undefined> {
try {
if (!utils.isCacheFeatureAvailable()) {
core.setOutput(Outputs.CacheHit, "false");
return;
}
try {
if (!utils.isCacheFeatureAvailable()) {
core.setOutput(Outputs.CacheHit, "false");
return;
}

if (!utils.isValidEvent()) {
const eventName = process.env[Events.Key] || "";
utils.logWarning(
`Event Validation Error: The event type ${eventName} is not supported because it's not tied to a branch or tag ref.`,
);
return;
}
if (!utils.isValidEvent()) {
const eventName = process.env[Events.Key] || "";
utils.logWarning(
`Event Validation Error: The event type ${eventName} is not supported because it's not tied to a branch or tag ref.`,
);
return;
}

utils.setConditionalPageBuild();
utils.setConditionalPageBuild();

const cachePaths = await utils.getBuildOutputPaths();
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);
const cachePaths = await utils.getBuildOutputPaths();
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);

let primaryKey = core.getInput(Inputs.Key);
if (!primaryKey) {
const platform = process.env[Platform.RunnerOs] as Runner;
primaryKey = `${platform}-gatsby-build-`;
}
let primaryKey = core.getInput(Inputs.Key);
if (!primaryKey) {
const platform = process.env[Platform.RunnerOs] as Runner;
primaryKey = `${platform}-gatsby-build-`;
}

core.debug(`primary key is ${primaryKey}`);
stateProvider.setState(State.CachePrimaryKey, primaryKey);
core.debug(`primary key is ${primaryKey}`);
stateProvider.setState(State.CachePrimaryKey, primaryKey);

const cacheKey: string | undefined = await cache.restoreCache(
cachePaths,
primaryKey,
restoreKeys,
);
const cacheKey: string | undefined = await cache.restoreCache(
cachePaths,
primaryKey,
restoreKeys,
);

if (!cacheKey) {
core.info(
`Cache not found for keys: ${[primaryKey, ...restoreKeys].join(", ")}`,
);
if (!cacheKey) {
core.info(
`Cache not found for keys: ${[primaryKey, ...restoreKeys].join(", ")}`,
);

return;
}
return;
}

stateProvider.setState(State.CacheMatchedKey, cacheKey);
stateProvider.setState(State.CacheMatchedKey, cacheKey);

const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);

core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
core.info(`Cache restored from key: ${cacheKey}`);
core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
core.info(`Cache restored from key: ${cacheKey}`);

return cacheKey;
} catch (error: unknown) {
core.setFailed((error as Error).message);
return;
}
return cacheKey;
} catch (error: unknown) {
core.setFailed((error as Error).message);
return;
}
}

export default restoreImpl;
2 changes: 1 addition & 1 deletion src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import saveImpl from "./saveImpl";
import { StateProvider } from "./stateProvider";

async function run(): Promise<void> {
await saveImpl(new StateProvider());
await saveImpl(new StateProvider());
}

void run();
Expand Down
70 changes: 35 additions & 35 deletions src/saveImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ import { BaseStateProvider } from "./stateProvider";
process.on("uncaughtException", (e) => utils.logWarning(e.message));

async function saveImpl(
stateProvider: BaseStateProvider,
stateProvider: BaseStateProvider,
): Promise<number | void> {
let cacheId = -1;
try {
if (!utils.isCacheFeatureAvailable()) {
return;
}

const state = stateProvider.getCacheState();

const primaryKey =
stateProvider.getState(State.CachePrimaryKey) ||
core.getInput(Inputs.Key);

if (!primaryKey) {
utils.logWarning(`Key is not specified.`);
return;
}

if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`,
);
return;
}

const cachePaths = await utils.getBuildOutputPaths();
cacheId = await cache.saveCache(cachePaths, primaryKey);

if (cacheId != -1) {
core.info(`Cache saved with key: ${primaryKey}`);
}
} catch (error: unknown) {
utils.logWarning((error as Error).message);
}
return cacheId;
let cacheId = -1;
try {
if (!utils.isCacheFeatureAvailable()) {
return;
}

const state = stateProvider.getCacheState();

const primaryKey =
stateProvider.getState(State.CachePrimaryKey) ||
core.getInput(Inputs.Key);

if (!primaryKey) {
utils.logWarning(`Key is not specified.`);
return;
}

if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`,
);
return;
}

const cachePaths = await utils.getBuildOutputPaths();
cacheId = await cache.saveCache(cachePaths, primaryKey);

if (cacheId != -1) {
core.info(`Cache saved with key: ${primaryKey}`);
}
} catch (error: unknown) {
utils.logWarning((error as Error).message);
}
return cacheId;
}

export default saveImpl;
26 changes: 13 additions & 13 deletions src/stateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import * as core from "@actions/core";
import { State } from "./constants";

export interface BaseStateProvider {
getCacheState(): string | undefined;
getState(key: string): string;
setState(key: string, value: string): void;
getCacheState(): string | undefined;
getState(key: string): string;
setState(key: string, value: string): void;
}

export class StateProvider implements BaseStateProvider {
getCacheState(): string | undefined {
const cacheKey = this.getState(State.CacheMatchedKey);
if (cacheKey) {
core.debug(`Cache state/key: ${cacheKey}`);
return cacheKey;
}
getCacheState(): string | undefined {
const cacheKey = this.getState(State.CacheMatchedKey);
if (cacheKey) {
core.debug(`Cache state/key: ${cacheKey}`);
return cacheKey;
}

return undefined;
}
return undefined;
}

getState = core.getState;
setState = core.saveState;
getState = core.getState;
setState = core.saveState;
}
80 changes: 40 additions & 40 deletions src/utils/actionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,71 @@ import path from "path";
import { Gatsby, RefKey } from "../constants";

export function isGhes(): boolean {
const url = process.env.GITHUB_SERVER_URL ?? "https://github.com";
const ghUrl = new URL(url);
return ghUrl.hostname.toUpperCase() !== "GITHUB.COM";
const url = process.env.GITHUB_SERVER_URL ?? "https://github.com";
const ghUrl = new URL(url);
return ghUrl.hostname.toUpperCase() !== "GITHUB.COM";
}

export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
return !!(
cacheKey &&
cacheKey.localeCompare(key, undefined, {
sensitivity: "accent",
}) === 0
);
return !!(
cacheKey &&
cacheKey.localeCompare(key, undefined, {
sensitivity: "accent",
}) === 0
);
}

export function logWarning(message: string): void {
const warningPrefix = "[warning]";
core.info(`${warningPrefix}${message}`);
const warningPrefix = "[warning]";
core.info(`${warningPrefix}${message}`);
}

export function isValidEvent(): boolean {
return RefKey in process.env && Boolean(process.env[RefKey]);
return RefKey in process.env && Boolean(process.env[RefKey]);
}

export function getInputAsArray(
name: string,
options?: core.InputOptions,
name: string,
options?: core.InputOptions,
): string[] {
return core
.getInput(name, options)
.split("\n")
.map((s) => s.replace(/^!\s+/, "!").trim())
.filter((x) => x !== "");
return core
.getInput(name, options)
.split("\n")
.map((s) => s.replace(/^!\s+/, "!").trim())
.filter((x) => x !== "");
}

export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) {
return true;
}
if (cache.isFeatureAvailable()) {
return true;
}

if (isGhes()) {
logWarning(
`Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.
if (isGhes()) {
logWarning(
`Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.
Otherwise please upgrade to GHES version >= 3.5 and If you are also using Github Connect, please unretire the actions/cache namespace before upgrade (see https://docs.github.com/en/enterprise-server@3.5/admin/github-actions/managing-access-to-actions-from-githubcom/enabling-automatic-access-to-githubcom-actions-using-github-connect#automatic-retirement-of-namespaces-for-actions-accessed-on-githubcom)`,
);
return false;
}
);
return false;
}

logWarning(
"An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.",
);
return false;
logWarning(
"An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.",
);
return false;
}

export function setConditionalPageBuild(): void {
process.env[Gatsby.Env] = "true";
core.debug(`Set ${Gatsby.Env}=${process.env[Gatsby.Env]}`);
process.env[Gatsby.Env] = "true";
core.debug(`Set ${Gatsby.Env}=${process.env[Gatsby.Env]}`);
}

export async function getBuildOutputPaths(): Promise<string[]> {
const targetPaths = [Gatsby.CacheDir, Gatsby.PublicDir];
const buildOutputPaths = [];
const targetPaths = [Gatsby.CacheDir, Gatsby.PublicDir];
const buildOutputPaths = [];

for await (const target of targetPaths) {
buildOutputPaths.push(path.join(process.cwd(), target));
}
for await (const target of targetPaths) {
buildOutputPaths.push(path.join(process.cwd(), target));
}

return buildOutputPaths;
return buildOutputPaths;
}

0 comments on commit 97113be

Please sign in to comment.