Skip to content

Commit 60cbcd7

Browse files
committed
Add custom avatar
1 parent de6e835 commit 60cbcd7

5 files changed

Lines changed: 38 additions & 7 deletions

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,6 +3576,10 @@
35763576
"fileRange": {
35773577
"type": "string",
35783578
"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"
3579+
},
3580+
"avatar": {
3581+
"type": "string",
3582+
"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"
35793583
}
35803584
},
35813585
"additionalProperties": false

src/avatars.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,7 @@ function getAvatarUriCore(
126126
const avatar = createOrUpdateAvatar(key, email, size, hash, options?.defaultStyle);
127127
if (avatar.uri != null) return avatar.uri;
128128

129-
if (
130-
!options?.cached &&
131-
repoPathOrCommit != null &&
132-
getContext('gitlens:repos:withHostingIntegrationsConnected')?.includes(
133-
typeof repoPathOrCommit === 'string' ? repoPathOrCommit : repoPathOrCommit.repoPath,
134-
)
135-
) {
129+
if (!options?.cached && repoPathOrCommit != null) {
136130
let query = avatarQueue.get(key);
137131
if (query == null && hasAvatarExpired(avatar)) {
138132
query = getAvatarUriFromRemoteProvider(avatar, key, email, repoPathOrCommit, { size: size }).then(
@@ -220,6 +214,23 @@ async function getAvatarUriFromRemoteProvider(
220214
) {
221215
ensureAvatarCache(avatarCache);
222216

217+
if (
218+
!getContext('gitlens:repos:withHostingIntegrationsConnected')?.includes(
219+
typeof repoPathOrCommit === 'string' ? repoPathOrCommit : repoPathOrCommit.repoPath,
220+
)
221+
) {
222+
const remote = await Container.instance.git.getBestRemoteWithProvider(
223+
typeof repoPathOrCommit !== 'string' ? repoPathOrCommit.repoPath : repoPathOrCommit,
224+
);
225+
const avatarUrl = remote?.provider.getUrlForAvatar?.(email, size);
226+
if (avatarUrl) {
227+
avatar.uri = Uri.parse(avatarUrl);
228+
avatar.timestamp = Date.now();
229+
avatar.retries = 0;
230+
}
231+
return avatar.uri ?? avatar.fallback;
232+
}
233+
223234
try {
224235
let account: CommitAuthor | undefined;
225236
// if (typeof repoPathOrCommit === 'string') {

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ export interface RemotesUrlsConfig {
572572
readonly fileInCommit: string;
573573
readonly fileLine: string;
574574
readonly fileRange: string;
575+
readonly avatar?: string;
575576
}
576577

577578
// 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
@@ -33,6 +33,19 @@ export class CustomRemote extends RemoteProvider {
3333
return Promise.resolve(undefined);
3434
}
3535

36+
override getUrlForAvatar(email: string, size: number) {
37+
if (this.urls.avatar != null) {
38+
const [name, domain] = email.split('@');
39+
return this.encodeUrl(
40+
interpolate(
41+
this.urls.avatar,
42+
this.getContext({ name: name, domain: domain, email: email, size: String(size) }),
43+
),
44+
);
45+
}
46+
return undefined;
47+
}
48+
3649
protected override getUrlForRepository(): string {
3750
return this.getUrl(this.urls.repository, this.getContext());
3851
}

src/git/remotes/remoteProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ export abstract class RemoteProvider<T extends ResourceDescriptor = ResourceDesc
167167

168168
protected abstract getUrlForCommit(sha: string): string;
169169

170+
getUrlForAvatar?(email: string, size: number): string | undefined;
171+
170172
protected getUrlForComparison?(base: string, compare: string, notation: '..' | '...'): string | undefined;
171173

172174
protected getUrlForCreatePullRequest?(

0 commit comments

Comments
 (0)