-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.ts
154 lines (135 loc) · 5.48 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as core from '@actions/core';
import * as context from './context';
import * as git from './git';
import * as gpg from './gpg';
import * as openpgp from './openpgp';
import * as stateHelper from './state-helper';
async function run(): Promise<void> {
try {
const inputs: context.Inputs = await context.getInputs();
if (inputs.workdir && inputs.workdir !== '.') {
core.info(`Using ${inputs.workdir} as working directory...`);
process.chdir(inputs.workdir);
}
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}`);
});
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}`);
});
stateHelper.setFingerprint(privateKey.fingerprint);
let fingerprint = privateKey.fingerprint;
if (inputs.fingerprint) {
fingerprint = inputs.fingerprint;
}
await core.group(`Fingerprint to use`, async () => {
core.info(fingerprint);
});
await core.group(`Importing GPG private key`, async () => {
await gpg.importKey(inputs.gpgPrivateKey).then(stdout => {
core.info(stdout);
});
});
if (inputs.passphrase) {
await core.group(`Configuring GnuPG agent`, async () => {
const gpgHome = await gpg.getHome();
core.info(`GnuPG home: ${gpgHome}`);
await gpg.configureAgent(gpgHome, gpg.agentConfig);
});
if (!inputs.fingerprint) {
// Set the passphrase for all subkeys
await core.group(`Getting keygrips`, async () => {
for (const keygrip of await gpg.getKeygrips(fingerprint)) {
core.info(`Presetting passphrase for ${keygrip}`);
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
core.debug(stdout);
});
}
});
} else {
// Set the passphrase only for the subkey specified in the input `fingerprint`
await core.group(`Getting keygrip for fingerprint`, async () => {
const keygrip = await gpg.getKeygrip(fingerprint);
core.info(`Presetting passphrase for key ${fingerprint} with keygrip ${keygrip}`);
await gpg.presetPassphrase(keygrip, inputs.passphrase).then(stdout => {
core.debug(stdout);
});
});
}
}
if (inputs.trustLevel) {
await core.group(`Setting key's trust level`, async () => {
await gpg.setTrustLevel(privateKey.keyID, inputs.trustLevel).then(() => {
core.info(`Trust level set to ${inputs.trustLevel} for ${privateKey.keyID}`);
});
});
}
await core.group(`Setting outputs`, async () => {
core.info(`fingerprint=${fingerprint}`);
core.setOutput('fingerprint', fingerprint);
core.info(`keyid=${privateKey.keyID}`);
core.setOutput('keyid', privateKey.keyID);
core.info(`name=${privateKey.name}`);
core.setOutput('name', privateKey.name);
core.info(`email=${privateKey.email}`);
core.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);
}
}
} catch (error) {
core.setFailed(error.message);
}
}
async function cleanup(): Promise<void> {
if (stateHelper.fingerprint.length <= 0) {
core.debug('Primary key fingerprint is not defined. Skipping cleanup.');
return;
}
try {
core.info(`Removing key ${stateHelper.fingerprint}`);
await gpg.deleteKey(stateHelper.fingerprint);
core.info('Killing GnuPG agent');
await gpg.killAgent();
} catch (error) {
core.warning(error.message);
}
}
if (!stateHelper.IsPost) {
run();
} else {
cleanup();
}