Skip to content

Commit db8398a

Browse files
committed
Adds custom avatar
1 parent a04ab4d commit db8398a

5 files changed

Lines changed: 34 additions & 1 deletion

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,6 +3056,10 @@
30563056
"fileRange": {
30573057
"type": "string",
30583058
"markdownDescription": "Specifies the format of a range in a file URL for the custom remote service\n\nAvailable tokens\\\n`${start}` — starting line\\\n`${end}` — ending line"
3059+
},
3060+
"avatar": {
3061+
"type": "string",
3062+
"markdownDescription": "Specifies the format of a avatar url for the custom remote service\n\nAvailable tokens\\\n`${email}` — contributor email\\\n`${name}` — email local-part\\\n`${domain}` — email domain\\\n`${size}` — avatar size"
30593063
}
30603064
},
30613065
"additionalProperties": false

src/avatars.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function getAvatarUriCore(
125125
const avatar = createOrUpdateAvatar(key, email, size, hash, options?.defaultStyle);
126126
if (avatar.uri != null) return avatar.uri;
127127

128-
if (!options?.cached && repoPathOrCommit != null && getContext('gitlens:hasConnectedRemotes')) {
128+
if (!options?.cached && repoPathOrCommit != null && getContext('gitlens:hasRemotes')) {
129129
let query = avatarQueue.get(key);
130130
if (query == null && hasAvatarExpired(avatar)) {
131131
query = getAvatarUriFromRemoteProvider(avatar, key, email, repoPathOrCommit, { size: size }).then(
@@ -213,6 +213,19 @@ async function getAvatarUriFromRemoteProvider(
213213
) {
214214
ensureAvatarCache(avatarCache);
215215

216+
if (!getContext('gitlens:hasConnectedRemotes')) {
217+
const remote = await Container.instance.git.getBestRemoteWithProvider(
218+
typeof repoPathOrCommit !== 'string' ? repoPathOrCommit.repoPath : repoPathOrCommit,
219+
);
220+
const avatarUrl = remote?.provider.getUrlForAvatar?.(email, size);
221+
if (avatarUrl) {
222+
avatar.uri = Uri.parse(avatarUrl);
223+
avatar.timestamp = Date.now();
224+
avatar.retries = 0;
225+
}
226+
return avatar.uri ?? avatar.fallback;
227+
}
228+
216229
try {
217230
let account;
218231
// if (typeof repoPathOrCommit === 'string') {

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ export interface RemotesUrlsConfig {
509509
readonly fileInCommit: string;
510510
readonly fileLine: string;
511511
readonly fileRange: string;
512+
readonly avatar?: string;
512513
}
513514

514515
// NOTE: Must be kept in sync with `gitlens.advanced.messages` setting in the package.json

src/git/remotes/custom.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ export class CustomRemote extends RemoteProvider {
2727
return Promise.resolve(undefined);
2828
}
2929

30+
override getUrlForAvatar(email: string, size: number) {
31+
if (this.urls.avatar != null) {
32+
const [name, domain] = email.split('@');
33+
return this.encodeUrl(
34+
interpolate(
35+
this.urls.avatar,
36+
this.getContext({ name: name, domain: domain, email: email, size: String(size) }),
37+
),
38+
);
39+
}
40+
return undefined;
41+
}
42+
3043
protected override getUrlForRepository(): string {
3144
return this.getUrl(this.urls.repository, this.getContext());
3245
}

src/git/remotes/remoteProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export abstract class RemoteProvider implements RemoteProviderReference {
139139

140140
protected abstract getUrlForCommit(sha: string): string;
141141

142+
getUrlForAvatar?(email: string, size: number): string | undefined;
143+
142144
protected getUrlForComparison?(base: string, compare: string, notation: '..' | '...'): string | undefined;
143145

144146
protected getUrlForCreatePullRequest?(

0 commit comments

Comments
 (0)