Skip to content

Commit f077159

Browse files
meorphismeorphis
andauthored
fix: disambiguate project name in comment title (#225)
* fix: disambiguate project name in comment title * fix * lint --------- Co-authored-by: meorphis <eric@stainless.com>
1 parent b8802cc commit f077159

8 files changed

Lines changed: 59 additions & 45 deletions

File tree

dist/build.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/merge.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/preview.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__snapshots__/comment.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`printComment > should print comment 1`] = `
4-
"<h3>✱ Stainless preview builds</h3>
4+
"<h3>✱ Stainless preview builds for test-project</h3>
55
66
This PR will update the <code>test-project</code> SDKs with the following commit message.
77

src/comment.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ describe("printComment", () => {
1717
});
1818

1919
it("should print no changes comment", () => {
20-
expect(printComment({ noChanges: true })).toMatchInlineSnapshot(`
21-
"<h3>✱ Stainless preview builds</h3>
20+
expect(printComment({ noChanges: true, projectName: "fake project" }))
21+
.toMatchInlineSnapshot(`
22+
"<h3>✱ Stainless preview builds for fake project</h3>
2223
2324
No changes were made to the SDKs.
2425

src/comment.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
sortDiagnostics,
1212
} from "./outcomes";
1313

14-
const COMMENT_TITLE = MD.Heading(
15-
`${MD.Symbol.HeavyAsterisk} Stainless preview builds`,
16-
);
14+
const COMMENT_TITLE = (projectName: string | null) =>
15+
MD.Heading(
16+
`${MD.Symbol.HeavyAsterisk} Stainless preview builds${projectName ? ` for ${projectName}` : ""}`,
17+
);
1718

1819
const COMMENT_FOOTER_DIVIDER = MD.Comment("stainless-preview-footer");
1920

@@ -41,8 +42,11 @@ export function printComment({
4142
outcomes,
4243
}:
4344
| ({ noChanges?: never } & Omit<PrintCommentOptions, "noChanges">)
44-
| ({ noChanges: true } & {
45-
[K in keyof Omit<PrintCommentOptions, "noChanges">]?: never;
45+
| ({ noChanges: true; projectName: string } & {
46+
[K in keyof Omit<
47+
Omit<PrintCommentOptions, "noChanges">,
48+
"projectName"
49+
>]?: never;
4650
})) {
4751
const Blocks = (() => {
4852
if (noChanges) {
@@ -84,7 +88,7 @@ export function printComment({
8488
.replace(/\.\d+Z$/, " UTC");
8589

8690
const fullComment = MD.Dedent`
87-
${COMMENT_TITLE}
91+
${COMMENT_TITLE(projectName)}
8892
8993
${Blocks}
9094
@@ -564,11 +568,16 @@ export function parseCommitMessages(body?: string | null): {
564568
: {};
565569
}
566570

567-
export async function retrieveComment(prNumber: number) {
571+
export async function retrieveComment(prNumber: number, projectName: string) {
568572
const comments = await api().listComments(prNumber);
569573

570-
const existingComment = comments.find((comment) =>
571-
comment.body?.includes(COMMENT_TITLE),
574+
const existingComment = comments.find(
575+
(comment) =>
576+
comment.body?.includes(COMMENT_TITLE(projectName)) ||
577+
// backwards compatibility for comments that don't include the project name
578+
// TODO: remove this fallback eventually
579+
(!comment.body?.includes(COMMENT_TITLE(null) + " for") &&
580+
comment.body?.includes(COMMENT_TITLE(null))),
572581
);
573582

574583
if (!existingComment) {

src/merge.run.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ export async function runMerge(
117117
}
118118

119119
const comment =
120-
makeComment && prNumber ? await retrieveComment(prNumber) : null;
120+
makeComment && prNumber
121+
? await retrieveComment(prNumber, projectName)
122+
: null;
121123
const commitMessage =
122124
comment?.commitMessage ??
123125
makeCommitMessageConventional(defaultCommitMessage);

src/preview.run.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export async function runPreview(
139139
if (makeComment) {
140140
logger.group("Updating comment");
141141

142-
const commentBody = printComment({ noChanges: true });
142+
const commentBody = printComment({ noChanges: true, projectName });
143143

144144
await upsertComment(prNumber, {
145145
body: commentBody,
@@ -163,7 +163,9 @@ export async function runPreview(
163163

164164
logger.groupEnd();
165165

166-
const initialComment = makeComment ? await retrieveComment(prNumber) : null;
166+
const initialComment = makeComment
167+
? await retrieveComment(prNumber, projectName)
168+
: null;
167169
let commitMessage =
168170
initialComment?.commitMessage ??
169171
makeCommitMessageConventional(defaultCommitMessage);
@@ -215,7 +217,7 @@ export async function runPreview(
215217
const { outcomes, baseOutcomes } = latestRun;
216218

217219
// In case the comment was updated between polls:
218-
const comment = await retrieveComment(prNumber);
220+
const comment = await retrieveComment(prNumber, projectName);
219221
commitMessage = comment?.commitMessage ?? commitMessage;
220222
targetCommitMessages =
221223
comment?.targetCommitMessages ?? targetCommitMessages;

0 commit comments

Comments
 (0)