From e16ad4b7fd5e93770d8755f5bafa4813a371d1c4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Mar 2026 15:53:08 +0000 Subject: [PATCH] refactor: extract shared utils, improve logging, fix async pattern and guards Co-authored-by: HiGarfield <32226909+HiGarfield@users.noreply.github.com> Agent-Logs-Url: https://github.com/HiGarfield/cachewrtbuild/sessions/b6c68633-68f8-4d69-b554-894c31229c91 --- fetch.js | 47 ++++++++------------------------- save.js | 80 +++++++++++++++++++++++++------------------------------- utils.js | 38 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 80 deletions(-) create mode 100644 utils.js diff --git a/fetch.js b/fetch.js index 226aa45b..6e065ac2 100644 --- a/fetch.js +++ b/fetch.js @@ -1,49 +1,19 @@ const core = require("@actions/core"); const { execSync } = require("child_process"); -const path = require("path"); const cache = require("@actions/cache"); - -function parseBooleanInput(value, defaultValue = false) { - const normalized = value.trim().toLowerCase(); - return { 'true': true, 'false': false }[normalized] ?? defaultValue; -} +const { parseBooleanInput, buildBaseConfig } = require("./utils"); async function fetchCache() { try { - const paths = []; - const restoreKeys = []; - - const mixkey = core.getInput("mixkey"); - const prefix = core.getInput("prefix"); const cleanUpCache = parseBooleanInput(core.getInput("clean")); - if (cleanUpCache) return; - if (prefix) { - process.chdir(prefix); - core.debug(`Changed working directory to: ${prefix}`); - } - - let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt"; - - const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true); + const { keyString: baseKey, paths, cacheToolchain, cacheCcache } = buildBaseConfig(); const skipBuildingToolchain = parseBooleanInput(core.getInput("skip"), true); - if (cacheToolchain) { - const toolchainHash = execSync('git log --pretty=tformat:"%h" -n1 tools toolchain') - .toString() - .trim(); - - keyString += `-${toolchainHash}`; - paths.push( - path.join("staging_dir", "host*"), - path.join("staging_dir", "tool*") - ); - } else { - core.debug("Skipping toolchain processing"); - } + let keyString = baseKey; + const restoreKeys = []; - const cacheCcache = parseBooleanInput(core.getInput("ccache")); if (cacheCcache) { const timestamp = execSync("date +%s").toString().trim(); restoreKeys.unshift(keyString); @@ -51,8 +21,14 @@ async function fetchCache() { paths.push(".ccache"); } + if (paths.length === 0) { + core.debug("No paths configured for caching, skipping"); + return; + } + + core.debug(`Cache key: ${keyString}`); + core.debug(`Cache restore keys: ${restoreKeys.join(", ")}`); core.debug(`Cache paths: ${paths.join(", ")}`); - console.log(keyString, restoreKeys); const cacheFetchingResult = await cache.restoreCache(paths, keyString, restoreKeys); @@ -69,7 +45,6 @@ async function fetchCache() { } } catch (error) { core.setFailed(error.message); - process.exit(1); } } diff --git a/save.js b/save.js index f1991425..eb12f39e 100644 --- a/save.js +++ b/save.js @@ -1,56 +1,48 @@ const core = require("@actions/core"); const { execSync } = require("child_process"); -const path = require("path"); const cache = require("@actions/cache"); - -function parseBooleanInput(value, defaultValue = false) { - const normalized = value.trim().toLowerCase(); - return { 'true': true, 'false': false }[normalized] ?? defaultValue; -} +const { parseBooleanInput, buildBaseConfig } = require("./utils"); async function saveCache() { try { + const cleanUpCache = parseBooleanInput(core.getInput("clean")); + if (cleanUpCache) { + core.debug("Cache clean requested, skipping save"); + return; + } + const skipSaving = parseBooleanInput(core.getInput("skip_saving")); + if (skipSaving) { + core.debug("skip_saving is set, skipping save"); + return; + } + const cacheState = core.getState("CACHE_STATE"); + if (cacheState === "hit") { + core.debug("Cache was already restored, skipping save"); + return; + } + + const { keyString: baseKey, paths, cacheCcache } = buildBaseConfig(); + + let keyString = baseKey; + if (cacheCcache) { + const timestamp = execSync("date +%s").toString().trim(); + keyString += `-${timestamp}`; + paths.push(".ccache"); + } + + if (paths.length === 0) { + core.debug("No paths configured for caching, skipping"); + return; + } + + core.debug(`Saving cache with key: ${keyString}`); + core.debug(`Cache paths: ${paths.join(", ")}`); - if (cacheState !== "hit" && !skipSaving) { - const paths = []; - const mixkey = core.getInput("mixkey"); - let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt"; - - const prefix = core.getInput("prefix"); - if (prefix) { - process.chdir(prefix); - core.debug(`Changed working directory to: ${prefix}`); - } - - const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true); - if (cacheToolchain) { - const toolchainHash = execSync( - 'git log --pretty=tformat:"%h" -n1 tools toolchain' - ).toString().trim(); - - keyString += `-${toolchainHash}`; - paths.push( - path.join("staging_dir", "host*"), - path.join("staging_dir", "tool*") - ); - } - - const cacheCcache = parseBooleanInput(core.getInput("ccache")); - if (cacheCcache) { - const timestamp = execSync("date +%s").toString().trim(); - keyString += `-${timestamp}`; - paths.push(".ccache"); - } - - console.log(keyString); - - await cache.saveCache(paths, keyString) - .then(res => { - if (res) console.log(res, " cache saved"); - }) - .catch(err => core.error(`Cache save failed: ${err.stack}`)); + const cacheId = await cache.saveCache(paths, keyString); + if (cacheId) { + core.info(`Cache saved with key: ${keyString} (id: ${cacheId})`); } } catch (error) { core.warning(error.message); diff --git a/utils.js b/utils.js new file mode 100644 index 00000000..e9629d5a --- /dev/null +++ b/utils.js @@ -0,0 +1,38 @@ +const core = require("@actions/core"); +const { execSync } = require("child_process"); +const path = require("path"); + +function parseBooleanInput(value, defaultValue = false) { + const normalized = value.trim().toLowerCase(); + return { 'true': true, 'false': false }[normalized] ?? defaultValue; +} + +function buildBaseConfig() { + const prefix = core.getInput("prefix"); + if (prefix) { + process.chdir(prefix); + core.debug(`Changed working directory to: ${prefix}`); + } + + const mixkey = core.getInput("mixkey"); + let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt"; + const paths = []; + + const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true); + if (cacheToolchain) { + const toolchainHash = execSync('git log --pretty=tformat:"%h" -n1 tools toolchain') + .toString() + .trim(); + keyString += `-${toolchainHash}`; + paths.push( + path.join("staging_dir", "host*"), + path.join("staging_dir", "tool*") + ); + } + + const cacheCcache = parseBooleanInput(core.getInput("ccache")); + + return { keyString, paths, cacheToolchain, cacheCcache }; +} + +module.exports = { parseBooleanInput, buildBaseConfig };