Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
- Loading branch information
Showing
14 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getName, mocked } from '../../../test/util'; | ||
import * as exec_ from '../../util/exec'; | ||
import { setPrivateKey, writePrivateKey } from './private-key'; | ||
|
||
jest.mock('fs-extra'); | ||
jest.mock('../../util/exec'); | ||
|
||
const exec = mocked(exec_); | ||
|
||
describe(getName(__filename), () => { | ||
describe('writePrivateKey()', () => { | ||
it('returns if no private key', async () => { | ||
await expect(writePrivateKey('/tmp/some-repo')).resolves.not.toThrow(); | ||
}); | ||
it('throws error if failing', async () => { | ||
setPrivateKey('some-key'); | ||
exec.exec.mockResolvedValueOnce({ | ||
stderr: `something wrong`, | ||
stdout: '', | ||
}); | ||
await expect(writePrivateKey('/tmp/some-repo')).rejects.toThrow(); | ||
}); | ||
it('imports the private key', async () => { | ||
setPrivateKey('some-key'); | ||
exec.exec.mockResolvedValueOnce({ | ||
stderr: `gpg: key BADC0FFEE: secret key imported\nfoo\n`, | ||
stdout: '', | ||
}); | ||
await expect(writePrivateKey('/tmp/some-repo')).resolves.not.toThrow(); | ||
}); | ||
it('does not import the key again', async () => { | ||
await expect(writePrivateKey('/tmp/some-repo')).resolves.not.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os from 'os'; | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { PLATFORM_GPG_FAILED } from '../../constants/error-messages'; | ||
import { logger } from '../../logger'; | ||
import { exec } from '../../util/exec'; | ||
|
||
let gitPrivateKey: string; | ||
let keyId: string; | ||
|
||
export function setPrivateKey(key: string): void { | ||
gitPrivateKey = key; | ||
} | ||
|
||
async function importKey(): Promise<void> { | ||
if (keyId) { | ||
return; | ||
} | ||
const keyFileName = path.join(os.tmpdir() + '/git-private.key'); | ||
await fs.outputFile(keyFileName, gitPrivateKey); | ||
const { stdout, stderr } = await exec(`gpg --import ${keyFileName}`); | ||
logger.debug({ stdout, stderr }, 'Private key import result'); | ||
keyId = (stdout + stderr) | ||
.split('\n') | ||
.find((line) => line.includes('secret key imported')) | ||
.replace('gpg: key ', '') | ||
.split(':') | ||
.shift(); | ||
await fs.remove(keyFileName); | ||
} | ||
|
||
export async function writePrivateKey(cwd: string): Promise<void> { | ||
if (!gitPrivateKey) { | ||
return; | ||
} | ||
logger.debug('Setting git private key'); | ||
try { | ||
await importKey(); | ||
await exec(`git config user.signingkey ${keyId}`, { cwd }); | ||
await exec(`git config commit.gpgsign true`, { cwd }); | ||
} catch (err) { | ||
logger.warn({ err }, 'Error writing git private key'); | ||
throw new Error(PLATFORM_GPG_FAILED); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters