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 {
2022-03-15 07:56:21 +01:00
const 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 !== '.' ) {
2021-11-19 12:54:41 +01:00
core . info ( ` Using ${ inputs . workdir } as working directory... ` ) ;
2020-09-06 22:03:16 +02:00
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 } ` ) ;
} ) ;
2022-03-03 13:48:37 +00:00
stateHelper . setFingerprint ( privateKey . fingerprint ) ;
2021-10-15 13:40:04 +02:00
let fingerprint = privateKey . fingerprint ;
if ( inputs . fingerprint ) {
fingerprint = inputs . fingerprint ;
}
2022-03-03 13:48:37 +00:00
2021-10-15 13:40:04 +02:00
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
} ) ;
2022-02-28 15:36:54 +00:00
if ( inputs . passphrase && ! inputs . fingerprint ) {
// Set the passphrase for all subkeys
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 ( ) = > {
2022-03-15 07:56:21 +01:00
for ( const 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
2022-02-28 15:36:54 +00:00
if ( inputs . passphrase && inputs . fingerprint ) {
// Set the passphrase only for the subkey specified in the input `fingerprint`
core . info ( 'Configuring GnuPG agent' ) ;
await gpg . configureAgent ( gpg . agentConfig ) ;
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 ) ;
} ) ;
} ) ;
}
2023-05-06 18:20:11 +02:00
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 } ` ) ;
} ) ;
} ) ;
}
2021-10-15 13:40:04 +02:00
await core . group ( ` Setting outputs ` , async ( ) = > {
core . info ( ` fingerprint= ${ fingerprint } ` ) ;
2022-10-18 23:40:13 +02:00
core . setOutput ( 'fingerprint' , fingerprint ) ;
2021-10-15 13:40:04 +02:00
core . info ( ` keyid= ${ privateKey . keyID } ` ) ;
2022-10-18 23:40:13 +02:00
core . setOutput ( 'keyid' , privateKey . keyID ) ;
2021-10-15 13:40:04 +02:00
core . info ( ` name= ${ privateKey . name } ` ) ;
2022-10-18 23:40:13 +02:00
core . setOutput ( 'name' , privateKey . name ) ;
2021-10-15 13:40:04 +02:00
core . info ( ` email= ${ privateKey . email } ` ) ;
2022-10-18 23:40:13 +02:00
core . setOutput ( 'email' , privateKey . email ) ;
2021-10-15 13:40:04 +02:00
} ) ;
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 ) {
2022-03-03 13:48:37 +00:00
core . debug ( 'Primary key fingerprint is not defined. Skipping cleanup.' ) ;
2020-05-03 20:46:05 +02:00
return ;
}
try {
2022-03-03 13:48:37 +00:00
core . info ( ` Removing key ${ stateHelper . fingerprint } ` ) ;
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 ( ) ;
}