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

Commit 48063b6

Browse files
committed
Put explanations next to suggestions
1 parent f2a4dbb commit 48063b6

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

src/comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ I'll bump it to the DT maintainer queue. Thank you for your patience, @${author}
119119

120120
// Introduction to the review when config files diverge from the
121121
// required form
122-
export const Suggestions = (user: string) =>
122+
export const explanations = (user: string) =>
123123
`@${user} I noticed these differences from the required form. If you can revise your changes to avoid them, so much the better! Otherwise please reply with explanations why they're needed and a maintainer will take a look. Thanks!`;

src/compute-pr-actions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Comments from "./comments";
22
import * as urls from "./urls";
33
import { PrInfo, BotError, BotEnsureRemovedFromProject, BotNoPackages, FileInfo } from "./pr-info";
44
import { CIResult } from "./util/CIResult";
5-
import { ReviewInfo, Suggestion } from "./pr-info";
5+
import { ReviewInfo, Explanation } from "./pr-info";
66
import { noNullish, flatten, unique, sameUser, daysSince, sha256 } from "./util/util";
77

88
type ColumnName =
@@ -51,7 +51,7 @@ export interface Actions {
5151
targetColumn?: ColumnName;
5252
labels: LabelName[];
5353
responseComments: Comments.Comment[];
54-
suggestions: ({ path: string } & Suggestion)[];
54+
explanations: ({ path: string } & Explanation)[];
5555
shouldClose: boolean;
5656
shouldMerge: boolean;
5757
shouldUpdateLabels: boolean;
@@ -65,7 +65,7 @@ function createDefaultActions(pr_number: number): Actions {
6565
targetColumn: "Other",
6666
labels: [],
6767
responseComments: [],
68-
suggestions: [],
68+
explanations: [],
6969
shouldClose: false,
7070
shouldMerge: false,
7171
shouldUpdateLabels: true,
@@ -79,7 +79,7 @@ function createEmptyActions(prNumber: number): Actions {
7979
pr_number: prNumber,
8080
labels: [],
8181
responseComments: [],
82-
suggestions: [],
82+
explanations: [],
8383
shouldClose: false,
8484
shouldMerge: false,
8585
shouldUpdateLabels: false,
@@ -303,9 +303,9 @@ export function process(prInfo: PrInfo | BotEnsureRemovedFromProject | BotNoPack
303303
// Update intro comment
304304
post({ tag: "welcome", status: createWelcomeComment(info) });
305305

306-
// Propagate suggestions into actions
307-
context.suggestions = noNullish(flatten(info.pkgInfo.map(pkg => pkg.files.map(({ path, suggestion }) =>
308-
suggestion && { path, ...suggestion }))));
306+
// Propagate explanations into actions
307+
context.explanations = noNullish(flatten(info.pkgInfo.map(pkg => pkg.files.map(({ path, suspect }) =>
308+
suspect && { path, ...suspect }))));
309309

310310
// Ping reviewers when needed
311311
if (!(info.hasChangereqs || info.approvedBy.includes("owner") || info.approvedBy.includes("maintainer"))) {
@@ -531,9 +531,9 @@ function createWelcomeComment(info: ExtendedPrInfo) {
531531
} else if (info.noOtherOwners) {
532532
display(` * ${approved} ${RequiredApprover} can merge changes when there are no other reviewers`);
533533
} else if (info.editsConfig) {
534-
display(` * ${approved} ${RequiredApprover} needs to approve changes which affect module config files`);
535-
info.pkgInfo.forEach(pkg => pkg.files.forEach(file =>
536-
file.suspect && display(` - ${reviewLink(file)}: ${file.suspect}`)));
534+
const links = noNullish(flatten(info.pkgInfo.map(pkg => pkg.files.map(file =>
535+
file.suspect && reviewLink(file)))));
536+
display(` * ${approved} ${RequiredApprover} needs to approve changes which affect module config files (${links.join(", ")})`);
537537
} else {
538538
display(` * ${approved} Only ${requiredApprover} can approve changes [without tests](${testsLink})`);
539539
}

src/execute-pr-actions.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function executePrActions(actions: Actions, info: PRQueryResult, dr
3131
...await getMutationsForProjectChanges(actions, pr),
3232
...getMutationsForComments(actions, pr.id, botComments),
3333
...getMutationsForCommentRemovals(actions, botComments),
34-
...getMutationsForSuggestions(actions, pr),
34+
...getMutationsForExplanations(actions, pr),
3535
...getMutationsForChangingPRState(actions, pr),
3636
]);
3737
if (!dry) {
@@ -113,26 +113,26 @@ function getMutationsForCommentRemovals(actions: Actions, botComments: ParsedCom
113113
});
114114
}
115115

116-
function getMutationsForSuggestions(actions: Actions, pr: PR_repository_pullRequest) {
117-
// Suggestions will be empty if we already reviewed this head
118-
if (actions.suggestions.length === 0) return [];
116+
function getMutationsForExplanations(actions: Actions, pr: PR_repository_pullRequest) {
117+
// Explanations will be empty if we already reviewed this head
118+
if (actions.explanations.length === 0) return [];
119119
if (!pr.author) throw new Error("Internal Error: expected to be checked");
120120
return [
121-
...actions.suggestions.map(({ path, startLine, endLine, text }) =>
121+
...actions.explanations.map(({ path, startLine, endLine, body }) =>
122122
createMutation(addPullRequestReviewThread, {
123123
input: {
124124
pullRequestId: pr.id,
125125
path,
126126
startLine: startLine === endLine ? undefined : startLine,
127127
line: endLine,
128-
body: "```suggestion\n" + text + "```",
128+
body,
129129
}
130130
})
131131
),
132132
createMutation(submitPullRequestReview, {
133133
input: {
134134
pullRequestId: pr.id,
135-
body: comments.Suggestions(pr.author.login),
135+
body: comments.explanations(pr.author.login),
136136
event: "COMMENT",
137137
}
138138
}),

src/pr-info.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@ type FileKind = "test" | "definition" | "markdown" | "package-meta" | "package-m
7474
export type FileInfo = {
7575
path: string,
7676
kind: FileKind,
77-
suspect?: string, // reason for a file being "package-meta" rather than "package-meta-ok"
78-
suggestion?: Suggestion, // The differences from the required form, as GitHub suggestions
77+
suspect?: Explanation // reason for a file being "package-meta" rather than "package-meta-ok"
7978
};
8079

81-
export interface Suggestion {
82-
readonly startLine: number;
83-
readonly endLine: number;
84-
readonly text: string;
80+
export interface Explanation {
81+
readonly startLine?: number;
82+
readonly endLine?: number;
83+
readonly body: string;
8584
}
8685

8786
export type ReviewInfo = {
@@ -201,7 +200,7 @@ export async function queryPRInfo(prNumber: number) {
201200
interface Refs {
202201
readonly head: string;
203202
readonly master: "master";
204-
readonly latestSuggestions: string;
203+
readonly latestExplanations: string;
205204
}
206205

207206
// The GQL response => Useful data for us
@@ -236,8 +235,8 @@ export async function deriveStateForPR(
236235
const refs = {
237236
head: headCommit.oid,
238237
master: "master",
239-
// Exclude existing suggestions from subsequent reviews
240-
latestSuggestions: prInfo.reviews?.nodes?.reduce((latest, review) =>
238+
// Exclude existing explanations from subsequent reviews
239+
latestExplanations: prInfo.reviews?.nodes?.reduce((latest, review) =>
241240
review && !authorNotBot(review) && (
242241
!latest?.submittedAt || review.submittedAt && new Date (review.submittedAt) > new Date(latest.submittedAt))
243242
? review : latest, null)?.commit?.oid,
@@ -393,26 +392,26 @@ async function categorizeFile(path: string, getContents: GetContents): Promise<[
393392
case "md": return [pkg, { path, kind: "markdown" }];
394393
default: {
395394
const suspect = await configSuspicious(path, getContents);
396-
return [pkg, { path, kind: suspect ? "package-meta" : "package-meta-ok", ...suspect }];
395+
return [pkg, { path, kind: suspect ? "package-meta" : "package-meta-ok", suspect }];
397396
}
398397
}
399398
}
400399

401400
interface ConfigSuspicious {
402-
(path: string, getContents: GetContents): Promise<{ suspect: string, sugestion?: Suggestion } | undefined>;
403-
[basename: string]: (newText: string, getContents: GetContents) => Promise<{ suspect: string, suggestion?: Suggestion } | undefined>;
401+
(path: string, getContents: GetContents): Promise<Explanation | undefined>;
402+
[basename: string]: (newText: string, getContents: GetContents) => Promise<Explanation | undefined>;
404403
}
405404
const configSuspicious = <ConfigSuspicious>(async (path, getContents) => {
406405
const basename = path.replace(/.*\//, "");
407406
const tester = configSuspicious[basename];
408-
if (!tester) return { suspect: `edited` };
407+
if (!tester) return { body: `edited` };
409408
const newText = await getContents("head");
410-
if (newText === undefined) return { suspect: `couldn't fetch contents` };
409+
if (newText === undefined) return { body: `couldn't fetch contents` };
411410
return tester(newText, getContents);
412411
});
413412
configSuspicious["OTHER_FILES.txt"] = async newText =>
414413
// not empty
415-
(newText.length === 0) ? { suspect: "empty" }
414+
(newText.length === 0) ? { body: "empty" }
416415
: undefined;
417416
configSuspicious["package.json"] = makeJsonCheckerFromCore(
418417
{ private: true },
@@ -446,23 +445,20 @@ configSuspicious["tsconfig.json"] = makeJsonCheckerFromCore(
446445
function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[], requiredFormUrl?: string) {
447446
return async (newText: string, getContents: GetContents) => {
448447
let suggestion: any;
449-
try { suggestion = JSON.parse(newText); } catch (e) { return { suspect: "couldn't parse json" }; }
448+
try { suggestion = JSON.parse(newText); } catch (e) { return { body: "couldn't parse json" }; }
450449
const newJson = jsonDiff.deepClone(suggestion);
451450
jsonDiff.applyPatch(newJson, ignoredKeys.map(path => ({ op: "remove", path })));
452451
const towardsIt = jsonDiff.deepClone(requiredForm);
453452
// Getting closer to the required form relative to master isn't
454453
// suspect
455454
const vsMaster = await ignoreExistingDiffs("master");
456455
if (!vsMaster) return undefined;
457-
if (vsMaster.done) return { suspect: vsMaster.suspect };
456+
if (vsMaster.done) return { body: vsMaster.suspect };
458457
// whereas getting closer relative to existing suggestions means
459458
// no new suggestions
460-
if (!await ignoreExistingDiffs("latestSuggestions")) return { suspect: vsMaster.suspect };
459+
if (!await ignoreExistingDiffs("latestExplanations")) return { body: vsMaster.suspect };
461460
jsonDiff.applyPatch(suggestion, jsonDiff.compare(newJson, towardsIt));
462-
return {
463-
suspect: vsMaster.suspect,
464-
suggestion: makeJsonSuggestion(),
465-
};
461+
return makeJsonSuggestion();
466462

467463
// Apply any preexisting diffs to towardsIt
468464
async function ignoreExistingDiffs(ref: keyof Refs) {
@@ -511,7 +507,7 @@ function makeJsonCheckerFromCore(requiredForm: any, ignoredKeys: string[], requi
511507
return {
512508
startLine,
513509
endLine,
514-
text: suggestionLines.join(""),
510+
body: vsMaster!.suspect + "\n```suggestion\n" + suggestionLines.join("") + "```",
515511
};
516512
}
517513
};

0 commit comments

Comments
 (0)