From 82740a6ba4025e00a358a284e5cfd825fe9d4b7e Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 27 Jul 2022 01:59:01 -0400 Subject: [PATCH] Consolidates GitHub no reply parsing --- src/avatars.ts | 12 ++++++------ src/git/remotes/github.ts | 12 ++++++++++++ src/plus/github/github.ts | 17 ++++++++--------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/avatars.ts b/src/avatars.ts index 2fc12ab..c6ae6a4 100644 --- a/src/avatars.ts +++ b/src/avatars.ts @@ -2,6 +2,7 @@ import { EventEmitter, Uri } from 'vscode'; import { GravatarDefaultStyle } from './config'; import { Container } from './container'; import { GitRevisionReference } from './git/models'; +import { getGitHubNoReplyAddressParts } from './git/remotes/github'; import { StorageKeys } from './storage'; import { debounce } from './system/function'; import { filterMap } from './system/iterable'; @@ -56,8 +57,6 @@ const missingGravatarHash = '00000000000000000000000000000000'; const presenceCache = new Map(); -const gitHubNoReplyAddressRegex = /^(?:(?\d+)\+)?(?[a-zA-Z\d-]{1,39})@users\.noreply\.github\.com$/; - const millisecondsPerMinute = 60 * 1000; const millisecondsPerHour = 60 * 60 * 1000; const millisecondsPerDay = 24 * 60 * 60 * 1000; @@ -166,11 +165,12 @@ function getAvatarUriFromGravatar( } function getAvatarUriFromGitHubNoReplyAddress(email: string, size: number = 16): Uri | undefined { - const match = gitHubNoReplyAddressRegex.exec(email); - if (match == null) return undefined; + const parts = getGitHubNoReplyAddressParts(email); + if (parts == null || !equalsIgnoreCase(parts.authority, 'github.com')) return undefined; - const [, userId, userName] = match; - return Uri.parse(`https://avatars.githubusercontent.com/${userId ? `u/${userId}` : userName}?size=${size}`); + return Uri.parse( + `https://avatars.githubusercontent.com/${parts.userId ? `u/${parts.userId}` : parts.login}?size=${size}`, + ); } async function getAvatarUriFromRemoteProvider( diff --git a/src/git/remotes/github.ts b/src/git/remotes/github.ts index e520343..cbf7b35 100644 --- a/src/git/remotes/github.ts +++ b/src/git/remotes/github.ts @@ -372,3 +372,15 @@ export class GitHubRemote extends RichRemoteProvider { }); } } + +const gitHubNoReplyAddressRegex = /^(?:(\d+)\+)?([a-zA-Z\d-]{1,39})@users\.noreply\.(.*)$/i; + +export function getGitHubNoReplyAddressParts( + email: string, +): { userId: string; login: string; authority: string } | undefined { + const match = gitHubNoReplyAddressRegex.exec(email); + if (match == null) return undefined; + + const [, userId, login, authority] = match; + return { userId: userId, login: login, authority: authority }; +} diff --git a/src/plus/github/github.ts b/src/plus/github/github.ts index 4ce15b9..0c6831b 100644 --- a/src/plus/github/github.ts +++ b/src/plus/github/github.ts @@ -18,6 +18,7 @@ import { import { PagedResult, RepositoryVisibility } from '../../git/gitProvider'; import { type DefaultBranch, GitRevision, type GitUser, type IssueOrPullRequest, PullRequest } from '../../git/models'; import type { Account } from '../../git/models/author'; +import { getGitHubNoReplyAddressParts } from '../../git/remotes/github'; import type { RichRemoteProvider } from '../../git/remotes/provider'; import { LogCorrelationContext, Logger, LogLevel } from '../../logger'; import { debug } from '../../system/decorators/log'; @@ -1982,15 +1983,13 @@ export class GitHubApi implements Disposable { if (satisfies(version, '>= 3.0.0')) { let url: string | undefined; - const match = /^(?:(\d+)\+)?(.+?)@users\.noreply\.(.*)$/i.exec(email); - if (match != null) { - const [, userId, login, authority] = match; - - if (Uri.parse(baseUrl).authority === authority) { - if (userId != null) { - url = `${baseUrl}/enterprise/avatars/u/${encodeURIComponent(userId)}?s=${avatarSize}`; - } else if (login != null) { - url = `${baseUrl}/enterprise/avatars/${encodeURIComponent(login)}?s=${avatarSize}`; + const parts = getGitHubNoReplyAddressParts(email); + if (parts != null) { + if (Uri.parse(baseUrl).authority === parts.authority) { + if (parts.userId != null) { + url = `${baseUrl}/enterprise/avatars/u/${encodeURIComponent(parts.userId)}?s=${avatarSize}`; + } else if (parts.login != null) { + url = `${baseUrl}/enterprise/avatars/${encodeURIComponent(parts.login)}?s=${avatarSize}`; } } }