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

pullRequestReviewDrafts.ts

85 lines2.7 KB

1import type { PullRequestReviewCommentDraft } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { PullRequestReviewCommentDraft as CommentSchema } from "@t3tools/contracts";
4import type { PullRequestParams } from "./usePullRequestDiff";
5​
6export type PendingReviewComment = PullRequestReviewCommentDraft & {
7 readonly id: string;
8 readonly headSha: string;
9 readonly code: string;
10};
11export type ReviewDraft = {
12 readonly summary: string;
13 readonly comment: string;
14 readonly comments: readonly PendingReviewComment[];
15};
16export const EMPTY_REVIEW_DRAFT: ReviewDraft = { summary: "", comment: "", comments: [] };
17​
18export function reviewDraftReplacement(draft: ReviewDraft, headSha: string | null, id?: string) {
19 if (!id || headSha === null) return {};
20 return draft.comments.some((comment) => comment.id === id && comment.headSha !== headSha)
21 ? { draftId: id }
22 : { resetOnReload: { replacingDraftId: undefined } };
24​
25export function pullRequestReviewKey(params: PullRequestParams) {
26 return JSON.stringify([
27 params.environmentId,
28 params.projectId,
29 params.host ?? "",
30 params.repository,
31 params.number,
32 ]);
34export const ReviewDraftDocument = Schema.Struct({
35 version: Schema.Literal(1),
36 drafts: Schema.Record(
37 Schema.String,
38 Schema.Struct({
39 summary: Schema.String,
40 comment: Schema.String,
41 comments: Schema.Array(
42 Schema.Struct({
43 ...CommentSchema.fields,
44 id: Schema.String,
45 headSha: Schema.String,
46 code: Schema.String,
47 }),
48 ),
49 }),
50 ),
51});
52export function clearSubmittedReview(current: ReviewDraft, submitted: ReviewDraft): ReviewDraft {
53 return {
54 ...current,
55 summary: current.summary === submitted.summary ? "" : current.summary,
56 comments: current.comments.filter(
57 (comment) =>
58 !submitted.comments.some(
59 (saved) =>
60 saved.id === comment.id &&
61 saved.body === comment.body &&
62 saved.headSha === comment.headSha &&
63 saved.path === comment.path &&
64 saved.oldPath === comment.oldPath &&
65 saved.code === comment.code &&
66 samePosition(saved.position, comment.position),
67 ),
68 ),
69 };
71​
72function samePosition(
73 left: PendingReviewComment["position"],
74 right: PendingReviewComment["position"],
75) {
76 if (left.kind === "added" && right.kind === "added") return left.newLine === right.newLine;
77 if (left.kind === "deleted" && right.kind === "deleted") return left.oldLine === right.oldLine;
78 return (
79 left.kind === "context" &&
80 right.kind === "context" &&
81 left.oldLine === right.oldLine &&
82 left.newLine === right.newLine &&
83 left.side === right.side
84 );