Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestEditing.logic.ts

47 lines1.7 KB

1import type {
2 PullRequestComment,
3 PullRequestDetail,
4 PullRequestReviewThread,
5} from "@t3tools/contracts";
6​
7type EditingSubject = Pick<
8 PullRequestDetail,
9 "author" | "capabilities" | "viewer" | "viewerPermissions"
10>;
11​
12function sameLogin(one: string | null | undefined, other: string | null | undefined): boolean {
13 if (one == null || other == null) return false;
14 return one.trim().toLowerCase() === other.trim().toLowerCase();
16​
17export function canEditPullRequestChangeRequest(detail: EditingSubject): boolean {
18 if (detail.capabilities.edit?.changeRequest !== true) return false;
19 if (detail.viewerPermissions.editChangeRequest !== undefined) {
20 return detail.viewerPermissions.editChangeRequest;
21 }
22 return (
23 sameLogin(detail.viewer, detail.author?.login) ||
24 detail.viewerPermissions.actions.includes("merge")
25 );
27​
28/**
29 * A review's own summary is left out: the hosts disagree about what one even is, and none of
30 * them takes a rewrite of it through the mutation a comment goes through.
31 */
32export function canEditPullRequestComment(
33 detail: EditingSubject,
34 comment: Pick<PullRequestComment, "author" | "canEdit" | "kind">,
35): boolean {
36 if (detail.capabilities.edit?.comment !== true) return false;
37 if (comment.kind !== "issue-comment" && comment.kind !== "review-comment") return false;
38 if (comment.canEdit !== undefined) return comment.canEdit;
39 return sameLogin(detail.viewer, comment.author?.login);
41​
42export function canResolvePullRequestThread(
43 detail: Pick<PullRequestDetail, "viewerPermissions">,
44 thread: Pick<PullRequestReviewThread, "canResolve">,
45): boolean {
46 return detail.viewerPermissions.resolve && thread.canResolve !== false;