2020-05-03 20:46:05 +02:00
import * as core from '@actions/core' ;
2020-09-06 22:03:16 +02:00
import * as context from './context' ;
2020-05-04 20:59:11 +02:00
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 {
2020-09-06 22:03:16 +02:00
let inputs : context.Inputs = await context . getInputs ( ) ;
stateHelper . setGpgPrivateKey ( inputs . gpgPrivateKey ) ;
2020-05-03 20:46:05 +02:00
2020-09-06 22:03:16 +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-05 20:01:45 +02:00
2020-05-03 21:33:19 +02:00
core . info ( '📣 GnuPG info' ) ;
const version = await gpg . getVersion ( ) ;
2020-05-04 16:17:14 +02:00
const dirs = await gpg . getDirs ( ) ;
2020-05-04 16:40:21 +02:00
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
2020-05-06 01:15:33 +02:00
core . info ( '🔮 Checking GPG private key' ) ;
2020-09-06 22:03:16 +02:00
const privateKey = await openpgp . readPrivateKey ( inputs . gpgPrivateKey ) ;
2020-05-04 16:17:14 +02:00
core . debug ( ` Fingerprint : ${ privateKey . fingerprint } ` ) ;
core . debug ( ` KeyID : ${ privateKey . keyID } ` ) ;
2020-05-05 20:01:45 +02:00
core . debug ( ` Name : ${ privateKey . name } ` ) ;
core . debug ( ` Email : ${ privateKey . email } ` ) ;
2020-05-04 16:17:14 +02:00
core . debug ( ` CreationTime : ${ privateKey . creationTime } ` ) ;
2020-05-03 20:46:05 +02:00
2020-05-06 01:15:33 +02:00
core . info ( '🔑 Importing GPG private key' ) ;
2020-09-06 22:03:16 +02:00
await gpg . importKey ( inputs . gpgPrivateKey ) . then ( stdout = > {
2020-05-04 16:17:14 +02:00
core . debug ( stdout ) ;
} ) ;
2020-09-06 22:03:16 +02:00
if ( inputs . passphrase ) {
2020-05-04 20:09:52 +02:00
core . info ( '⚙️ Configuring GnuPG agent' ) ;
2020-05-04 16:17:14 +02:00
await gpg . configureAgent ( gpg . agentConfig ) ;
2020-09-03 17:19:11 +02:00
core . info ( '📌 Getting keygrips' ) ;
for ( let keygrip of await gpg . getKeygrips ( privateKey . fingerprint ) ) {
core . info ( ` 🔓 Presetting passphrase for ${ keygrip } ` ) ;
2020-09-06 22:03:16 +02:00
await gpg . presetPassphrase ( keygrip , inputs . passphrase ) . then ( stdout = > {
2020-09-03 17:19:11 +02:00
core . debug ( stdout ) ;
} ) ;
}
2020-05-04 16:17:14 +02:00
}
2020-05-04 20:59:11 +02:00
2020-05-07 20:42:27 +02:00
core . info ( '🛒 Setting outputs...' ) ;
core . setOutput ( 'fingerprint' , privateKey . fingerprint ) ;
core . setOutput ( 'keyid' , privateKey . keyID ) ;
2020-05-12 20:18:51 +02:00
core . setOutput ( 'name' , privateKey . name ) ;
2020-05-12 20:48:57 +02:00
core . setOutput ( 'email' , privateKey . email ) ;
2020-05-07 20:42:27 +02:00
2020-09-06 22:03:16 +02:00
if ( inputs . gitUserSigningkey ) {
2020-05-06 01:15:33 +02:00
core . info ( '🔐 Setting GPG signing keyID for this Git repository' ) ;
await git . setConfig ( 'user.signingkey' , privateKey . keyID ) ;
2020-09-06 22:03:16 +02:00
const userEmail = inputs . gitCommitterEmail || privateKey . email ;
const userName = inputs . gitCommitterName || privateKey . name ;
2020-05-12 20:18:51 +02:00
2020-09-06 22:03:16 +02:00
if ( userEmail != privateKey . email ) {
2020-11-24 06:03:54 -06:00
core . setFailed ( ` Committer email " ${ inputs . gitCommitterEmail } " (name: " ${ inputs . gitCommitterName } ") does not match GPG private key email " ${ privateKey . email } " (name: " ${ privateKey . name } ") ` ) ;
2020-05-05 20:01:45 +02:00
return ;
}
2020-05-06 01:15:33 +02:00
2020-09-06 22:03:16 +02:00
core . info ( ` 🔨 Configuring Git committer ( ${ userName } < ${ userEmail } >) ` ) ;
await git . setConfig ( 'user.name' , userName ) ;
await git . setConfig ( 'user.email' , userEmail ) ;
2020-05-05 20:01:45 +02:00
2020-09-06 22:03:16 +02:00
if ( inputs . gitCommitGpgsign ) {
2020-05-06 01:15:33 +02:00
core . info ( '💎 Sign all commits automatically' ) ;
await git . setConfig ( 'commit.gpgsign' , 'true' ) ;
}
2020-09-06 22:03:16 +02:00
if ( inputs . gitTagGpgsign ) {
2020-05-06 01:15:33 +02:00
core . info ( '💎 Sign all tags automatically' ) ;
await git . setConfig ( 'tag.gpgsign' , 'true' ) ;
}
2020-09-06 22:03:16 +02:00
if ( inputs . gitPushGpgsign ) {
2020-05-06 01:15:33 +02:00
core . info ( '💎 Sign all pushes automatically' ) ;
await git . setConfig ( 'push.gpgsign' , 'true' ) ;
}
2020-05-04 20:59:11 +02:00
}
2020-05-03 20:46:05 +02:00
} catch ( error ) {
core . setFailed ( error . message ) ;
}
}
async function cleanup ( ) : Promise < void > {
2020-09-06 22:03:16 +02:00
if ( stateHelper . gpgPrivateKey . length <= 0 ) {
2020-05-06 01:15:33 +02:00
core . debug ( 'GPG private key is not defined. Skipping cleanup.' ) ;
2020-05-03 20:46:05 +02:00
return ;
}
try {
2020-05-04 20:09:52 +02:00
core . info ( '🚿 Removing keys' ) ;
2020-09-06 22:03:16 +02:00
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' ) ;
await gpg . killAgent ( ) ;
2020-05-03 20:46:05 +02:00
} catch ( error ) {
core . warning ( error . message ) ;
}
}
if ( ! stateHelper . IsPost ) {
run ( ) ;
2020-09-06 22:03:16 +02:00
} else {
2020-05-03 20:46:05 +02:00
cleanup ( ) ;
}