Skip to content

Commit db66751

Browse files
wankun-tcjalexr00
andauthored
Fix avatar display issue in Pull Request tree view (#7851)
* Fix avatar display issue in Pull Request tree view - Fix getAvatarWithEnterpriseFallback to prioritize GitHub avatars over Gravatar fallback - Implement cache invalidation by including avatar URL hash in cache keys - Ensure logged-in users see their actual GitHub profile avatars instead of random generated ones Fixes #6914 * Revert package.json changes * Clean up --------- Co-authored-by: Alex Ross <[email protected]>
1 parent f746fcf commit db66751

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/common/uri.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,17 @@ export namespace DataUri {
205205
const iconsFolder = 'userIcons';
206206

207207
function iconFilename(user: IAccount | ITeam): string {
208-
return `${reviewerId(user)}.jpg`;
208+
// Include avatarUrl hash to invalidate cache when URL changes
209+
const baseId = reviewerId(user);
210+
if (user.avatarUrl) {
211+
// Create a simple hash of the URL to detect changes
212+
const urlHash = user.avatarUrl.split('').reduce((a, b) => {
213+
a = ((a << 5) - a) + b.charCodeAt(0);
214+
return a & a;
215+
}, 0);
216+
return `${baseId}_${Math.abs(urlHash)}.jpg`;
217+
}
218+
return `${baseId}.jpg`;
209219
}
210220

211221
function cacheLocation(context: vscode.ExtensionContext): vscode.Uri {
@@ -291,7 +301,6 @@ export namespace DataUri {
291301
const startingCacheSize = cacheLogOrder.length;
292302

293303
const results = await Promise.all(users.map(async (user) => {
294-
295304
const imageSourceUrl = user.avatarUrl;
296305
if (imageSourceUrl === undefined) {
297306
return undefined;
@@ -311,6 +320,9 @@ export namespace DataUri {
311320
cacheMiss = true;
312321
const doFetch = async () => {
313322
const response = await fetch(imageSourceUrl.toString());
323+
if (!response.ok) {
324+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
325+
}
314326
const buffer = await response.arrayBuffer();
315327
await writeAvatarToCache(context, user, new Uint8Array(buffer));
316328
innerImageContents = Buffer.from(buffer);

src/github/utils.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,13 @@ export function parseAccount(
653653
}
654654

655655
// In some places, Copilot comes in as a user, and in others as a bot
656+
657+
const finalAvatarUrl = githubRepository ? getAvatarWithEnterpriseFallback(avatarUrl, undefined, githubRepository.remote.isEnterprise) : avatarUrl;
658+
656659
return {
657660
login: author.login,
658661
url: COPILOT_ACCOUNTS[author.login]?.url ?? url,
659-
avatarUrl: githubRepository ? getAvatarWithEnterpriseFallback(avatarUrl, undefined, githubRepository.remote.isEnterprise) : avatarUrl,
662+
avatarUrl: finalAvatarUrl,
660663
email: author.email ?? undefined,
661664
id,
662665
name: author.name ?? COPILOT_ACCOUNTS[author.login]?.name ?? undefined,
@@ -1637,8 +1640,21 @@ export function generateGravatarUrl(gravatarId: string | undefined, size: number
16371640
}
16381641

16391642
export function getAvatarWithEnterpriseFallback(avatarUrl: string, email: string | undefined, isEnterpriseRemote: boolean): string | undefined {
1640-
return !isEnterpriseRemote ? avatarUrl : (email ? generateGravatarUrl(
1641-
crypto.createHash('sha256').update(email?.trim()?.toLowerCase()).digest('hex')) : undefined);
1643+
1644+
// For non-enterprise, always use the provided avatarUrl
1645+
if (!isEnterpriseRemote) {
1646+
return avatarUrl;
1647+
}
1648+
1649+
// For enterprise, prefer GitHub avatarUrl if available, fallback to Gravatar only if needed
1650+
if (avatarUrl && avatarUrl.trim()) {
1651+
return avatarUrl;
1652+
}
1653+
1654+
// Only fallback to Gravatar if no avatarUrl is available and email is provided
1655+
const gravatarUrl = email ? generateGravatarUrl(
1656+
crypto.createHash('sha256').update(email.trim().toLowerCase()).digest('hex')) : undefined;
1657+
return gravatarUrl;
16421658
}
16431659

16441660
export function getPullsUrl(repo: GitHubRepository) {

0 commit comments

Comments
 (0)