Open History to see the last commit
1import type { PullRequestComment, PullRequestDetail } from "@t3tools/contracts";
2
3/** Only the parts of a detail either answer reads, so a caller can pass a whole detail view. */
4type EditingSubject = Pick<
5 PullRequestDetail,
6 "author" | "capabilities" | "viewer" | "viewerPermissions"
7>;
8
9/** Hosts disagree about the case of a login and none of them treats two casings as two people. */
10function sameLogin(one: string | null | undefined, other: string | null | undefined): boolean {
11 if (one == null || other == null) return false;
12 return one.trim().toLowerCase() === other.trim().toLowerCase();
13}
14
15/**
16 * Whether the title and description can be rewritten from here. Beyond the host being able to at
17 * all, either the reader wrote the change request or they may merge it — merging is the one action
18 * every host here grants with write access and withholds without it, so it stands in for the
19 * permission none of them publishes by name.
20 */
21export function canEditPullRequestChangeRequest(detail: EditingSubject): boolean {
22 if (detail.capabilities.edit?.changeRequest !== true) return false;
23 return (
24 sameLogin(detail.viewer, detail.author?.login) ||
25 detail.viewerPermissions.actions.includes("merge")
26 );
27}
28
29/**
30 * Whether this remark can be rewritten from here. A review's own summary is left out: the hosts
31 * disagree about what one even is, and none of them takes a rewrite of it through the mutation
32 * a comment goes through.
33 */
34export function canEditPullRequestComment(
35 detail: EditingSubject,
36 comment: Pick<PullRequestComment, "author" | "kind">,
37): boolean {
38 if (detail.capabilities.edit?.comment !== true) return false;
39 if (comment.kind !== "issue-comment" && comment.kind !== "review-comment") return false;
40 return sameLogin(detail.viewer, comment.author?.login);
41}