Skip to content

Commit

Permalink
feat(utils): parse author env var for email (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpaciga committed Sep 22, 2020
1 parent bb28d9a commit 8951394
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/utils/src/build-context.js
Expand Up @@ -229,6 +229,17 @@ function getAuthor(hash = 'HEAD') {
return result.stdout.trim().slice(0, 256);
}

/**
* @param {string} author
* @return {string | null}
*/
function getEmailFromAuthor(author) {
const emailRegex = new RegExp(/ <(\S+@\S+)>$/);
const emailMatch = author.match(emailRegex);
if (!emailMatch) return null;
return emailMatch[1];
}

/**
* @param {string} hash
* @return {string}
Expand All @@ -240,6 +251,12 @@ function getAvatarUrl(hash = 'HEAD') {
]);
if (envHash) return envHash;

// Next try to parse the email from the complete author.
const author = getAuthor(hash);
const email = getEmailFromAuthor(author);
if (email) return getGravatarUrlFromEmail(email);

// Finally fallback to git again if we couldn't parse the email out of the author.
const result = childProcess.spawnSync('git', ['log', '--format=%aE', '-n', '1', hash], {
encoding: 'utf8',
});
Expand Down Expand Up @@ -352,6 +369,7 @@ module.exports = {
getExternalBuildUrl,
getCommitMessage,
getAuthor,
getEmailFromAuthor,
getAvatarUrl,
getGravatarUrlFromEmail,
getAncestorHash,
Expand Down
72 changes: 72 additions & 0 deletions packages/utils/test/build-context.test.js
Expand Up @@ -99,6 +99,64 @@ describe('build-context.js', () => {
});
});

describe('#getEmailFromAuthor', () => {
it('should work', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <patrick.hulce@gmail.com>')).toEqual(
'patrick.hulce@gmail.com'
);
});

it('should not be overly strict about email format', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <a@b>')).toEqual('a@b');
});

it('returns null if there is not an @ sign', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <patrick>')).toBeNull();
});

it('returns null if email does not have a user to the left of the @', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <@gmail.com>')).toBeNull();
});

it('returns null if the user is just whitespace', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce < @gmail.com>')).toBeNull();
});

it('returns null if email does not have a host to the right of the @', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <patrick@>')).toBeNull();
});

it('returns null if the host is just whitespace', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <patrick@ >')).toBeNull();
});

it('should ignore angle brackets other than the last pair', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <fir@st> <se@cond>')).toEqual(
'se@cond'
);
});

it('returns null if there is not a space between name and email', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce<patrick.hulce@gmail.com>')).toBeNull();
});

it('should not be sensitive to the author name itself', () => {
expect(buildContext.getEmailFromAuthor(' <patrick.hulce@gmail.com>')).toEqual(
'patrick.hulce@gmail.com'
);
});

it('returns null if there are not angle brackets surrounding the email', () => {
expect(buildContext.getEmailFromAuthor('Patrick Hulce <patrick.hulce@gmail.com')).toBeNull();
});

it('returns null if the email is not at the end of the string', () => {
expect(
buildContext.getEmailFromAuthor('Patrick Hulce <patrick.hulce@gmail.com> ')
).toBeNull();
});
});

describe('#getAvatarUrl()', () => {
it('should work', () => {
expect(buildContext.getAvatarUrl(hash)).toEqual(
Expand All @@ -110,6 +168,20 @@ describe('build-context.js', () => {
process.env.LHCI_BUILD_CONTEXT__AVATAR_URL = 'http://localhost:1234/profile.jpg';
expect(buildContext.getAvatarUrl(hash)).toEqual('http://localhost:1234/profile.jpg');
});

it('should parse email from the complete author env override', () => {
process.env.LHCI_BUILD_CONTEXT__AUTHOR = 'Paul Irish <paulirish@google.com>';
expect(buildContext.getAvatarUrl(hash)).toEqual(
'https://www.gravatar.com/avatar/629999fcb3f6a928abe5f65ed0ab09c2.jpg?d=identicon'
);
});

it('should use git if author env override does not have an email', () => {
process.env.LHCI_BUILD_CONTEXT__AUTHOR = 'Paul Irish <localhost>';
expect(buildContext.getAvatarUrl(hash)).toEqual(
'https://www.gravatar.com/avatar/78bafdcaf40e20b90bb76b9aa5834e11.jpg?d=identicon'
);
});
});

describe('#getGravatarUrlFromEmail()', () => {
Expand Down

0 comments on commit 8951394

Please sign in to comment.