-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
author.ts
44 lines (42 loc) · 1.13 KB
/
author.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import addrs from 'email-addresses';
import { logger } from '../../logger';
export interface GitAuthor {
name?: string;
address?: string;
}
export function parseGitAuthor(input: string): GitAuthor | null {
let result: GitAuthor = null;
if (!input) {
return null;
}
try {
result = addrs.parseOneAddress(input);
if (result) {
return result;
}
if (input.includes('[bot]@')) {
// invalid github app/bot addresses
const parsed = addrs.parseOneAddress(
input.replace('[bot]@', '@')
) as addrs.ParsedMailbox;
if (parsed?.address) {
result = {
name: parsed.name || input.replace(/@.*/, ''),
address: parsed.address.replace('@', '[bot]@'),
};
return result;
}
}
if (input.includes('<') && input.includes('>')) {
// try wrapping the name part in quotations
result = addrs.parseOneAddress('"' + input.replace(/(\s?<)/, '"$1'));
if (result) {
return result;
}
}
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'Unknown error parsing gitAuthor');
}
// give up
return null;
}