From 1150b8dc3ff20bd92ab5395c5037903b86aeadf3 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Tue, 7 Sep 2021 14:28:24 +0200 Subject: [PATCH] fix(git): better github bot massage during validation (#11622) --- .../git/__snapshots__/author.spec.ts.snap | 17 --------- lib/util/git/author.spec.ts | 27 ++++++++++---- lib/util/git/author.ts | 35 +++++++++++-------- 3 files changed, 41 insertions(+), 38 deletions(-) delete mode 100644 lib/util/git/__snapshots__/author.spec.ts.snap diff --git a/lib/util/git/__snapshots__/author.spec.ts.snap b/lib/util/git/__snapshots__/author.spec.ts.snap deleted file mode 100644 index 844b8e8eb738a1..00000000000000 --- a/lib/util/git/__snapshots__/author.spec.ts.snap +++ /dev/null @@ -1,17 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`util/git/author parseGitAuthor escapes names 1`] = `"name [what]"`; - -exports[`util/git/author parseGitAuthor parses bot email 1`] = ` -Object { - "address": "some[bot]@users.noreply.github.com", - "name": "some[bot]", -} -`; - -exports[`util/git/author parseGitAuthor parses bot name and email 1`] = ` -Object { - "address": "some[bot]@users.noreply.github.com", - "name": "some[bot]", -} -`; diff --git a/lib/util/git/author.spec.ts b/lib/util/git/author.spec.ts index cd80f6a393fcec..6e16f627c4bb25 100644 --- a/lib/util/git/author.spec.ts +++ b/lib/util/git/author.spec.ts @@ -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]" ') - ).toMatchSnapshot(); + parseGitAuthor('renovate[bot] ') + ).toMatchInlineSnapshot(` + Object { + "address": "renovate[bot]@users.noreply.github.com", + "name": "renovate[bot]", + } + `); }); it('escapes names', () => { // FIXME: explicit assert condition expect( parseGitAuthor('name [what] ').name - ).toMatchSnapshot(); + ).toMatchInlineSnapshot(`"name [what]"`); + }); + it('tries again and fails', () => { + expect(parseGitAuthor('foo')).toBeNull(); }); it('gives up', () => { expect(parseGitAuthor('a.b.c')).toBeNull(); diff --git a/lib/util/git/author.ts b/lib/util/git/author.ts index bb149346afc7fe..5fe82349f1ef15 100644 --- a/lib/util/git/author.ts +++ b/lib/util/git/author.ts @@ -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');