diff --git a/dist/index.js b/dist/index.js index f2786a0f6..912b7fe9e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1595,7 +1595,6 @@ const core = __importStar(__nccwpck_require__(2186)); const github_1 = __nccwpck_require__(5438); const plugin_retry_1 = __nccwpck_require__(6298); const cache = __importStar(__nccwpck_require__(7799)); -const CACHE_KEY = '_state'; const STATE_FILE = 'state.txt'; const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03'; const mkTempDir = () => { @@ -1644,22 +1643,25 @@ const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, fu } }); class StateCacheStorage { + constructor(cacheKey) { + this.cacheKey = cacheKey; + } save(serializedState) { return __awaiter(this, void 0, void 0, function* () { const tmpDir = mkTempDir(); const filePath = path_1.default.join(tmpDir, STATE_FILE); fs_1.default.writeFileSync(filePath, serializedState); try { - const cacheExists = yield checkIfCacheExists(CACHE_KEY); + const cacheExists = yield checkIfCacheExists(this.cacheKey); if (cacheExists) { - yield resetCacheWithOctokit(CACHE_KEY); + yield resetCacheWithOctokit(this.cacheKey); } const fileSize = fs_1.default.statSync(filePath).size; if (fileSize === 0) { core.info(`the state will be removed`); return; } - yield cache.saveCache([path_1.default.dirname(filePath)], CACHE_KEY); + yield cache.saveCache([path_1.default.dirname(filePath)], this.cacheKey); } catch (error) { core.warning(`Saving the state was not successful due to "${error.message || 'unknown reason'}"`); @@ -1675,12 +1677,12 @@ class StateCacheStorage { const filePath = path_1.default.join(tmpDir, STATE_FILE); unlinkSafely(filePath); try { - const cacheExists = yield checkIfCacheExists(CACHE_KEY); + const cacheExists = yield checkIfCacheExists(this.cacheKey); if (!cacheExists) { core.info('The saved state was not found, the process starts from the first issue.'); return ''; } - yield cache.restoreCache([path_1.default.dirname(filePath)], CACHE_KEY); + yield cache.restoreCache([path_1.default.dirname(filePath)], this.cacheKey); if (!fs_1.default.existsSync(filePath)) { core.warning('Unknown error when unpacking the cache, the process starts from the first issue.'); return ''; @@ -2685,16 +2687,47 @@ exports.LoggerService = LoggerService; /***/ }), /***/ 6330: -/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.getStateInstance = void 0; +const crypto = __importStar(__nccwpck_require__(6113)); const state_1 = __nccwpck_require__(5186); const state_cache_storage_1 = __nccwpck_require__(3709); +function sha256(message) { + const hash = crypto.createHash('sha256'); + hash.update(message); + return hash.digest('hex'); +} const getStateInstance = (options) => { - const storage = new state_cache_storage_1.StateCacheStorage(); + const json = JSON.stringify(options, (key, value) => key === 'repoToken' ? undefined : value); + const cacheKey = sha256(json); + const storage = new state_cache_storage_1.StateCacheStorage(cacheKey); return new state_1.State(storage, options); }; exports.getStateInstance = getStateInstance; diff --git a/src/classes/state/state-cache-storage.ts b/src/classes/state/state-cache-storage.ts index b30b503b6..176459bab 100644 --- a/src/classes/state/state-cache-storage.ts +++ b/src/classes/state/state-cache-storage.ts @@ -7,7 +7,6 @@ import {context, getOctokit} from '@actions/github'; import {retry as octokitRetry} from '@octokit/plugin-retry'; import * as cache from '@actions/cache'; -const CACHE_KEY = '_state'; const STATE_FILE = 'state.txt'; const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03'; @@ -65,15 +64,21 @@ const resetCacheWithOctokit = async (cacheKey: string): Promise => { } }; export class StateCacheStorage implements IStateStorage { + private cacheKey: string; + + constructor(cacheKey: string) { + this.cacheKey = cacheKey; + } + async save(serializedState: string): Promise { const tmpDir = mkTempDir(); const filePath = path.join(tmpDir, STATE_FILE); fs.writeFileSync(filePath, serializedState); try { - const cacheExists = await checkIfCacheExists(CACHE_KEY); + const cacheExists = await checkIfCacheExists(this.cacheKey); if (cacheExists) { - await resetCacheWithOctokit(CACHE_KEY); + await resetCacheWithOctokit(this.cacheKey); } const fileSize = fs.statSync(filePath).size; @@ -82,7 +87,7 @@ export class StateCacheStorage implements IStateStorage { return; } - await cache.saveCache([path.dirname(filePath)], CACHE_KEY); + await cache.saveCache([path.dirname(filePath)], this.cacheKey); } catch (error) { core.warning( `Saving the state was not successful due to "${ @@ -99,7 +104,7 @@ export class StateCacheStorage implements IStateStorage { const filePath = path.join(tmpDir, STATE_FILE); unlinkSafely(filePath); try { - const cacheExists = await checkIfCacheExists(CACHE_KEY); + const cacheExists = await checkIfCacheExists(this.cacheKey); if (!cacheExists) { core.info( 'The saved state was not found, the process starts from the first issue.' @@ -107,7 +112,7 @@ export class StateCacheStorage implements IStateStorage { return ''; } - await cache.restoreCache([path.dirname(filePath)], CACHE_KEY); + await cache.restoreCache([path.dirname(filePath)], this.cacheKey); if (!fs.existsSync(filePath)) { core.warning( diff --git a/src/services/state.service.ts b/src/services/state.service.ts index 77e403ecc..25719dadc 100644 --- a/src/services/state.service.ts +++ b/src/services/state.service.ts @@ -1,9 +1,20 @@ +import * as crypto from 'crypto'; import {IState} from '../interfaces/state/state'; import {State} from '../classes/state/state'; import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options'; import {StateCacheStorage} from '../classes/state/state-cache-storage'; +function sha256(message: string): string { + const hash = crypto.createHash('sha256'); + hash.update(message); + return hash.digest('hex'); +} + export const getStateInstance = (options: IIssuesProcessorOptions): IState => { - const storage = new StateCacheStorage(); + const json = JSON.stringify(options, (key, value) => + key === 'repoToken' ? undefined : value + ); + const cacheKey = sha256(json); + const storage = new StateCacheStorage(cacheKey); return new State(storage, options); };