Skip to content

Commit 6ba7ff4

Browse files
committed
fix(gpg): extract only primary key fingerprint
1 parent 1030c2d commit 6ba7ff4

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ jobs:
2828
- name: Release
2929
env:
3030
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
31+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3232
run: npx semantic-release

src/utils/createKey.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import confirm from "@inquirer/confirm";
33
import chalk from "chalk";
44
import { GitKeyKitCodes } from "../gitkeykitCodes";
55

6-
76
export async function createPgpKey(): Promise<GitKeyKitCodes> {
87
try {
98
const shouldCreate = await confirm({
@@ -23,6 +22,11 @@ export async function createPgpKey(): Promise<GitKeyKitCodes> {
2322
stdio: "inherit",
2423
});
2524

25+
gpg.on("error", (error) => {
26+
console.error(chalk.red(`Failed to start GPG process: ${error.message}`));
27+
resolve(GitKeyKitCodes.ERR_KEY_GENERATION);
28+
});
29+
2630
gpg.on("close", (code) => {
2731
if (code === 0) {
2832
console.log(chalk.green("GPG key has been generated successfully."));

src/utils/setGitConfig.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,43 @@ const execFileAsync = promisify(execFile);
88

99
async function getGpgKeyFingerprint(): Promise<string> {
1010
try {
11-
const { stdout } = await execFileAsync('gpg --list-secret-keys');
11+
/*
12+
output of `gpg --list-secret-keys --with-colons`
13+
┌────────────────────────────────────────────────────────────────────────────┐
14+
│ sec:u:4096:1:A1B2C3D4E5F6G7H8:1700848217:::u:::scESC:::#:::23::0: │
15+
│ fpr:::::::::1234567890ABCDEF1234567890ABCDEF12345678: │<-- we want this one
16+
│ grp:::::::::ABCDEF1234567890ABCDEF1234567890ABCDEF12: │
17+
│ uid:u::::1700850983::0123456789ABCDEF0123456789ABCDEF01234567::Username │
18+
│ <email>::::::::::0: │
19+
│ ssb:u:4096:1:FEDCBA9876543210:1732404248:1858548248:::::e:::+:::23: │
20+
│ fpr:::::::::FEDCBA9876543210FEDCBA9876543210FEDCBA98: │
21+
│ grp:::::::::9876543210FEDCBA9876543210FEDCBA98765432: │
22+
│ ssb:u:4096:1:1A2B3C4D5E6F7G8H:1732404191:1858548191:::::s:::+:::23: │
23+
│ fpr:::::::::ABCD1234EFGH5678IJKL9012MNOP3456QRST7890: │
24+
│ grp:::::::::WXYZ7890ABCD1234EFGH5678IJKL9012MNOP3456: │
25+
└────────────────────────────────────────────────────────────────────────────┘
26+
*/
27+
const { stdout } = await execFileAsync('gpg', ['--list-secret-keys', '--with-colons']);
1228

13-
// Find the longest string that could be a fingerprint
1429
const lines = stdout.split('\n');
15-
let maxLength = 0;
30+
let isPrimaryKey = false;
1631
let keyFingerprint = '';
17-
32+
1833
for (const line of lines) {
19-
const tokens = line.trim().split(/\s+/);
20-
for (const token of tokens) {
21-
if (token.length > maxLength) {
22-
keyFingerprint = token;
23-
maxLength = token.length;
24-
}
34+
const parts = line.split(':');
35+
// Mark when we find a primary key (sec)
36+
if (parts[0] === 'sec') {
37+
isPrimaryKey = true;
38+
continue;
39+
}
40+
// Get the fingerprint only if it's for the primary key
41+
if (isPrimaryKey && parts[0] === 'fpr') {
42+
keyFingerprint = parts[9];
43+
break;
44+
}
45+
46+
if (parts[0] === 'ssb') {
47+
isPrimaryKey = false;
2548
}
2649
}
2750

@@ -47,7 +70,6 @@ async function setGitConfigValue(key: string, value: string): Promise<void> {
4770

4871
export async function setGitConfig(gpgPath: string): Promise<GitKeyKitCodes> {
4972
try {
50-
// Get user input
5173
const username = await input({
5274
message: 'Enter your name:',
5375
validate: (value) => value.length > 0 || 'Name cannot be empty'
@@ -63,10 +85,8 @@ export async function setGitConfig(gpgPath: string): Promise<GitKeyKitCodes> {
6385

6486
console.log(chalk.blue('Setting git config...'));
6587

66-
// Get GPG key fingerprint
6788
const keyFingerprint = await getGpgKeyFingerprint();
6889

69-
// Configure git settings
7090
const configs = [
7191
['user.name', username],
7292
['user.email', email],

0 commit comments

Comments
 (0)