Skip to content

Commit 4f8a14a

Browse files
author
Rachel Macfarlane
committed
Improve error message when comment creation fails because of a pending review, fixes #255
1 parent 5a95e31 commit 4f8a14a

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/github/pullRequestManager.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,31 +317,47 @@ export class PullRequestManager implements IPullRequestManager {
317317
async createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string): Promise<Comment> {
318318
const { octokit, remote } = await (pullRequest as PullRequestModel).githubRepository.ensure();
319319

320-
let ret = await octokit.pullRequests.createCommentReply({
321-
owner: remote.owner,
322-
repo: remote.repositoryName,
323-
number: pullRequest.prNumber,
324-
body: body,
325-
in_reply_to: Number(reply_to)
326-
});
320+
try {
321+
let ret = await octokit.pullRequests.createCommentReply({
322+
owner: remote.owner,
323+
repo: remote.repositoryName,
324+
number: pullRequest.prNumber,
325+
body: body,
326+
in_reply_to: Number(reply_to)
327+
});
327328

328-
return ret.data;
329+
return ret.data;
330+
} catch (e) {
331+
if (e.code && e.code === 422) {
332+
throw new Error('There is already a pending review for this pull request on GitHub. Please finish or dismiss this review to be able to leave more comments');
333+
} else {
334+
throw e;
335+
}
336+
}
329337
}
330338

331339
async createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number): Promise<Comment> {
332340
const { octokit, remote } = await (pullRequest as PullRequestModel).githubRepository.ensure();
333341

334-
let ret = await octokit.pullRequests.createComment({
335-
owner: remote.owner,
336-
repo: remote.repositoryName,
337-
number: pullRequest.prNumber,
338-
body: body,
339-
commit_id: pullRequest.head.sha,
340-
path: path,
341-
position: position
342-
});
342+
try {
343+
let ret = await octokit.pullRequests.createComment({
344+
owner: remote.owner,
345+
repo: remote.repositoryName,
346+
number: pullRequest.prNumber,
347+
body: body,
348+
commit_id: pullRequest.head.sha,
349+
path: path,
350+
position: position
351+
});
343352

344-
return ret.data;
353+
return ret.data;
354+
} catch (e) {
355+
if (e.code && e.code === 422) {
356+
throw new Error('There is already a pending review for this pull request on GitHub. Please finish or dismiss this review to be able to leave more comments');
357+
} else {
358+
throw e;
359+
}
360+
}
345361
}
346362

347363
async closePullRequest(pullRequest: IPullRequestModel): Promise<any> {

0 commit comments

Comments
 (0)