Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gitAuthor parsing #6594

Merged
merged 3 commits into from Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/platform/__snapshots__/index.spec.ts.snap
Expand Up @@ -210,3 +210,17 @@ Object {
"platform": "bitbucket",
}
`;

exports[`platform parses bot email 1`] = `
Object {
"address": "some[bot]@users.noreply.github.com",
"name": "some[bot]",
}
`;

exports[`platform parses bot name and email 1`] = `
Object {
"address": "some[bot]@users.noreply.github.com",
"name": "some[bot]",
}
`;
24 changes: 24 additions & 0 deletions lib/platform/index.spec.ts
Expand Up @@ -116,4 +116,28 @@ describe('platform', () => {
const bitbucketMethods = Object.keys(bitbucketServer).sort();
expect(bitbucketMethods).toMatchObject(githubMethods);
});

it('returns null if empty email given', () => {
expect(platform.parseGitAuthor(undefined)).toBeNull();
});
it('parses bot email', () => {
expect(
platform.parseGitAuthor('some[bot]@users.noreply.github.com')
).toMatchSnapshot();
});
it('parses bot name and email', () => {
expect(
platform.parseGitAuthor(
'"some[bot]" <some[bot]@users.noreply.github.com>'
)
).toMatchSnapshot();
});
it('escapes names', () => {
expect(
platform.parseGitAuthor('name [what] <name@what.com>')
).not.toBeNull();
rarkins marked this conversation as resolved.
Show resolved Hide resolved
});
it('gives up', () => {
expect(platform.parseGitAuthor('a.b.c')).toBeNull();
});
});
49 changes: 43 additions & 6 deletions lib/platform/index.ts
Expand Up @@ -37,6 +37,48 @@ 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: RenovateConfig
): Promise<RenovateConfig> {
Expand All @@ -56,12 +98,7 @@ export async function initPlatform(
logger.debug('Using platform gitAuthor: ' + platformInfo.gitAuthor);
gitAuthor = platformInfo.gitAuthor;
}
let gitAuthorParsed: addrs.ParsedMailbox | null = null;
try {
gitAuthorParsed = addrs.parseOneAddress(gitAuthor) as addrs.ParsedMailbox;
} catch (err) /* istanbul ignore next */ {
logger.debug({ gitAuthor, err }, 'Error parsing gitAuthor');
}
const gitAuthorParsed = parseGitAuthor(gitAuthor);
// istanbul ignore if
if (!gitAuthorParsed) {
throw new Error('Init: gitAuthor is not parsed as valid RFC5322 format');
Expand Down