Browse Source

Consolidates GitHub no reply parsing

main
Eric Amodio 2 years ago
parent
commit
82740a6ba4
3 changed files with 26 additions and 15 deletions
  1. +6
    -6
      src/avatars.ts
  2. +12
    -0
      src/git/remotes/github.ts
  3. +8
    -9
      src/plus/github/github.ts

+ 6
- 6
src/avatars.ts View File

@ -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<ContactPresenceStatus, string>();
const gitHubNoReplyAddressRegex = /^(?:(?<userId>\d+)\+)?(?<userName>[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(

+ 12
- 0
src/git/remotes/github.ts View File

@ -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 };
}

+ 8
- 9
src/plus/github/github.ts View File

@ -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}`;
}
}
}

Loading…
Cancel
Save