Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(git): gitAuthor writing (#11535)
- Loading branch information
Showing
6 changed files
with
100 additions
and
88 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
6 changes: 3 additions & 3 deletions
6
...platform/__snapshots__/index.spec.ts.snap → ...til/git/__snapshots__/author.spec.ts.snap
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,30 @@ | ||
import { parseGitAuthor } from './author'; | ||
|
||
describe('util/git/author', () => { | ||
describe('parseGitAuthor', () => { | ||
it('returns null if empty email given', () => { | ||
expect(parseGitAuthor(undefined)).toBeNull(); | ||
}); | ||
it('parses bot email', () => { | ||
// FIXME: explicit assert condition | ||
expect( | ||
parseGitAuthor('some[bot]@users.noreply.github.com') | ||
).toMatchSnapshot(); | ||
}); | ||
it('parses bot name and email', () => { | ||
// FIXME: explicit assert condition | ||
expect( | ||
parseGitAuthor('"some[bot]" <some[bot]@users.noreply.github.com>') | ||
).toMatchSnapshot(); | ||
}); | ||
it('escapes names', () => { | ||
// FIXME: explicit assert condition | ||
expect( | ||
parseGitAuthor('name [what] <name@what.com>').name | ||
).toMatchSnapshot(); | ||
}); | ||
it('gives up', () => { | ||
expect(parseGitAuthor('a.b.c')).toBeNull(); | ||
}); | ||
}); | ||
}); |
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,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; | ||
} |
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