mirror of
https://github.com/actions/stale.git
synced 2025-12-11 12:37:27 +00:00
use highlevel actions cache api
This commit is contained in:
9
src/classes/actions-cache-hilevel/download.ts
Normal file
9
src/classes/actions-cache-hilevel/download.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import * as cache from '@actions/cache';
|
||||
import path from 'path';
|
||||
|
||||
export const downloadFileFromActionsCache = (
|
||||
destFileName: string,
|
||||
cacheKey: string,
|
||||
cacheVersion: string
|
||||
): Promise<void> =>
|
||||
cache.restoreCache([path.dirname(destFileName)], cacheKey) as Promise<void>;
|
||||
40
src/classes/actions-cache-hilevel/upload.ts
Normal file
40
src/classes/actions-cache-hilevel/upload.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import fs from 'fs';
|
||||
import * as core from '@actions/core';
|
||||
import * as cache from '@actions/cache';
|
||||
import {getOctokit} from '@actions/github';
|
||||
import {retry as octokitRetry} from '@octokit/plugin-retry';
|
||||
|
||||
const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
|
||||
const token = core.getInput('repo-token');
|
||||
const client = getOctokit(token, undefined, octokitRetry);
|
||||
// TODO: better way to get repository?
|
||||
const repo = process.env['GITHUB_REPOSITORY'];
|
||||
core.debug(`remove cache "${cacheKey}"`);
|
||||
try {
|
||||
// TODO: replace with client.rest.
|
||||
await client.request(
|
||||
`DELETE /repos/${repo}/actions/caches?key=${cacheKey}`
|
||||
);
|
||||
} catch (error) {
|
||||
if (error.status) {
|
||||
core.debug(`Cache ${cacheKey} does not exist`);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
export const uploadFileToActionsCache = async (
|
||||
filePath: string,
|
||||
cacheKey: string,
|
||||
cacheVersion: string
|
||||
) => {
|
||||
await resetCacheWithOctokit(cacheKey);
|
||||
const fileSize = fs.statSync(filePath).size;
|
||||
|
||||
if (fileSize === 0) {
|
||||
core.info(`the cache ${cacheKey} will be removed`);
|
||||
return;
|
||||
}
|
||||
|
||||
cache.saveCache([filePath], cacheKey);
|
||||
};
|
||||
@@ -3,8 +3,12 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import * as core from '@actions/core';
|
||||
import {uploadFileToActionsCache} from '../actions-cache/upload';
|
||||
import {downloadFileFromActionsCache} from '../actions-cache/download';
|
||||
import {downloadFileFromActionsCache} from '../actions-cache-hilevel/download';
|
||||
import {uploadFileToActionsCache} from '../actions-cache-hilevel/upload';
|
||||
/*
|
||||
import {uploadFileToActionsCache} from '../actions-cache-internal/upload';
|
||||
import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
|
||||
*/
|
||||
|
||||
const CACHE_KEY = '_state';
|
||||
const CACHE_VERSION = '1';
|
||||
|
||||
Reference in New Issue
Block a user