Skip to content

Commit c9a8b99

Browse files
committed
add delete comment
1 parent 83f09ea commit c9a8b99

File tree

1 file changed

+52
-24
lines changed
  • apps/array/src/main/services

1 file changed

+52
-24
lines changed

apps/array/src/main/services/git.ts

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ const getRepositoryFromRemoteUrl = async (
109109
return repoMatch[1];
110110
};
111111

112+
const validatePullRequestNumber = (prNumber: number): void => {
113+
if (
114+
typeof prNumber !== "number" ||
115+
!Number.isInteger(prNumber) ||
116+
prNumber < 1
117+
) {
118+
throw new Error(`Invalid pull request number: ${prNumber}`);
119+
}
120+
};
121+
122+
const validateCommentId = (commentId: number): void => {
123+
if (
124+
typeof commentId !== "number" ||
125+
!Number.isInteger(commentId) ||
126+
commentId < 1
127+
) {
128+
throw new Error(`Invalid comment ID: ${commentId}`);
129+
}
130+
};
131+
112132
export const isGitRepository = async (
113133
directoryPath: string,
114134
): Promise<boolean> => {
@@ -540,14 +560,7 @@ const getAllPullRequestComments = async (
540560
directoryPath: string,
541561
prNumber: number,
542562
): Promise<any> => {
543-
// Validate prNumber: must be a positive integer
544-
if (
545-
typeof prNumber !== "number" ||
546-
!Number.isInteger(prNumber) ||
547-
prNumber < 1
548-
) {
549-
throw new Error(`Invalid pull request number: ${prNumber}`);
550-
}
563+
validatePullRequestNumber(prNumber);
551564

552565
try {
553566
const { stdout } = await execAsync(
@@ -564,14 +577,7 @@ const getPullRequestReviewComments = async (
564577
directoryPath: string,
565578
prNumber: number,
566579
): Promise<any> => {
567-
// Validate prNumber: must be a positive integer
568-
if (
569-
typeof prNumber !== "number" ||
570-
!Number.isInteger(prNumber) ||
571-
prNumber < 1
572-
) {
573-
throw new Error(`Invalid pull request number: ${prNumber}`);
574-
}
580+
validatePullRequestNumber(prNumber);
575581

576582
try {
577583
const repo = await getRepositoryFromRemoteUrl(directoryPath);
@@ -600,14 +606,7 @@ const addPullRequestComment = async (
600606
prNumber: number,
601607
options: AddPullRequestCommentOptions,
602608
): Promise<any> => {
603-
// Validate prNumber: must be a positive integer
604-
if (
605-
typeof prNumber !== "number" ||
606-
!Number.isInteger(prNumber) ||
607-
prNumber < 1
608-
) {
609-
throw new Error(`Invalid pull request number: ${prNumber}`);
610-
}
609+
validatePullRequestNumber(prNumber);
611610

612611
// Validate required options
613612
if (!options.body || !options.commitId || !options.path) {
@@ -637,6 +636,24 @@ const addPullRequestComment = async (
637636
}
638637
};
639638

639+
const deletePullRequestComment = async (
640+
directoryPath: string,
641+
commentId: number,
642+
): Promise<void> => {
643+
validateCommentId(commentId);
644+
645+
try {
646+
const repo = await getRepositoryFromRemoteUrl(directoryPath);
647+
648+
await execAsync(
649+
`gh api repos/${repo}/pulls/comments/${commentId} -X DELETE`,
650+
{ cwd: directoryPath },
651+
);
652+
} catch (error) {
653+
throw new Error(`Failed to delete PR comment: ${error}`);
654+
}
655+
};
656+
640657
export function registerGitIpc(
641658
getMainWindow: () => BrowserWindow | null,
642659
): void {
@@ -950,4 +967,15 @@ export function registerGitIpc(
950967
return addPullRequestComment(directoryPath, prNumber, options);
951968
},
952969
);
970+
971+
ipcMain.handle(
972+
"delete-pr-comment",
973+
async (
974+
_event: IpcMainInvokeEvent,
975+
directoryPath: string,
976+
commentId: number,
977+
): Promise<void> => {
978+
return deletePullRequestComment(directoryPath, commentId);
979+
},
980+
);
953981
}

0 commit comments

Comments
 (0)