2023-06-23 23:13:39 +02:00
|
|
|
import {IStateStorage} from '../../interfaces/state/state-storage';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import os from 'os';
|
|
|
|
|
import * as core from '@actions/core';
|
2023-07-05 10:37:35 +02:00
|
|
|
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';
|
|
|
|
|
*/
|
2023-06-23 23:13:39 +02:00
|
|
|
|
|
|
|
|
const CACHE_KEY = '_state';
|
|
|
|
|
const CACHE_VERSION = '1';
|
|
|
|
|
const STATE_FILE = 'state.txt';
|
|
|
|
|
export class StateCacheStorage implements IStateStorage {
|
|
|
|
|
async save(serializedState: string): Promise<void> {
|
|
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'state-'));
|
|
|
|
|
const file = path.join(tmpDir, STATE_FILE);
|
|
|
|
|
fs.writeFileSync(file, serializedState);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await uploadFileToActionsCache(file, CACHE_KEY, CACHE_VERSION);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.warning(
|
|
|
|
|
`Saving the state was not successful due to "${
|
|
|
|
|
error.message || 'unknown reason'
|
|
|
|
|
}"`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async restore(): Promise<string> {
|
|
|
|
|
const tmpDir = fs.mkdtempSync('state-');
|
|
|
|
|
const fileName = path.join(tmpDir, STATE_FILE);
|
|
|
|
|
try {
|
2023-07-04 18:29:58 +02:00
|
|
|
await downloadFileFromActionsCache(fileName, CACHE_KEY, CACHE_VERSION);
|
2023-06-23 23:13:39 +02:00
|
|
|
if (!fs.existsSync(fileName)) {
|
|
|
|
|
core.info(
|
2023-07-04 18:29:58 +02:00
|
|
|
'The stored state has not been found, probably because of the very first run or the previous run failed'
|
2023-06-23 23:13:39 +02:00
|
|
|
);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return fs.readFileSync(path.join(tmpDir, STATE_FILE), {
|
|
|
|
|
encoding: 'utf8'
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.warning(
|
|
|
|
|
`Restoring the state was not successful due to "${
|
|
|
|
|
error.message || 'unknown reason'
|
|
|
|
|
}"`
|
|
|
|
|
);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|