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 ( ) ;
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
const version = await gpg . getVersion ( ) ;
2020-05-04 16:17:14 +02:00
const dirs = await gpg . getDirs ( ) ;
2021-09-05 01:00:24 +02:00
await core . group ( ` GnuPG info ` , async ( ) = > {
2021-08-10 08:46:50 +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-09-06 22:03:16 +02:00
const privateKey = await openpgp . readPrivateKey ( inputs . gpgPrivateKey ) ;
2021-09-05 01:00:24 +02:00
await core . group ( ` GPG private key info ` , async ( ) = > {
2021-08-10 08:46:50 +02:00
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 } ` ) ;
} ) ;
2021-10-15 13:40:04 +02:00
let fingerprint = privateKey . fingerprint ;
if ( inputs . fingerprint ) {
fingerprint = inputs . fingerprint ;
}
stateHelper . setFingerprint ( fingerprint ) ;
await core . group ( ` Fingerprint to use ` , async ( ) = > {
core . info ( fingerprint ) ;
} ) ;
2021-09-05 01:00:24 +02:00
await core . group ( ` Importing GPG private key ` , async ( ) = > {
2021-08-10 08:46:50 +02:00
await gpg . importKey ( inputs . gpgPrivateKey ) . then ( stdout = > {
core . info ( stdout ) ;
} ) ;
2020-05-04 16:17:14 +02:00
} ) ;
2020-09-06 22:03:16 +02:00
if ( inputs . passphrase ) {
2021-09-05 01:00:24 +02:00
core . info ( 'Configuring GnuPG agent' ) ;
2020-05-04 16:17:14 +02:00
await gpg . configureAgent ( gpg . agentConfig ) ;
2021-09-05 01:00:24 +02:00
await core . group ( ` Getting keygrips ` , async ( ) = > {
2021-10-15 13:40:04 +02:00
for ( let keygrip of await gpg . getKeygrips ( fingerprint ) ) {
2021-09-05 01:00:24 +02:00
core . info ( ` Presetting passphrase for ${ keygrip } ` ) ;
2021-08-10 08:46:50 +02:00
await gpg . presetPassphrase ( keygrip , inputs . passphrase ) . then ( stdout = > {
core . debug ( stdout ) ;
} ) ;
}
} ) ;
2020-05-04 16:17:14 +02:00
}
2020-05-04 20:59:11 +02:00
2021-10-15 13:40:04 +02:00
await core . group ( ` Setting outputs ` , async ( ) = > {
core . info ( ` fingerprint= ${ fingerprint } ` ) ;
context . setOutput ( 'fingerprint' , fingerprint ) ;
core . info ( ` keyid= ${ privateKey . keyID } ` ) ;
context . setOutput ( 'keyid' , privateKey . keyID ) ;
core . info ( ` name= ${ privateKey . name } ` ) ;
context . setOutput ( 'name' , privateKey . name ) ;
core . info ( ` email= ${ privateKey . email } ` ) ;
context . setOutput ( 'email' , privateKey . email ) ;
} ) ;
2020-05-07 20:42:27 +02:00
2020-09-06 22:03:16 +02:00
if ( inputs . gitUserSigningkey ) {
2021-09-05 01:00:24 +02:00
core . info ( 'Setting GPG signing keyID for this Git repository' ) ;
2021-08-10 09:28:13 +02:00
await git . setConfig ( 'user.signingkey' , privateKey . keyID , inputs . gitConfigGlobal ) ;
2020-05-06 01:15:33 +02:00
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
2021-09-05 01:00:24 +02:00
core . info ( ` Configuring Git committer ( ${ userName } < ${ userEmail } >) ` ) ;
2021-08-10 09:28:13 +02:00
await git . setConfig ( 'user.name' , userName , inputs . gitConfigGlobal ) ;
await git . setConfig ( 'user.email' , userEmail , inputs . gitConfigGlobal ) ;
2020-05-05 20:01:45 +02:00
2020-09-06 22:03:16 +02:00
if ( inputs . gitCommitGpgsign ) {
2021-09-05 01:00:24 +02:00
core . info ( 'Sign all commits automatically' ) ;
2021-08-10 09:28:13 +02:00
await git . setConfig ( 'commit.gpgsign' , 'true' , inputs . gitConfigGlobal ) ;
2020-05-06 01:15:33 +02:00
}
2020-09-06 22:03:16 +02:00
if ( inputs . gitTagGpgsign ) {
2021-09-05 01:00:24 +02:00
core . info ( 'Sign all tags automatically' ) ;
2021-08-10 09:28:13 +02:00
await git . setConfig ( 'tag.gpgsign' , 'true' , inputs . gitConfigGlobal ) ;
2020-05-06 01:15:33 +02:00
}
2020-09-06 22:03:16 +02:00
if ( inputs . gitPushGpgsign ) {
2021-09-05 01:00:24 +02:00
core . info ( 'Sign all pushes automatically' ) ;
2021-08-10 09:28:13 +02:00
await git . setConfig ( 'push.gpgsign' , inputs . gitPushGpgsign , inputs . gitConfigGlobal ) ;
2020-05-06 01:15:33 +02:00
}
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 > {
2021-10-15 13:40:04 +02:00
if ( stateHelper . fingerprint . length <= 0 ) {
core . debug ( 'Fingerprint is not defined. Skipping cleanup.' ) ;
2020-05-03 20:46:05 +02:00
return ;
}
try {
2021-09-05 01:00:24 +02:00
core . info ( 'Removing keys' ) ;
2021-10-15 13:40:04 +02:00
await gpg . deleteKey ( stateHelper . fingerprint ) ;
2020-05-06 00:23:29 +02:00
2021-09-05 01:00:24 +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 ( ) ;
2020-09-06 22:03:16 +02:00
} else {
2020-05-03 20:46:05 +02:00
cleanup ( ) ;
}