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

do not attempt to delete existing cache #1183

Open
wants to merge 4 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 1 addition & 40 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1592,8 +1592,6 @@ const fs_1 = __importDefault(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const os_1 = __importDefault(__nccwpck_require__(2037));
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';
Expand All @@ -1611,49 +1609,13 @@ const unlinkSafely = (filePath) => {
/* ignore */
}
};
const getOctokitClient = () => {
const token = core.getInput('repo-token');
return (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry);
};
const checkIfCacheExists = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () {
const client = getOctokitClient();
try {
const issueResult = yield client.request(`/repos/${github_1.context.repo.owner}/${github_1.context.repo.repo}/actions/caches`);
const caches = issueResult.data['actions_caches'] || [];
return Boolean(caches.find(cache => cache['key'] === cacheKey));
}
catch (error) {
core.debug(`Error checking if cache exist: ${error.message}`);
}
return false;
});
const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () {
const client = getOctokitClient();
core.debug(`remove cache "${cacheKey}"`);
try {
// TODO: replace with client.rest.
yield client.request(`DELETE /repos/${github_1.context.repo.owner}/${github_1.context.repo.repo}/actions/caches?key=${cacheKey}`);
}
catch (error) {
if (error.status) {
core.warning(`Error delete ${cacheKey}: [${error.status}] ${error.message || 'Unknown reason'}`);
}
else {
throw error;
}
}
});
class StateCacheStorage {
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);
if (cacheExists) {
yield resetCacheWithOctokit(CACHE_KEY);
}
const fileSize = fs_1.default.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the state will be removed`);
Expand All @@ -1675,12 +1637,11 @@ class StateCacheStorage {
const filePath = path_1.default.join(tmpDir, STATE_FILE);
unlinkSafely(filePath);
try {
const cacheExists = yield checkIfCacheExists(CACHE_KEY);
const cacheExists = yield cache.restoreCache([path_1.default.dirname(filePath)], CACHE_KEY);
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);
if (!fs_1.default.existsSync(filePath)) {
core.warning('Unknown error when unpacking the cache, the process starts from the first issue.');
return '';
Expand Down
52 changes: 4 additions & 48 deletions src/classes/state/state-cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import fs from 'fs';
import path from 'path';
import os from 'os';
import * as core from '@actions/core';
import {context, getOctokit} from '@actions/github';
import {retry as octokitRetry} from '@octokit/plugin-retry';
import * as cache from '@actions/cache';

const CACHE_KEY = '_state';
Expand All @@ -25,56 +23,13 @@ const unlinkSafely = (filePath: string) => {
}
};

const getOctokitClient = () => {
const token = core.getInput('repo-token');
return getOctokit(token, undefined, octokitRetry);
};

const checkIfCacheExists = async (cacheKey: string): Promise<boolean> => {
const client = getOctokitClient();
try {
const issueResult = await client.request(
`/repos/${context.repo.owner}/${context.repo.repo}/actions/caches`
);
const caches: Array<{key?: string}> =
issueResult.data['actions_caches'] || [];
return Boolean(caches.find(cache => cache['key'] === cacheKey));
} catch (error) {
core.debug(`Error checking if cache exist: ${error.message}`);
}
return false;
};
const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
const client = getOctokitClient();
core.debug(`remove cache "${cacheKey}"`);
try {
// TODO: replace with client.rest.
await client.request(
`DELETE /repos/${context.repo.owner}/${context.repo.repo}/actions/caches?key=${cacheKey}`
);
} catch (error) {
if (error.status) {
core.warning(
`Error delete ${cacheKey}: [${error.status}] ${
error.message || 'Unknown reason'
}`
);
} else {
throw error;
}
}
};
export class StateCacheStorage implements IStateStorage {
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);
if (cacheExists) {
await resetCacheWithOctokit(CACHE_KEY);
}
const fileSize = fs.statSync(filePath).size;

if (fileSize === 0) {
Expand All @@ -99,16 +54,17 @@ export class StateCacheStorage implements IStateStorage {
const filePath = path.join(tmpDir, STATE_FILE);
unlinkSafely(filePath);
try {
const cacheExists = await checkIfCacheExists(CACHE_KEY);
const cacheExists = await cache.restoreCache(
[path.dirname(filePath)],
CACHE_KEY
);
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);

if (!fs.existsSync(filePath)) {
core.warning(
'Unknown error when unpacking the cache, the process starts from the first issue.'
Expand Down