Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github actions cache: Use more unique key #1134

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 40 additions & 8 deletions dist/index.js
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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'}"`);
Expand All @@ -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 '';
Expand Down Expand Up @@ -2685,16 +2687,46 @@ 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 cacheKey = sha256(JSON.stringify(options));
const storage = new state_cache_storage_1.StateCacheStorage(cacheKey);
return new state_1.State(storage, options);
};
exports.getStateInstance = getStateInstance;
Expand Down
17 changes: 11 additions & 6 deletions src/classes/state/state-cache-storage.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -65,15 +64,21 @@ const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
}
};
export class StateCacheStorage implements IStateStorage {
private cacheKey: string;

constructor(cacheKey: string) {
this.cacheKey = cacheKey;
}

async save(serializedState: string): Promise<void> {
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;

Expand All @@ -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 "${
Expand All @@ -99,15 +104,15 @@ 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.'
);
return '';
}

await cache.restoreCache([path.dirname(filePath)], CACHE_KEY);
await cache.restoreCache([path.dirname(filePath)], this.cacheKey);

if (!fs.existsSync(filePath)) {
core.warning(
Expand Down
10 changes: 9 additions & 1 deletion src/services/state.service.ts
@@ -1,9 +1,17 @@
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 cacheKey = sha256(JSON.stringify(options));
const storage = new StateCacheStorage(cacheKey);
return new State(storage, options);
};