diff --git a/lib/platform/index.spec.ts b/lib/platform/index.spec.ts index 54fd78d40abe1b..1a29be4909fd66 100644 --- a/lib/platform/index.spec.ts +++ b/lib/platform/index.spec.ts @@ -70,31 +70,4 @@ describe('platform/index', () => { platform: PLATFORM_TYPE_BITBUCKET, }); }); - - it('returns null if empty email given', () => { - expect(platform.parseGitAuthor(undefined)).toBeNull(); - }); - it('parses bot email', () => { - // FIXME: explicit assert condition - expect( - platform.parseGitAuthor('some[bot]@users.noreply.github.com') - ).toMatchSnapshot(); - }); - it('parses bot name and email', () => { - // FIXME: explicit assert condition - expect( - platform.parseGitAuthor( - '"some[bot]" ' - ) - ).toMatchSnapshot(); - }); - it('escapes names', () => { - // FIXME: explicit assert condition - expect( - platform.parseGitAuthor('name [what] ').name - ).toMatchSnapshot(); - }); - it('gives up', () => { - expect(platform.parseGitAuthor('a.b.c')).toBeNull(); - }); }); diff --git a/lib/platform/index.ts b/lib/platform/index.ts index 8268aecdc7c1da..5436884fda69ed 100644 --- a/lib/platform/index.ts +++ b/lib/platform/index.ts @@ -1,10 +1,10 @@ import URL from 'url'; -import addrs from 'email-addresses'; import type { AllConfig } from '../config/types'; import { PLATFORM_NOT_FOUND } from '../constants/error-messages'; import { logger } from '../logger'; import type { HostRule } from '../types'; import { setNoVerify, setPrivateKey } from '../util/git'; +import { parseGitAuthor } from '../util/git/author'; import * as hostRules from '../util/host-rules'; import platforms from './api'; import type { Platform } from './types'; @@ -39,48 +39,6 @@ export function setPlatformApi(name: string): void { _platform = platforms.get(name); } -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.error({ err }, 'Unknown error parsing gitAuthor'); - } - // give up - return null; -} - export async function initPlatform(config: AllConfig): Promise { setPrivateKey(config.gitPrivateKey); setNoVerify(config.gitNoVerify ?? []); diff --git a/lib/platform/__snapshots__/index.spec.ts.snap b/lib/util/git/__snapshots__/author.spec.ts.snap similarity index 50% rename from lib/platform/__snapshots__/index.spec.ts.snap rename to lib/util/git/__snapshots__/author.spec.ts.snap index 3217d7d515f0db..844b8e8eb738a1 100644 --- a/lib/platform/__snapshots__/index.spec.ts.snap +++ b/lib/util/git/__snapshots__/author.spec.ts.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`platform/index escapes names 1`] = `"name [what]"`; +exports[`util/git/author parseGitAuthor escapes names 1`] = `"name [what]"`; -exports[`platform/index parses bot email 1`] = ` +exports[`util/git/author parseGitAuthor parses bot email 1`] = ` Object { "address": "some[bot]@users.noreply.github.com", "name": "some[bot]", } `; -exports[`platform/index parses bot name and email 1`] = ` +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 new file mode 100644 index 00000000000000..cd80f6a393fcec --- /dev/null +++ b/lib/util/git/author.spec.ts @@ -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]" ') + ).toMatchSnapshot(); + }); + it('escapes names', () => { + // FIXME: explicit assert condition + expect( + parseGitAuthor('name [what] ').name + ).toMatchSnapshot(); + }); + it('gives up', () => { + expect(parseGitAuthor('a.b.c')).toBeNull(); + }); + }); +}); diff --git a/lib/util/git/author.ts b/lib/util/git/author.ts new file mode 100644 index 00000000000000..bb149346afc7fe --- /dev/null +++ b/lib/util/git/author.ts @@ -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; +} diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index e24c8084122bcc..5194853076ac0a 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -234,6 +234,27 @@ async function setBranchPrefix(branchPrefix: string): Promise { } } +export async function writeGitAuthor(): Promise { + const { gitAuthorName, gitAuthorEmail } = config; + try { + if (gitAuthorName) { + logger.debug({ gitAuthorName }, 'Setting git author name'); + await git.addConfig('user.name', gitAuthorName); + } + if (gitAuthorEmail) { + logger.debug({ gitAuthorEmail }, 'Setting git author email'); + await git.addConfig('user.email', gitAuthorEmail); + } + } catch (err) /* istanbul ignore next */ { + checkForPlatformFailure(err); + logger.debug( + { err, gitAuthorName, gitAuthorEmail }, + 'Error setting git author config' + ); + throw new Error(TEMPORARY_ERROR); + } +} + export async function setUserRepoConfig({ branchPrefix, gitIgnoredAuthors, @@ -344,21 +365,7 @@ export async function syncGit(): Promise { } logger.warn({ err }, 'Cannot retrieve latest commit'); } - try { - const { gitAuthorName, gitAuthorEmail } = config; - if (gitAuthorName) { - logger.debug({ gitAuthorName }, 'Setting git author name'); - await git.addConfig('user.name', gitAuthorName); - } - if (gitAuthorEmail) { - logger.debug({ gitAuthorEmail }, 'Setting git author email'); - await git.addConfig('user.email', gitAuthorEmail); - } - } catch (err) /* istanbul ignore next */ { - checkForPlatformFailure(err); - logger.debug({ err }, 'Error setting git author config'); - throw new Error(TEMPORARY_ERROR); - } + await writeGitAuthor(); config.currentBranch = config.currentBranch || (await getDefaultBranch(git)); if (config.branchPrefix) { await setBranchPrefix(config.branchPrefix);