Skip to content

Commit bbd4cd7

Browse files
fix(canvas): show authenticated local comment avatars (#50)
<!-- Use a conventional commit-style title, such as "fix(hub): retain prototype history". --> ## Why <!-- Explain the problem and why this change matters in a short, plain-English paragraph. Link the issue when applicable. --> Local comments saved only the connected reviewer's login, so they showed no profile photo. The canvas also neither served nor allowed the shared avatar placeholder, leaving its fallback broken. ## What changes <!-- Describe what becomes different for a first-time reader. Explain necessary terms. Include accurate Mermaid diagrams when they clarify the proposal, using before/after diagrams when helpful. Add screenshots for meaningful UI changes. --> <!-- Omit validation sections, checklists, and routine test/CI summaries such as "CI is green" or "tests passed". Mention only known verification gaps that affect the change, not disclaimers about drafting this description. --> The canvas reads the authenticated GitHub CLI profile once per Hub connection and uses its name and photo only when its login matches the Hub-connected reviewer. New local comments retain that metadata, and existing local comments gain it when displayed without rewriting their saved records or changing author ownership. Unavailable, invalid or mismatched accounts produce a visible warning without blocking comments or replacing saved author details. Hub comments and local comment synchronization stay unchanged. The plugin bundles the shared placeholder, permits same-origin images without widening the external image allowlist, and falls back when a photo fails to load. Generated renderer assets and focused regression coverage are included. A session-only development canvas, using the real benchmark checkout and connected reviewer without identity overrides, displayed an existing local comment with the reviewer's name and a loaded GitHub profile photo. The same comment previously showed only the login and a broken placeholder. No comments were created, edited or published, and neither the global plugin nor benchmark source was changed. Browser fixtures covered loaded, missing and failed photos; new-comment submission and fallback behavior with the live authenticated account remain unverified. After this fix is available from the selected plugin source, update Replica, start a fresh Copilot session and reopen the preview. This PR does not change the installed plugin or publish a prototype. --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1853eb07-a2d2-4737-9d85-c31911e51d39
1 parent 1f75e63 commit bbd4cd7

14 files changed

Lines changed: 801 additions & 14 deletions

File tree

Lines changed: 4 additions & 0 deletions
Loading

‎.github/extensions/replica-prototype/assets/renderer.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.github/extensions/replica-prototype/create-local-review-comments/index.mjs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,25 @@ export function createLocalReviewComments(context, options = {}) {
5454
throw new Error("Check Hub before changing this comment: its upload could not be confirmed.")
5555
return draft
5656
}
57+
function withAuthor(scope, comment) {
58+
if (
59+
scope.author?.login?.toLowerCase() !== scope.viewerLogin.toLowerCase() ||
60+
comment.author.login.toLowerCase() !== scope.viewerLogin.toLowerCase()
61+
)
62+
return comment
63+
return {
64+
...comment,
65+
author: {
66+
...comment.author,
67+
name: scope.author.name ?? comment.author.name,
68+
avatarUrl: scope.author.avatarUrl ?? comment.author.avatarUrl,
69+
},
70+
}
71+
}
5772
return {
5873
async list(scope) {
5974
return (await entries(scope)).map((entry) => ({
60-
...entry.comment,
75+
...withAuthor(scope, entry.comment),
6176
viewerCanEdit: !entry.sendingAt,
6277
}))
6378
},
@@ -75,7 +90,7 @@ export function createLocalReviewComments(context, options = {}) {
7590
})
7691
return mutate(scope, (drafts) => {
7792
const now = new Date().toISOString()
78-
const comment = {
93+
const comment = withAuthor(scope, {
7994
id: 2 ** 48 + randomBytes(6).readUIntBE(0, 6),
8095
kind: "root",
8196
body,
@@ -95,7 +110,7 @@ export function createLocalReviewComments(context, options = {}) {
95110
resolvedBy: null,
96111
activityVersion: 1,
97112
viewerLastSeenActivityVersion: 1,
98-
}
113+
})
99114
drafts.push({
100115
treeSha: revision.treeSha,
101116
prototypeId: revision.prototypeId ?? null,
@@ -109,12 +124,12 @@ export function createLocalReviewComments(context, options = {}) {
109124
return mutate(scope, (drafts) => {
110125
const draft = editable(drafts, id)
111126
const now = new Date().toISOString()
112-
draft.comment = {
127+
draft.comment = withAuthor(scope, {
113128
...draft.comment,
114129
body: text,
115130
editedAt: now,
116131
updatedAt: now,
117-
}
132+
})
118133
return draft.comment
119134
})
120135
},

‎.github/extensions/replica-prototype/create-local-review-comments/index.test.mjs‎

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert/strict"
22
import { execFileSync } from "node:child_process"
3-
import { writeFile } from "node:fs/promises"
3+
import { readFile, readdir, writeFile } from "node:fs/promises"
44
import path from "node:path"
55
import { describe, it } from "node:test"
66

@@ -49,6 +49,111 @@ function fixture(context) {
4949
}
5050

5151
describe("createLocalReviewComments()", () => {
52+
it("saves verified profile metadata without replacing the Hub viewer login", async () => {
53+
await withContext(async (context) => {
54+
const { comments, captured, scope } = fixture(context)
55+
const author = {
56+
login: "OctoCat",
57+
name: "Mona",
58+
avatarUrl: "https://avatars.githubusercontent.com/u/1",
59+
}
60+
const created = await comments.create({ ...scope, author }, captured, {
61+
body: "Keep this selection",
62+
anchor: { schemaVersion: 1, kind: "element", selector: "#save" },
63+
})
64+
const expected = { ...author, login: scope.viewerLogin }
65+
assert.deepEqual(created.author, expected)
66+
assert.deepEqual((await localComments(context).list(scope))[0].author, expected)
67+
const updated = await comments.update(
68+
{
69+
...scope,
70+
author: { login: scope.viewerLogin, name: null, avatarUrl: null },
71+
},
72+
created.id,
73+
"Keep the saved profile",
74+
)
75+
assert.deepEqual(updated.author, expected)
76+
assert.deepEqual((await localComments(context).list(scope))[0].author, expected)
77+
})
78+
})
79+
80+
it("projects a matching profile onto persisted comments without rewriting storage", async () => {
81+
await withContext(async (context) => {
82+
const { comments, captured, scope } = fixture(context)
83+
const created = await comments.create(scope, captured, {
84+
body: "Written before profile discovery",
85+
anchor: { schemaVersion: 1, kind: "element", selector: "#save" },
86+
})
87+
const directory = path.join(context.repositoryPath, ".git", "local-review-comments")
88+
const file = path.join(
89+
directory,
90+
(await readdir(directory)).find((name) => name.endsWith(".json")),
91+
)
92+
const saved = await readFile(file, "utf8")
93+
const author = {
94+
login: "OCTOCAT",
95+
name: "Mona",
96+
avatarUrl: "https://avatars.githubusercontent.com/u/1",
97+
}
98+
const enrichedScope = { ...scope, author }
99+
const expected = { ...author, login: created.author.login }
100+
assert.deepEqual((await localComments(context).list(enrichedScope))[0].author, expected)
101+
assert.equal(await readFile(file, "utf8"), saved)
102+
const updated = await localComments(context).update(
103+
enrichedScope,
104+
created.id,
105+
"Updated feedback",
106+
)
107+
assert.deepEqual(updated.author, expected)
108+
assert.deepEqual((await localComments(context).list(scope))[0].author, expected)
109+
})
110+
})
111+
112+
it("never applies another account's metadata or reassigns historical authors", async () => {
113+
await withContext(async (context) => {
114+
const { comments, captured, scope } = fixture(context)
115+
const anotherAuthor = {
116+
login: "mona",
117+
name: "Another reviewer",
118+
avatarUrl: "https://avatars.githubusercontent.com/u/2",
119+
}
120+
const created = await comments.create({ ...scope, author: anotherAuthor }, captured, {
121+
body: "Keep the connected identity",
122+
anchor: { schemaVersion: 1, kind: "element", selector: "#save" },
123+
})
124+
assert.deepEqual(created.author, {
125+
login: scope.viewerLogin,
126+
name: null,
127+
avatarUrl: null,
128+
})
129+
const directory = path.join(context.repositoryPath, ".git", "local-review-comments")
130+
const file = path.join(
131+
directory,
132+
(await readdir(directory)).find((name) => name.endsWith(".json")),
133+
)
134+
const entries = JSON.parse(await readFile(file, "utf8"))
135+
entries[0].comment.author = anotherAuthor
136+
await writeFile(file, JSON.stringify(entries))
137+
138+
const verifiedScope = {
139+
...scope,
140+
author: {
141+
login: scope.viewerLogin,
142+
name: "Current reviewer",
143+
avatarUrl: "https://avatars.githubusercontent.com/u/1",
144+
},
145+
}
146+
assert.deepEqual((await comments.list(verifiedScope))[0].author, anotherAuthor)
147+
const updated = await comments.update(
148+
verifiedScope,
149+
created.id,
150+
"Preserve historical ownership",
151+
)
152+
assert.deepEqual(updated.author, anotherAuthor)
153+
assert.deepEqual((await localComments(context).list(scope))[0].author, anotherAuthor)
154+
})
155+
})
156+
52157
it("persists local edits and deletion across instances", async () => {
53158
await withContext(async (context) => {
54159
const { scope, captured } = fixture(context)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { runCommand } from "../run-command/index.mjs"
2+
3+
export function createReviewerProfile(context) {
4+
const profiles = new Map()
5+
6+
async function load(scope) {
7+
const author = { login: scope.viewerLogin, name: null, avatarUrl: null }
8+
function unavailable(message) {
9+
return { author, error: `Profile photo unavailable: ${message}` }
10+
}
11+
const result = await runCommand(
12+
"gh",
13+
["api", "--hostname", "github.com", "user"],
14+
context.repositoryPath,
15+
)
16+
if (!result.ok) return unavailable("GitHub CLI could not read the authenticated account.")
17+
18+
let profile
19+
try {
20+
profile = JSON.parse(result.stdout)
21+
} catch {
22+
return unavailable("GitHub CLI returned invalid account details.")
23+
}
24+
if (
25+
typeof profile?.login !== "string" ||
26+
!profile.login ||
27+
(profile.name != null && typeof profile.name !== "string") ||
28+
(profile.avatar_url != null && typeof profile.avatar_url !== "string")
29+
)
30+
return unavailable("GitHub CLI returned invalid account details.")
31+
if (profile.login.toLowerCase() !== scope.viewerLogin.toLowerCase())
32+
return unavailable("GitHub CLI is signed in as a different reviewer.")
33+
34+
const avatar = profile.avatar_url ? URL.parse(profile.avatar_url) : null
35+
if (
36+
profile.avatar_url &&
37+
(!avatar ||
38+
avatar.protocol !== "https:" ||
39+
avatar.hostname !== "avatars.githubusercontent.com" ||
40+
avatar.port ||
41+
avatar.username ||
42+
avatar.password)
43+
)
44+
return unavailable("GitHub returned an unsupported profile photo URL.")
45+
46+
return {
47+
author: {
48+
...author,
49+
name: profile.name?.trim() || null,
50+
avatarUrl: avatar?.href || null,
51+
},
52+
error: null,
53+
}
54+
}
55+
56+
return {
57+
async read(scope) {
58+
const key = JSON.stringify([
59+
scope.hubOrigin,
60+
scope.repository,
61+
scope.connectionId,
62+
scope.viewerLogin,
63+
])
64+
if (!profiles.has(key)) profiles.set(key, load(scope))
65+
return profiles.get(key)
66+
},
67+
}
68+
}

0 commit comments

Comments
 (0)