Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit a00662f

Browse files
committed
Add a utility to wrap long lines of text
1 parent c5fad9b commit a00662f

File tree

3 files changed

+142
-70
lines changed

3 files changed

+142
-70
lines changed

src/comments.ts

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sha256 } from "./util/util";
1+
import { sha256, txt } from "./util/util";
22

33
// use `deletedWhenNotPresent` for comments that should be removed if not in the actions
44
export const tagsToDeleteIfNotPosted: string[] = [];
@@ -11,85 +11,113 @@ export type Comment = { tag: string, status: string };
1111

1212
export const HadError = (user: string | undefined, error: string) => ({
1313
tag: "had-error",
14-
status: `${user ? `@${user} — ` : ""}There was an error that prevented me from properly processing this PR:
15-
16-
${error}`
14+
status: txt`
15+
|${user ? `@${user} — ` : ""}There was an error that prevented me from properly processing
16+
this PR:
17+
|
18+
| ${error}`
1719
});
1820

1921
export const CIFailed = (abbrOid: string, user: string, ciUrl: string) => ({
2022
tag: `gh-actions-complaint-${abbrOid}`,
21-
status: `@${user} The CI build failed! Please [review the logs for more information](${ciUrl}).
22-
23-
Once you've pushed the fixes, the build will automatically re-run. Thanks!
24-
25-
**Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review.**`
23+
status: txt`
24+
|@${user} The CI build failed! Please [review the logs for more information](${ciUrl}).
25+
|
26+
|Once you've pushed the fixes, the build will automatically re-run. Thanks!
27+
|
28+
|**Note: builds which are failing do not end up on the list of PRs for the DT
29+
maintainers to review.**`
2630
});
2731

2832
export const MergeConflicted = (abbrOid: string, user: string) => ({
2933
tag: `merge-complaint-${abbrOid}`,
30-
status: `@${user} Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day!`
34+
status: txt`
35+
|@${user} Unfortunately, this pull request currently has a merge conflict 😥.
36+
Please update your PR branch to be up-to-date with respect to master. Have a nice day!`
3137
});
3238

3339
export const ChangesRequest = (abbrOid: string, user: string) => ({
3440
tag: `reviewer-complaint-${abbrOid}`,
35-
status: `@${user} One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you!`
41+
status: txt`
42+
|@${user} One or more reviewers has requested changes. Please address their comments.
43+
I'll be back once they sign off or you've pushed new commits. Thank you!`
3644
});
3745

3846
export const SuggestTesting = deletedWhenNotPresent("suggest-testing", tag =>
3947
(user: string, testsLink: string) => ({
40-
tag, status: `Hey @${user},
41-
42-
:unamused: Your PR doesn't modify any tests, so it's hard to know what's being fixed, and your changes might regress in the future. Please consider [adding tests](${testsLink}) to cover the change you're making. Including tests allows this PR to be merged by yourself and the owners of this module.
43-
44-
***This can potentially save days of time for you!***`
48+
tag, status: txt`
49+
|Hey @${user},
50+
|
51+
|:unamused: Your PR doesn't modify any tests, so it's hard to know what's being fixed,
52+
and your changes might regress in the future. Please consider [adding tests](${testsLink})
53+
to cover the change you're making. Including tests allows this PR to be merged by
54+
yourself and the owners of this module.
55+
|
56+
|***This can potentially save days of time for you!***`
4557
}));
4658

4759
export const PingReviewers = (names: readonly string[], reviewLink: string) => ({
4860
tag: "pinging-reviewers",
49-
status: `🔔 ${names.map(n => `@${n}`).join(" ")} — please [review this PR](${reviewLink}) in the next few days. Be sure to explicitly select **\`Approve\`** or **\`Request Changes\`** in the GitHub UI so I know what's going on.`
61+
status: txt`
62+
|🔔 ${names.map(n => `@${n}`).join(" ")} — please [review this PR](${reviewLink}) in the
63+
next few days. Be sure to explicitly select **\`Approve\`** or **\`Request Changes\`**
64+
in the GitHub UI so I know what's going on.`
5065
});
5166

5267
export const PingReviewersOther = (user: string, reviewLink: string) => ({
5368
tag: "pinging-reviewers-others",
54-
status: `🔔 @${user} — you're the only owner, but it would still be good if you find someone to [review this PR](${reviewLink}) in the next few days, otherwise a maintainer will look at it. (And if you do find someone, maybe even recruit them to be a second owner to make future changes easier...)`
69+
status: txt`
70+
|🔔 @${user} — you're the only owner, but it would still be good if you find someone to
71+
[review this PR](${reviewLink}) in the next few days, otherwise a maintainer will look at it.
72+
(And if you do find someone, maybe even recruit them to be a second owner to make future
73+
changes easier...)`
5574
});
5675

5776
export const PingReviewersTooMany = (names: readonly string[]) => ({
5877
tag: "pinging-reviewers-too-many",
59-
status: `⚠️ There are too many reviewers for this PR change (${names.length}). Merging can only be handled by a DT maintainer.
60-
61-
<details>
62-
<summary>People who would have been pinged</summary>
63-
${names.map(n => `${n}`).join(" ")}
64-
</details>`
78+
status: txt`
79+
|⚠️ There are too many reviewers for this PR change (${names.length}).
80+
Merging can only be handled by a DT maintainer.
81+
|
82+
|<details>
83+
|<summary>People who would have been pinged</summary>
84+
|${names.map(n => `${n}`).join(" ")}
85+
|</details>`
6586
});
6687

6788
export const PingStaleReviewer = (reviewedAbbrOid: string, reviewers: string[]) => ({
6889
tag: `stale-ping-${sha256(reviewers.join("-")).substr(0, 6)}-${reviewedAbbrOid}`,
69-
status: `@${reviewers.join(", @")} Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review?`
90+
status: txt`
91+
|@${reviewers.join(", @")} Thank you for reviewing this PR! The author has pushed new
92+
commits since your last review. Could you take another look and submit a fresh review?`
7093
});
7194

7295
export const OfferSelfMerge = deletedWhenNotPresent("merge-offer", tag =>
7396
(user: string, otherOwners: string[], abbrOid: string) => ({
7497
// Note: pr-info.ts searches for the `(at ${abbrOid})`
75-
tag, status: `@${user} Everything looks good here. Great job! I am ready to merge this PR (at ${abbrOid}) on your behalf.
76-
77-
If you'd like that to happen, please post a comment saying:
78-
79-
> Ready to merge
80-
81-
and I'll merge this PR almost instantly. Thanks for helping out! :heart:
82-
${otherOwners.length === 0 ? "" : `
83-
(${otherOwners.map(o => "@" + o).join(", ")}: you can do this too.)`}`}));
98+
tag, status: txt`
99+
|@${user} Everything looks good here. Great job! I am ready to merge this PR
100+
(at ${abbrOid}) on your behalf.
101+
|
102+
|If you'd like that to happen, please post a comment saying:
103+
|
104+
|> Ready to merge
105+
|
106+
|and I'll merge this PR almost instantly. Thanks for helping out! :heart:
107+
|${otherOwners.length === 0 ? "" : `
108+
|(${otherOwners.map(o => "@" + o).join(", ")}: you can do this too.)`}`}));
84109

85110
export const WaitUntilMergeIsOK = (user: string, abbrOid: string, uri: string, mainCommentID: number | undefined) => ({
86111
// at most one reminder per update
87112
tag: `wait-for-merge-offer-${abbrOid}`,
88-
status: `:passport_control: Hi @${user},
89-
90-
I can't [accept a pull request](${uri}) until all of the checks in the "Status" section of [this comment](#issuecomment-${mainCommentID || "???"}) are green. I will let you know once that happens.
91-
92-
Thanks, and happy typing!`
113+
status: txt`
114+
|:passport_control: Hi @${user},
115+
|
116+
|I can't [accept a pull request](${uri}) until all of the checks in the "Status" section of
117+
[this comment](#issuecomment-${mainCommentID || "???"}) are green.
118+
I will let you know once that happens.
119+
|
120+
|Thanks, and happy typing!`
93121
});
94122

95123
// Explanation for the stalness count in the welcome message
@@ -107,24 +135,38 @@ export const StalenessComment = (author: string, ownersToPing: string[], expires
107135
const ownerPing = ownersToPing.map(o => "@"+o).join(", ");
108136
return {
109137
// --Unmerged--
110-
"Unmerged:nearly": `Re-ping @${author} / ${ownerPing}:
111-
112-
This PR has been ready to merge for over a week, and I haven't seen any requests to merge it. I will close it on ${expires} (in three weeks) if this doesn't happen.
113-
114-
(If there's no reason to avoid merging it, please do so. Otherwise, if it shouldn't be merged or if it needs more time, please close it or turn it into a draft.)`,
115-
"Unmerged:done": `After a month, no one has requested merging the PR 😞. I'm going to assume that the change is not wanted after all, and will therefore close it.`,
138+
"Unmerged:nearly": txt`
139+
|Re-ping @${author} / ${ownerPing}:
140+
|
141+
|This PR has been ready to merge for over a week, and I haven't seen any requests to
142+
merge it. I will close it on ${expires} (in three weeks) if this doesn't happen.
143+
|
144+
|(If there's no reason to avoid merging it, please do so. Otherwise, if it shouldn't
145+
be merged or if it needs more time, please close it or turn it into a draft.)`,
146+
"Unmerged:done": txt`
147+
|After a month, no one has requested merging the PR 😞. I'm going to assume that the
148+
change is not wanted after all, and will therefore close it.`,
116149
// --Abandoned--
117-
"Abandoned:nearly": `@${author} I haven't seen any activity on this PR in more than three weeks, and it still has problems that prevent it from being merged. The PR will be closed on ${expires} (in a week) if the issues aren't addressed.`,
118-
"Abandoned:done": `@${author} To keep things tidy, we have to close PRs that aren't mergeable and don't have activity in the last month. No worries, though — please open a new PR if you'd like to continue with this change. Thank you!`,
150+
"Abandoned:nearly": txt`
151+
|@${author} I haven't seen any activity on this PR in more than three weeks, and it
152+
still has problems that prevent it from being merged. The PR will be closed on
153+
${expires} (in a week) if the issues aren't addressed.`,
154+
"Abandoned:done": txt`
155+
|@${author} To keep things tidy, we have to close PRs that aren't mergeable and don't
156+
have activity in the last month. No worries, though — please open a new PR if you'd
157+
like to continue with this change. Thank you!`,
119158
// --Unreviewed--
120-
"Unreviewed:nearly": `Re-ping ${ownerPing}:
121-
122-
This PR has been out for over a week, yet I haven't seen any reviews.
123-
124-
Could someone please give it some attention? Thanks!`,
125-
"Unreviewed:done": `It has been more than two weeks and this PR still has no reviews.
126-
127-
I'll bump it to the DT maintainer queue. Thank you for your patience, @${author}.
128-
129-
(Ping ${ownerPing}.)`} as { [k: string]: string };
159+
"Unreviewed:nearly": txt`
160+
|Re-ping ${ownerPing}:
161+
|
162+
|This PR has been out for over a week, yet I haven't seen any reviews.
163+
|
164+
|Could someone please give it some attention? Thanks!`,
165+
"Unreviewed:done": txt`
166+
|It has been more than two weeks and this PR still has no reviews.
167+
|
168+
|I'll bump it to the DT maintainer queue. Thank you for your patience, @${author}.
169+
|
170+
|(Ping ${ownerPing}.)`
171+
} as { [k: string]: string };
130172
};

src/compute-pr-actions.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as emoji from "./emoji";
44
import * as urls from "./urls";
55
import { PrInfo, BotResult, FileInfo } from "./pr-info";
66
import { ReviewInfo } from "./pr-info";
7-
import { noNullish, flatten, unique, sameUser, min, sha256, abbrOid } from "./util/util";
7+
import { noNullish, flatten, unique, sameUser, min, sha256, abbrOid, txt } from "./util/util";
88
import * as dayjs from "dayjs";
99
import * as advancedFormat from "dayjs/plugin/advancedFormat";
1010
dayjs.extend(advancedFormat);
@@ -370,7 +370,8 @@ function createWelcomeComment(info: ExtendedPrInfo, post: (c: Comments.Comment)
370370
const testsLink = info.hasNewPackages ? urls.testingNewPackages : urls.testingEditedPackages;
371371

372372
const specialWelcome = !info.isFirstContribution ? `` :
373-
` I see this is your first time submitting to DefinitelyTyped 👋 — I'm the local bot who will help you through the process of getting things through.`;
373+
txt`| I see this is your first time submitting to DefinitelyTyped 👋
374+
— I'm the local bot who will help you through the process of getting things through.`;
374375
display(`@${info.author} Thank you for submitting this PR!${specialWelcome}`,
375376
``,
376377
`***This is a live comment which I will keep updated.***`);
@@ -448,7 +449,9 @@ function createWelcomeComment(info: ExtendedPrInfo, post: (c: Comments.Comment)
448449
}
449450
if (addedSelfToManyOwners > 0) {
450451
display(``,
451-
`@${info.author}: I see that you have added yourself as an owner${addedSelfToManyOwners > 1 ? " to several packages" : ""}, are you sure you want to [become an owner](${urls.definitionOwners})?`);
452+
txt`@${info.author}: I see that you have added yourself as an
453+
owner${addedSelfToManyOwners > 1 ? " to several packages" : ""},
454+
are you sure you want to [become an owner](${urls.definitionOwners})?`);
452455
}
453456

454457
// Lets the author know who needs to review this
@@ -458,18 +461,26 @@ function createWelcomeComment(info: ExtendedPrInfo, post: (c: Comments.Comment)
458461
if (info.blessingKind === "merge") {
459462
display("This PR can be merged.");
460463
} else if (info.hasNewPackages) {
461-
display(`This PR adds a new definition, so it needs to be reviewed by ${requiredApprover} before it can be merged.`);
464+
display(txt`This PR adds a new definition, so it needs to be reviewed by
465+
${requiredApprover} before it can be merged.`);
462466
} else if (info.popularityLevel === "Critical" && info.blessingKind !== "review") {
463-
display(`Because this is a widely-used package, ${requiredApprover} will need to review it before it can be merged.`);
467+
display(txt`Because this is a widely-used package, ${requiredApprover}
468+
will need to review it before it can be merged.`);
464469
} else if (!info.requireMaintainer) {
465-
const and = info.hasDefinitions && info.hasTests ? "and updated the tests (👏)" : "and there were no type definition changes";
466-
display(`Because you edited one package ${and}, I can help you merge this PR once someone else signs off on it.`);
470+
const and = info.hasDefinitions && info.hasTests
471+
? "and updated the tests (👏)"
472+
: "and there were no type definition changes";
473+
display(txt`Because you edited one package ${and}, I can help you merge this PR
474+
once someone else signs off on it.`);
467475
} else if (info.noOtherOwners && info.blessingKind !== "review") {
468-
display(`There aren't any other owners of this package, so ${requiredApprover} will review it.`);
476+
display(txt`There aren't any other owners of this package,
477+
so ${requiredApprover} will review it.`);
469478
} else if (info.hasMultiplePackages && info.blessingKind !== "review") {
470-
display(`Because this PR edits multiple packages, it can be merged once it's reviewed by ${requiredApprover}.`);
479+
display(txt`Because this PR edits multiple packages, it can be merged
480+
once it's reviewed by ${requiredApprover}.`);
471481
} else if (info.checkConfig && info.blessingKind !== "review") {
472-
display(`Because this PR edits the configuration file, it can be merged once it's reviewed by ${requiredApprover}.`);
482+
display(txt`Because this PR edits the configuration file, it can be merged
483+
once it's reviewed by ${requiredApprover}.`);
473484
} else if (info.blessingKind !== "review") {
474485
display(`This PR can be merged once it's reviewed by ${requiredApprover}.`);
475486
} else {
@@ -518,9 +529,12 @@ function createWelcomeComment(info: ExtendedPrInfo, post: (c: Comments.Comment)
518529

519530
display(``);
520531
if (!info.canBeSelfMerged) {
521-
display(`Once every item on this list is checked, I'll ask you for permission to merge and publish the changes.`);
532+
display(txt`Once every item on this list is checked,
533+
I'll ask you for permission to merge and publish the changes.`);
522534
} else {
523-
display(`All of the items on the list are green. **To merge, you need to post a comment including the string "Ready to merge"** to bring in your changes.`);
535+
display(txt`All of the items on the list are green.
536+
**To merge, you need to post a comment including the string "Ready to merge"**
537+
to bring in your changes.`);
524538
}
525539

526540
if (info.staleness && info.staleness.state !== "fresh") {

src/util/util.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export function authorNotBot(node: { login: string } | { author?: { login: strin
4747
}
4848

4949
export function scrubDiagnosticDetails(s: string) {
50-
return s.replace(/<details><summary>Diagnostic Information.*?<\/summary>(?:\\n)+```json\\n{.*?\\n}\\n```(?:\\n)+<\/details>/sg, "... diagnostics scrubbed ...");
50+
return s.replace(/<details><summary>Diagnostic Information.*?<\/summary>(?:\\n)+```json\\n{.*?\\n}\\n```(?:\\n)+<\/details>/sg,
51+
"... diagnostics scrubbed ...");
5152
}
5253

5354
export function sha256(s: string) {
@@ -57,3 +58,18 @@ export function sha256(s: string) {
5758
export function abbrOid(s: string) {
5859
return s.slice(0, 7);
5960
}
61+
62+
// Remove when the fix propagates to a published version
63+
interface HACK {
64+
raw(template: { raw: readonly string[] | ArrayLike<string>}, ...substitutions: any[]): string;
65+
}
66+
67+
// Convenient utility for long texts: trimmed, then remove all spaces up to a
68+
// "|" on each line, and lines that have no "|" at the beginning are joined with
69+
// the previous line (with a single space). (Should really be mapped only on `strs`
70+
// only, otherwise there're compositionality problems (one place in comments.ts).)
71+
export function txt(strs: TemplateStringsArray, ...xs: any) {
72+
return (String as HACK).raw({ raw: strs }, ...xs)
73+
.trim().replace(/(^|\n) *([^\s])/g, (_m, pfx, sfx) =>
74+
sfx === "|" ? pfx : pfx ? " " + sfx : sfx);
75+
}

0 commit comments

Comments
 (0)