do not restore if caches does not exist
This commit is contained in:
parent
47ab9e7777
commit
19b0faf982
|
@ -1611,15 +1611,28 @@ const unlinkSafely = (filePath) => {
|
||||||
/* ignore */
|
/* ignore */
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () {
|
const getOctokitClient = () => {
|
||||||
const token = core.getInput('repo-token');
|
const token = core.getInput('repo-token');
|
||||||
const client = (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry);
|
return (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry);
|
||||||
// TODO: better way to get repository?
|
};
|
||||||
const repo = process.env['GITHUB_REPOSITORY'];
|
const checkCacheExist = (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}"`);
|
core.debug(`remove cache "${cacheKey}"`);
|
||||||
try {
|
try {
|
||||||
// TODO: replace with client.rest.
|
// TODO: replace with client.rest.
|
||||||
yield client.request(`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`);
|
yield client.request(`DELETE /repos/${github_1.context.repo.owner}/${github_1.context.repo.repo}/actions/caches?key=${cacheKey}`);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
if (error.status) {
|
if (error.status) {
|
||||||
|
@ -1659,9 +1672,14 @@ class StateCacheStorage {
|
||||||
const filePath = path_1.default.join(tmpDir, STATE_FILE);
|
const filePath = path_1.default.join(tmpDir, STATE_FILE);
|
||||||
unlinkSafely(filePath);
|
unlinkSafely(filePath);
|
||||||
try {
|
try {
|
||||||
|
const cacheExist = yield checkCacheExist(CACHE_KEY);
|
||||||
|
if (!cacheExist) {
|
||||||
|
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)], CACHE_KEY);
|
||||||
if (!fs_1.default.existsSync(filePath)) {
|
if (!fs_1.default.existsSync(filePath)) {
|
||||||
core.info('The stored state has not been found, probably because of the very first run or the previous run failed');
|
core.warning('Unknown error when unpacking the cache, the process starts from the first issue.');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return fs_1.default.readFileSync(path_1.default.join(tmpDir, STATE_FILE), {
|
return fs_1.default.readFileSync(path_1.default.join(tmpDir, STATE_FILE), {
|
||||||
|
@ -1766,6 +1784,7 @@ class State {
|
||||||
this.reset();
|
this.reset();
|
||||||
const serialized = yield this.stateStorage.restore();
|
const serialized = yield this.stateStorage.restore();
|
||||||
this.deserialize(serialized);
|
this.deserialize(serialized);
|
||||||
|
if (this.processedIssuesIDs.size > 0)
|
||||||
core.info(`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`);
|
core.info(`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import {getOctokit} from '@actions/github';
|
import {context, getOctokit} from '@actions/github';
|
||||||
import {retry as octokitRetry} from '@octokit/plugin-retry';
|
import {retry as octokitRetry} from '@octokit/plugin-retry';
|
||||||
import * as cache from '@actions/cache';
|
import * as cache from '@actions/cache';
|
||||||
|
|
||||||
|
@ -25,16 +25,32 @@ const unlinkSafely = (filePath: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
|
const getOctokitClient = () => {
|
||||||
const token = core.getInput('repo-token');
|
const token = core.getInput('repo-token');
|
||||||
const client = getOctokit(token, undefined, octokitRetry);
|
return getOctokit(token, undefined, octokitRetry);
|
||||||
// TODO: better way to get repository?
|
};
|
||||||
const repo = process.env['GITHUB_REPOSITORY'];
|
|
||||||
|
const checkCacheExist = 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}"`);
|
core.debug(`remove cache "${cacheKey}"`);
|
||||||
try {
|
try {
|
||||||
// TODO: replace with client.rest.
|
// TODO: replace with client.rest.
|
||||||
await client.request(
|
await client.request(
|
||||||
`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`
|
`DELETE /repos/${context.repo.owner}/${context.repo.repo}/actions/caches?key=${cacheKey}`
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.status) {
|
if (error.status) {
|
||||||
|
@ -76,11 +92,19 @@ export class StateCacheStorage implements IStateStorage {
|
||||||
const filePath = path.join(tmpDir, STATE_FILE);
|
const filePath = path.join(tmpDir, STATE_FILE);
|
||||||
unlinkSafely(filePath);
|
unlinkSafely(filePath);
|
||||||
try {
|
try {
|
||||||
|
const cacheExist = await checkCacheExist(CACHE_KEY);
|
||||||
|
if (!cacheExist) {
|
||||||
|
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)], CACHE_KEY);
|
||||||
|
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!fs.existsSync(filePath)) {
|
||||||
core.info(
|
core.warning(
|
||||||
'The stored state has not been found, probably because of the very first run or the previous run failed'
|
'Unknown error when unpacking the cache, the process starts from the first issue.'
|
||||||
);
|
);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ export class State implements IState {
|
||||||
this.reset();
|
this.reset();
|
||||||
const serialized = await this.stateStorage.restore();
|
const serialized = await this.stateStorage.restore();
|
||||||
this.deserialize(serialized);
|
this.deserialize(serialized);
|
||||||
|
if (this.processedIssuesIDs.size > 0)
|
||||||
core.info(
|
core.info(
|
||||||
`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`
|
`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue