diff --git a/__tests__/.gpgtmp/private.asc b/__tests__/.gpgtmp/private.asc new file mode 100644 index 00000000..35e18e83 --- /dev/null +++ b/__tests__/.gpgtmp/private.asc @@ -0,0 +1 @@ +KEY CONTENTS \ No newline at end of file diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts index 4a56744e..b8e3eac6 100644 --- a/__tests__/auth.test.ts +++ b/__tests__/auth.test.ts @@ -22,8 +22,8 @@ import * as auth from '../src/auth'; const env = process.env; const m2Dir = path.join(__dirname, auth.M2_DIR); const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE); -const gpgDir = path.join(__dirname, auth.GPG_DIR); -const gpgFile = auth.GPG_FILE; +const privateKeyDir = path.join(__dirname, auth.PRIVATE_KEY_DIR); +const privateKeyFile = auth.PRIVATE_KEY_FILE; describe('auth tests', () => { beforeEach(async () => { @@ -33,7 +33,7 @@ describe('auth tests', () => { afterAll(async () => { try { await io.rmRF(m2Dir); - await io.rmRF(gpgDir); + await io.rmRF(privateKeyDir); } catch { console.log('Failed to remove test directories'); } @@ -182,11 +182,11 @@ describe('auth tests', () => { expect(exec.exec).toHaveBeenCalledWith( 'gpg', - ['--import', '--batch', gpgFile], - {cwd: gpgDir} + ['--import', '--batch', privateKeyFile], + {cwd: privateKeyDir} ); - expect(fs.existsSync(gpgDir)).toBe(false); + expect(fs.existsSync(privateKeyDir)).toBe(false); }, 100000); it('does not import gpg private key when private key is not set', async () => { @@ -198,10 +198,10 @@ describe('auth tests', () => { expect(exec.exec).not.toHaveBeenCalledWith( 'gpg', - ['--import', '--batch', gpgFile], - {cwd: gpgDir} + ['--import', '--batch', privateKeyFile], + {cwd: privateKeyDir} ); - expect(fs.existsSync(gpgDir)).toBe(false); + expect(fs.existsSync(privateKeyDir)).toBe(false); }, 100000); }); diff --git a/dist/index.js b/dist/index.js index de2b10d2..c1018c00 100644 Binary files a/dist/index.js and b/dist/index.js differ diff --git a/src/auth.ts b/src/auth.ts index 46fbb67f..6b40ccb3 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -7,8 +7,8 @@ import * as exec from '@actions/exec'; export const M2_DIR = '.m2'; export const SETTINGS_FILE = 'settings.xml'; -export const GPG_DIR = '.gpgtmp'; -export const GPG_FILE = 'private.asc'; +export const PRIVATE_KEY_DIR = '.keys'; +export const PRIVATE_KEY_FILE = 'private-key.asc'; export const DEFAULT_ID = 'github'; export const DEFAULT_USERNAME = 'GITHUB_ACTOR'; @@ -46,13 +46,15 @@ export async function configAuthentication( if (gpgPrivateKey !== DEFAULT_GPG_PRIVATE_KEY) { console.log('importing gpg key'); - const gpgDirectory: string = path.join(os.homedir(), GPG_DIR); - await io.mkdirP(gpgDirectory); - core.debug(`created directory ${gpgDirectory}`); - await write(gpgDirectory, GPG_FILE, gpgPrivateKey); - await importGpgKey(gpgDirectory, GPG_FILE); - await io.rmRF(gpgDirectory); - core.debug(`removed directory ${gpgDirectory}`); + const privateKeyDirectory: string = path.join( + os.homedir(), + PRIVATE_KEY_DIR + ); + await io.mkdirP(privateKeyDirectory); + core.debug(`created directory ${privateKeyDirectory}`); + await write(privateKeyDirectory, PRIVATE_KEY_FILE, gpgPrivateKey); + await importGpgKey(privateKeyDirectory, PRIVATE_KEY_FILE); + await remove(privateKeyDirectory); } } @@ -109,6 +111,11 @@ async function write(directory: string, file: string, contents: string) { }); } -async function importGpgKey(directory: string, file: string) { - exec.exec('gpg', ['--import', '--batch', file], {cwd: directory}); +async function remove(path: string) { + console.log(`removing ${path}`); + return io.rmRF(path); +} + +async function importGpgKey(directory: string, file: string) { + return exec.exec('gpg', ['--import', '--batch', file], {cwd: directory}); }