import-gpg/src/main.ts

119 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-05-03 20:46:05 +02:00
import * as core from '@actions/core';
import * as context from './context';
import * as git from './git';
2020-05-03 21:33:19 +02:00
import * as gpg from './gpg';
import * as openpgp from './openpgp';
2020-05-03 20:46:05 +02:00
import * as stateHelper from './state-helper';
async function run(): Promise<void> {
try {
let inputs: context.Inputs = await context.getInputs();
stateHelper.setGpgPrivateKey(inputs.gpgPrivateKey);
2020-05-03 20:46:05 +02:00
if (inputs.workdir && inputs.workdir !== '.') {
core.info(`📂 Using ${inputs.workdir} as working directory...`);
process.chdir(inputs.workdir);
2020-08-28 16:30:49 -04:00
}
2020-05-03 21:33:19 +02:00
const version = await gpg.getVersion();
const dirs = await gpg.getDirs();
await core.group(`GnuPG info`, async () => {
core.info(`Version : ${version.gnupg} (libgcrypt ${version.libgcrypt})`);
core.info(`Libdir : ${dirs.libdir}`);
core.info(`Libexecdir : ${dirs.libexecdir}`);
core.info(`Datadir : ${dirs.datadir}`);
core.info(`Homedir : ${dirs.homedir}`);
});
2020-05-03 20:46:05 +02:00
const privateKey = await openpgp.readPrivateKey(inputs.gpgPrivateKey);
await core.group(`GPG private key info`, async () => {
core.info(`Fingerprint : ${privateKey.fingerprint}`);
core.info(`KeyID : ${privateKey.keyID}`);
core.info(`Name : ${privateKey.name}`);
core.info(`Email : ${privateKey.email}`);
core.info(`CreationTime : ${privateKey.creationTime}`);
});
await core.group(`Importing GPG private key`, async () => {
await gpg.importKey(inputs.gpgPrivateKey).then(stdout => {
core.info(stdout);
});
});
if (inputs.passphrase) {
core.info('Configuring GnuPG agent');
await gpg.configureAgent(gpg.agentConfig);
await core.group(`Getting keygrips`, async () => {
for (let keygrip of await gpg.getKeygrips(privateKey.fingerprint)) {
core.info(`Presetting passphrase for ${keygrip}`);
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
core.debug(stdout);
});
}
});
}
core.info('Setting outputs');
context.setOutput('fingerprint', privateKey.fingerprint);
context.setOutput('keyid', privateKey.keyID);
context.setOutput('name', privateKey.name);
context.setOutput('email', privateKey.email);
if (inputs.gitUserSigningkey) {
core.info('Setting GPG signing keyID for this Git repository');
await git.setConfig('user.signingkey', privateKey.keyID, inputs.gitConfigGlobal);
const userEmail = inputs.gitCommitterEmail || privateKey.email;
const userName = inputs.gitCommitterName || privateKey.name;
if (userEmail != privateKey.email) {
core.setFailed(`Committer email "${inputs.gitCommitterEmail}" (name: "${inputs.gitCommitterName}") does not match GPG private key email "${privateKey.email}" (name: "${privateKey.name}")`);
return;
}
core.info(`Configuring Git committer (${userName} <${userEmail}>)`);
await git.setConfig('user.name', userName, inputs.gitConfigGlobal);
await git.setConfig('user.email', userEmail, inputs.gitConfigGlobal);
if (inputs.gitCommitGpgsign) {
core.info('Sign all commits automatically');
await git.setConfig('commit.gpgsign', 'true', inputs.gitConfigGlobal);
}
if (inputs.gitTagGpgsign) {
core.info('Sign all tags automatically');
await git.setConfig('tag.gpgsign', 'true', inputs.gitConfigGlobal);
}
if (inputs.gitPushGpgsign) {
core.info('Sign all pushes automatically');
await git.setConfig('push.gpgsign', inputs.gitPushGpgsign, inputs.gitConfigGlobal);
}
}
2020-05-03 20:46:05 +02:00
} catch (error) {
core.setFailed(error.message);
}
}
async function cleanup(): Promise<void> {
if (stateHelper.gpgPrivateKey.length <= 0) {
core.debug('GPG private key is not defined. Skipping cleanup.');
2020-05-03 20:46:05 +02:00
return;
}
try {
core.info('Removing keys');
const privateKey = await openpgp.readPrivateKey(stateHelper.gpgPrivateKey);
2020-05-03 21:33:19 +02:00
await gpg.deleteKey(privateKey.fingerprint);
2020-05-06 00:23:29 +02:00
core.info('Killing GnuPG agent');
2020-05-06 00:23:29 +02:00
await gpg.killAgent();
2020-05-03 20:46:05 +02:00
} catch (error) {
core.warning(error.message);
}
}
if (!stateHelper.IsPost) {
run();
} else {
2020-05-03 20:46:05 +02:00
cleanup();
}