Skip to content

Commit

Permalink
fix(git): better github bot massage during validation (#11622)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Sep 7, 2021
1 parent 08ecf25 commit 1150b8d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
17 changes: 0 additions & 17 deletions lib/util/git/__snapshots__/author.spec.ts.snap

This file was deleted.

27 changes: 21 additions & 6 deletions lib/util/git/author.spec.ts
Expand Up @@ -5,23 +5,38 @@ describe('util/git/author', () => {
it('returns null if empty email given', () => {
expect(parseGitAuthor(undefined)).toBeNull();
});
it('handles a normal address', () => {
expect(parseGitAuthor('renovate@whitesourcesoftware.com')).not.toBeNull();
});
it('parses bot email', () => {
// FIXME: explicit assert condition
expect(
parseGitAuthor('some[bot]@users.noreply.github.com')
).toMatchSnapshot();
expect(parseGitAuthor('renovate[bot]@users.noreply.github.com'))
.toMatchInlineSnapshot(`
Object {
"address": "renovate[bot]@users.noreply.github.com",
"name": "renovate[bot]",
}
`);
});
it('parses bot name and email', () => {
// FIXME: explicit assert condition
expect(
parseGitAuthor('"some[bot]" <some[bot]@users.noreply.github.com>')
).toMatchSnapshot();
parseGitAuthor('renovate[bot] <renovate[bot]@users.noreply.github.com>')
).toMatchInlineSnapshot(`
Object {
"address": "renovate[bot]@users.noreply.github.com",
"name": "renovate[bot]",
}
`);
});
it('escapes names', () => {
// FIXME: explicit assert condition
expect(
parseGitAuthor('name [what] <name@what.com>').name
).toMatchSnapshot();
).toMatchInlineSnapshot(`"name [what]"`);
});
it('tries again and fails', () => {
expect(parseGitAuthor('foo<foo>')).toBeNull();
});
it('gives up', () => {
expect(parseGitAuthor('a.b.c')).toBeNull();
Expand Down
35 changes: 20 additions & 15 deletions lib/util/git/author.ts
Expand Up @@ -16,25 +16,30 @@ export function parseGitAuthor(input: string): GitAuthor | null {
if (result) {
return result;
}
let massagedInput;
let massagedBotEmail = false;
if (input.includes('<') && input.includes('>')) {
// try wrapping the name part in quotations
massagedInput = '"' + input.replace(/(\s?<)/, '"$1');
}
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;
}
massagedInput = (massagedInput || input).replace('[bot]@', '@');
massagedBotEmail = true;
}
if (input.includes('<') && input.includes('>')) {
// try wrapping the name part in quotations
result = addrs.parseOneAddress('"' + input.replace(/(\s?<)/, '"$1'));
if (result) {
return result;
if (!massagedInput) {
return null;
}
const parsed = addrs.parseOneAddress(massagedInput) as addrs.ParsedMailbox;
if (parsed?.address) {
result = {
name: parsed.name || input.replace(/@.*/, ''),
address: parsed.address,
};
if (massagedBotEmail) {
result.address = result.address.replace('@', '[bot]@');
}
return result;
}
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'Unknown error parsing gitAuthor');
Expand Down

0 comments on commit 1150b8d

Please sign in to comment.