2020-05-02 11:33:15 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as gpg from './gpg';
|
2020-07-16 01:53:39 +00:00
|
|
|
import * as constants from './constants';
|
2023-03-09 12:49:35 +00:00
|
|
|
import {isJobStatusSuccess} from './util';
|
|
|
|
import {save} from './cache';
|
2020-05-02 11:33:15 +00:00
|
|
|
|
2021-08-19 17:19:35 +00:00
|
|
|
async function removePrivateKeyFromKeychain() {
|
2023-03-09 12:49:35 +00:00
|
|
|
if (core.getInput(constants.INPUT_GPG_PRIVATE_KEY, {required: false})) {
|
2021-04-05 10:02:27 +00:00
|
|
|
core.info('Removing private key from keychain');
|
2020-05-02 11:33:15 +00:00
|
|
|
try {
|
2023-03-09 12:49:35 +00:00
|
|
|
const keyFingerprint = core.getState(
|
|
|
|
constants.STATE_GPG_PRIVATE_KEY_FINGERPRINT
|
|
|
|
);
|
2020-05-02 11:33:15 +00:00
|
|
|
await gpg.deleteKey(keyFingerprint);
|
|
|
|
} catch (error) {
|
2021-09-24 09:53:06 +00:00
|
|
|
core.setFailed(`Failed to remove private key due to: ${error.message}`);
|
2020-05-02 11:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:19:35 +00:00
|
|
|
/**
|
|
|
|
* Check given input and run a save process for the specified package manager
|
|
|
|
* @returns Promise that will be resolved when the save process finishes
|
|
|
|
*/
|
|
|
|
async function saveCache() {
|
|
|
|
const jobStatus = isJobStatusSuccess();
|
|
|
|
const cache = core.getInput(constants.INPUT_CACHE);
|
|
|
|
return jobStatus && cache ? save(cache) : Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The save process is best-effort, and it should not make the workflow fail
|
|
|
|
* even though this process throws an error.
|
|
|
|
* @param promise the promise to ignore error from
|
|
|
|
* @returns Promise that will ignore error reported by the given promise
|
|
|
|
*/
|
|
|
|
async function ignoreError(promise: Promise<void>) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
promise
|
|
|
|
.catch(error => {
|
|
|
|
core.warning(error);
|
|
|
|
resolve(void 0);
|
|
|
|
})
|
|
|
|
.then(resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function run() {
|
|
|
|
await removePrivateKeyFromKeychain();
|
|
|
|
await ignoreError(saveCache());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
// https://nodejs.org/api/modules.html#modules_accessing_the_main_module
|
|
|
|
core.info('the script is loaded as a module, so skipping the execution');
|
|
|
|
}
|