From 19b0faf98248dd5e6eb90696c68f9f0dbc363b3c Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Thu, 20 Jul 2023 21:30:10 +0200 Subject: [PATCH 1/2] do not restore if caches does not exist --- dist/index.js | 33 ++++++++++++++----- src/classes/state/state-cache-storage.ts | 40 +++++++++++++++++++----- src/classes/state/state.ts | 7 +++-- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0c7b9f79..ef5e92cc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1611,15 +1611,28 @@ const unlinkSafely = (filePath) => { /* ignore */ } }; -const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () { +const getOctokitClient = () => { const token = core.getInput('repo-token'); - const client = (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry); - // TODO: better way to get repository? - const repo = process.env['GITHUB_REPOSITORY']; + return (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry); +}; +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}"`); try { // 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) { if (error.status) { @@ -1659,9 +1672,14 @@ class StateCacheStorage { const filePath = path_1.default.join(tmpDir, STATE_FILE); unlinkSafely(filePath); 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); 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 fs_1.default.readFileSync(path_1.default.join(tmpDir, STATE_FILE), { @@ -1766,7 +1784,8 @@ class State { this.reset(); const serialized = yield this.stateStorage.restore(); this.deserialize(serialized); - core.info(`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`); + if (this.processedIssuesIDs.size > 0) + core.info(`state: restored with info about ${this.processedIssuesIDs.size} issue(s)`); }); } } diff --git a/src/classes/state/state-cache-storage.ts b/src/classes/state/state-cache-storage.ts index c4e72b83..96c98326 100644 --- a/src/classes/state/state-cache-storage.ts +++ b/src/classes/state/state-cache-storage.ts @@ -3,7 +3,7 @@ import fs from 'fs'; import path from 'path'; import os from 'os'; 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 * as cache from '@actions/cache'; @@ -25,16 +25,32 @@ const unlinkSafely = (filePath: string) => { } }; -const resetCacheWithOctokit = async (cacheKey: string): Promise => { +const getOctokitClient = () => { const token = core.getInput('repo-token'); - const client = getOctokit(token, undefined, octokitRetry); - // TODO: better way to get repository? - const repo = process.env['GITHUB_REPOSITORY']; + return getOctokit(token, undefined, octokitRetry); +}; + +const checkCacheExist = async (cacheKey: string): Promise => { + 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 => { + const client = getOctokitClient(); core.debug(`remove cache "${cacheKey}"`); try { // TODO: replace with client.rest. await client.request( - `DELETE /repos/${repo}/actions/caches?key=${cacheKey}` + `DELETE /repos/${context.repo.owner}/${context.repo.repo}/actions/caches?key=${cacheKey}` ); } catch (error) { if (error.status) { @@ -76,11 +92,19 @@ export class StateCacheStorage implements IStateStorage { const filePath = path.join(tmpDir, STATE_FILE); unlinkSafely(filePath); 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); if (!fs.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 ''; } diff --git a/src/classes/state/state.ts b/src/classes/state/state.ts index c734d8c8..bf7b599f 100644 --- a/src/classes/state/state.ts +++ b/src/classes/state/state.ts @@ -64,8 +64,9 @@ export class State implements IState { this.reset(); const serialized = await this.stateStorage.restore(); this.deserialize(serialized); - core.info( - `state: restored with info about ${this.processedIssuesIDs.size} issue(s)` - ); + if (this.processedIssuesIDs.size > 0) + core.info( + `state: restored with info about ${this.processedIssuesIDs.size} issue(s)` + ); } } From dbb041abe2ae3e1304430c47859699e948cf285f Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Tue, 25 Jul 2023 11:48:11 +0200 Subject: [PATCH 2/2] Fix spelling --- dist/index.js | 8 ++++---- src/classes/state/state-cache-storage.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index ef5e92cc..343c8313 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1615,7 +1615,7 @@ const getOctokitClient = () => { const token = core.getInput('repo-token'); return (0, github_1.getOctokit)(token, undefined, plugin_retry_1.retry); }; -const checkCacheExist = (cacheKey) => __awaiter(void 0, void 0, void 0, function* () { +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`); @@ -1623,7 +1623,7 @@ const checkCacheExist = (cacheKey) => __awaiter(void 0, void 0, void 0, function return Boolean(caches.find(cache => cache['key'] === cacheKey)); } catch (error) { - core.debug(`$Error checking if cache exist: ${error.message}`); + core.debug(`Error checking if cache exist: ${error.message}`); } return false; }); @@ -1672,8 +1672,8 @@ class StateCacheStorage { const filePath = path_1.default.join(tmpDir, STATE_FILE); unlinkSafely(filePath); try { - const cacheExist = yield checkCacheExist(CACHE_KEY); - if (!cacheExist) { + const cacheExists = yield checkIfCacheExists(CACHE_KEY); + if (!cacheExists) { core.info('The saved state was not found, the process starts from the first issue.'); return ''; } diff --git a/src/classes/state/state-cache-storage.ts b/src/classes/state/state-cache-storage.ts index 96c98326..848872a3 100644 --- a/src/classes/state/state-cache-storage.ts +++ b/src/classes/state/state-cache-storage.ts @@ -30,7 +30,7 @@ const getOctokitClient = () => { return getOctokit(token, undefined, octokitRetry); }; -const checkCacheExist = async (cacheKey: string): Promise => { +const checkIfCacheExists = async (cacheKey: string): Promise => { const client = getOctokitClient(); try { const issueResult = await client.request( @@ -40,7 +40,7 @@ const checkCacheExist = async (cacheKey: string): Promise => { issueResult.data['actions_caches'] || []; return Boolean(caches.find(cache => cache['key'] === cacheKey)); } catch (error) { - core.debug(`$Error checking if cache exist: ${error.message}`); + core.debug(`Error checking if cache exist: ${error.message}`); } return false; }; @@ -92,8 +92,8 @@ export class StateCacheStorage implements IStateStorage { const filePath = path.join(tmpDir, STATE_FILE); unlinkSafely(filePath); try { - const cacheExist = await checkCacheExist(CACHE_KEY); - if (!cacheExist) { + const cacheExists = await checkIfCacheExists(CACHE_KEY); + if (!cacheExists) { core.info( 'The saved state was not found, the process starts from the first issue.' );