Skip to content

Commit 293c4fb

Browse files
feat(annotations): Make each card's label from its object, not the saved label
A card no longer shows an Annotation's stored label. It shows the label of the live card, or, when Junior cannot read the object, the label that its link type makes from the link. Link types get an optional `label(url)` method. GitHub issue and pull request links make `owner/repo#n`. A link type without one shows the host of the link. Co-Authored-By: BYK <[email protected]>
1 parent c8aef83 commit 293c4fb

10 files changed

Lines changed: 70 additions & 27 deletions

File tree

‎packages/junior-github/src/issue-links.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ async function canViewLink(ctx: ObjectLinkContext, url: URL): Promise<boolean> {
321321
return Boolean(source && (await viewerCanSeeRepository(ctx, source)));
322322
}
323323

324+
// The label of a live card, such as `owner/repo#12`, from the link alone.
325+
function linkLabel(url: URL): string | undefined {
326+
const link = parseLink(url);
327+
return link ? `${link.owner}/${link.name}#${link.number}` : undefined;
328+
}
329+
324330
async function resolveLink(
325331
ctx: ObjectLinkContext,
326332
url: URL,
@@ -338,6 +344,7 @@ export const githubIssueLinks: PluginObjectLinkDefinition = {
338344
cacheCards: false,
339345
canView: canViewLink,
340346
filters: [{ host: "github.com", pathPattern: "/*/*/issues/*" }],
347+
label: linkLabel,
341348
objectTypes: ["task", "code_change"],
342349
readTool: (object) =>
343350
object.objectType === "code_change"
@@ -383,6 +390,7 @@ export const githubPullRequestLinks: PluginObjectLinkDefinition = {
383390
cacheCards: false,
384391
canView: canViewLink,
385392
filters: [{ host: "github.com", pathPattern: "/*/*/pull/*" }],
393+
label: linkLabel,
386394
objectTypes: ["code_change"],
387395
// A page of pull request cards reads GitHub in one query.
388396
prefetch: prefetchLinks,

‎packages/junior-plugin-api/src/object-links.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ export interface PluginObjectLinkDefinition {
201201
*/
202202
canView(ctx: ObjectLinkContext, url: URL): Promise<boolean>;
203203
filters: ObjectLinkFilter[];
204+
/**
205+
* Name the object from its link alone, such as `owner/repo#12`. A card
206+
* shows it when Junior cannot read the object, such as for a viewer who
207+
* cannot see it or when the provider is down. Without it, the card shows
208+
* the host of the link.
209+
*/
210+
label?(url: URL): string | undefined;
204211
/**
205212
* The object types that `resolve` can return. One URL can open more than
206213
* one type, such as a GitHub issue URL that opens a pull request.

‎packages/junior/src/api/objects/link-types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export interface ObjectLinkType {
2525
prefetched?: ObjectLinkPrefetchOutcome,
2626
): Promise<boolean>;
2727
filters: readonly ObjectLinkFilter[];
28+
/** Name the object from its link, for a card that cannot read it. */
29+
label?(url: URL): string | undefined;
2830
/** The object types that `resolve` can return. */
2931
objectTypes: readonly ObjectType[];
3032
/** `junior` for core, or the plugin name. */

‎packages/junior/src/api/objects/live-annotations.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
/**
22
* Annotations as people see them: each object reads its title and state
3-
* live, with the same resolver as the Inbox. An Annotation whose plugin has
4-
* no link type for its object keeps what it saved.
3+
* live, with the same resolver as the Inbox. A card never shows the stored
4+
* label: the plugin makes the label from the object, or from its link when
5+
* Junior cannot read it. An Annotation whose plugin has no link type for
6+
* its object keeps what it saved.
57
*/
68
import type { ConversationAnnotation, User } from "@sentry/junior-plugin-api";
79
import { annotationSubject } from "@/chat/plugins/annotations";
810
import { resolveObjectCards, type ObjectCardView } from "./subject-cards";
911

1012
/**
1113
* What an Annotation shows when its object cannot be read live:
12-
* - `label`: only the stored label and link, for a viewer who cannot read
13-
* the object or when its provider is down.
14-
* - `saved`: the saved facts, for a card that Junior posts once.
14+
* - `label`: only the label of its link and the link, for a viewer who
15+
* cannot read the object or when its provider is down.
16+
* - `saved`: the saved facts with the label of its link, for a card that
17+
* Junior posts once.
1518
*/
1619
export type AnnotationFallback = "label" | "saved";
1720

@@ -42,11 +45,12 @@ function liveAnnotation(
4245
updatedAt,
4346
};
4447
}
45-
if (fallback === "saved") return annotation;
48+
const label = view.label ?? view.url;
49+
if (fallback === "saved") return { ...annotation, label };
4650
return {
4751
kind: "resource_link",
4852
key,
49-
label: annotation.label,
53+
label,
5054
...(annotation.objectType
5155
? { objectType: annotation.objectType }
5256
: undefined),

‎packages/junior/src/api/objects/resolve.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function pluginLinkTypes(plugin: PluginRegistration): ObjectLinkType[] {
129129
canView: (viewer, url, prefetched) =>
130130
link.canView(objectLinkContext(name, viewer, prefetched), url),
131131
filters: link.filters,
132+
...(link.label ? { label: link.label } : undefined),
132133
objectTypes: link.objectTypes,
133134
owner: name,
134135
...(link.prefetch

‎packages/junior/src/api/objects/subject-cards.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ export interface ObjectCardSource {
2828
url?: string | null | undefined;
2929
}
3030

31-
/** The link of one object, and its card when the viewer can read it. */
31+
/**
32+
* The link of one object, and its card when the viewer can read it. A
33+
* source with an owner also gets the label that its link type makes from
34+
* the link, for a card that cannot read the object.
35+
*/
3236
export interface ObjectCardView {
37+
label?: string;
3338
object?: OwnedObjectAnnotation;
3439
url: string;
3540
}
@@ -55,20 +60,25 @@ export async function resolveObjectCards(
5560
? await readSubjectLinks(getDb(), subjects)
5661
: new Map<string, string>();
5762
const linkTypes = readObjectLinkTypes();
58-
const urls = sources.map((source) => {
63+
const labels: Array<string | undefined> = [];
64+
const urls = sources.map((source, index) => {
5965
const url =
6066
(source.subject && links.get(subjectKey(source.subject))) ??
6167
source.url ??
6268
undefined;
6369
if (!url || !source.owner) return url;
6470
const { objectType, plugin } = source.owner;
65-
const linkType = URL.canParse(url)
66-
? findObjectLinkType(linkTypes, new URL(url))
67-
: undefined;
68-
return linkType?.owner === plugin &&
69-
linkType.objectTypes.includes(objectType)
70-
? url
71-
: undefined;
71+
const parsed = URL.parse(url);
72+
const linkType = parsed && findObjectLinkType(linkTypes, parsed);
73+
if (
74+
!parsed ||
75+
linkType?.owner !== plugin ||
76+
!linkType.objectTypes.includes(objectType)
77+
) {
78+
return undefined;
79+
}
80+
labels[index] = linkType.label?.(parsed) ?? parsed.host;
81+
return url;
7282
});
7383
const read = [...new Set(urls.filter((url) => url !== undefined))];
7484
const cards = new Map(
@@ -83,8 +93,13 @@ export async function resolveObjectCards(
8393
if (!url) return undefined;
8494
const object = cards.get(url);
8595
const owner = sources[index]?.owner;
86-
return object && (!owner || object.plugin === owner.plugin)
87-
? { object, url }
88-
: { url };
96+
const label = labels[index];
97+
return {
98+
...(label ? { label } : undefined),
99+
...(object && (!owner || object.plugin === owner.plugin)
100+
? { object }
101+
: undefined),
102+
url,
103+
};
89104
});
90105
}

‎packages/junior/tests/integration/annotation-cards.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ it("reads each delivered card once with the access of the person who started the
595595
kind: "object" as const,
596596
objectType: "code_change" as const,
597597
key: "getsentry/junior#38",
598-
label: "getsentry/junior#38",
598+
// A posted card never shows the stored label. GitHub makes it.
599+
label: "Saved label",
599600
title: "Draft: retry uploads",
600601
status: "open",
601602
url: `${GITHUB_REPO}/pull/38`,
@@ -630,12 +631,13 @@ it("reads each delivered card once with the access of the person who started the
630631

631632
// GitHub is down, so the card posts what Junior saved.
632633
await expect(loadPendingMessageCards(conversationId)).resolves.toEqual([
633-
{ ...saved, plugin: "github" },
634+
{ ...saved, label: "getsentry/junior#38", plugin: "github" },
634635
]);
635636
githubUp = true;
636637
await expect(loadPendingMessageCards(conversationId)).resolves.toEqual([
637638
expect.objectContaining({
638639
key: saved.key,
640+
label: "getsentry/junior#38",
639641
plugin: "github",
640642
status: "merged",
641643
title: "Retry uploads",

‎packages/junior/tests/integration/api/conversations/detail.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ describe("conversation detail API", () => {
490490
kind: "object",
491491
objectType: "code_change",
492492
key: "getsentry/junior#35",
493-
label: "getsentry/junior#35",
493+
// A card never shows the stored label. GitHub makes it.
494+
label: "Saved label",
494495
title: "Draft title",
495496
status: "open",
496497
url: `${GITHUB_REPO}/pull/35`,
@@ -504,6 +505,7 @@ describe("conversation detail API", () => {
504505
expect.objectContaining({
505506
key: "getsentry/junior#35",
506507
kind: "object",
508+
label: "getsentry/junior#35",
507509
plugin: "github",
508510
status: "merged",
509511
title: "Show screenshots on cards",
@@ -531,7 +533,8 @@ describe("conversation detail API", () => {
531533
source: "github",
532534
});
533535

534-
// A reader without access to the repository sees the label and link.
536+
// A reader without access to the repository sees the label that
537+
// GitHub makes from the link, and the link.
535538
const outsider = await readConversationDetail(conversationId, {
536539
viewer: await signedInViewer("[email protected]"),
537540
});
@@ -548,7 +551,7 @@ describe("conversation detail API", () => {
548551
},
549552
]);
550553
expect(JSON.stringify(outsider?.sidebarAnnotations)).not.toMatch(
551-
/Draft title|Show screenshots|merged|open/,
554+
/Draft title|Saved label|Show screenshots|merged|open/,
552555
);
553556
} finally {
554557
vi.unstubAllEnvs();

‎packages/junior/tests/integration/api/conversations/sidebar-annotations.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe("conversation list sidebar annotations", () => {
274274
kind: "object",
275275
objectType: "code_change",
276276
key: "getsentry/junior#36",
277-
label: "getsentry/junior#36",
277+
label: "Saved label",
278278
title: "Draft: retry uploads",
279279
status: "open",
280280
url: `${GITHUB_REPO}/pull/36`,

‎packages/junior/tests/integration/slack/automation-details-webhook.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ describe("Slack Work Object details", () => {
328328
kind: "object" as const,
329329
plugin: "github",
330330
key: "getsentry/junior#37",
331-
label: "getsentry/junior#37",
331+
// Details never show the stored label. GitHub makes it.
332+
label: "Saved label",
332333
title: "Draft: live card details",
333334
objectType: "code_change" as const,
334335
status: "open",
@@ -377,7 +378,7 @@ describe("Slack Work Object details", () => {
377378
},
378379
});
379380
expect(JSON.stringify(outsider)).not.toMatch(
380-
/Draft: live card details|Show live card details|"open"|"merged"/,
381+
/Draft: live card details|Saved label|Show live card details|"open"|"merged"/,
381382
);
382383
} finally {
383384
vi.unstubAllEnvs();

0 commit comments

Comments
 (0)