From d93d2b670f84dd07c8d115b2d8c956d1f13c0908 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 5 Oct 2020 12:22:44 -0700 Subject: [PATCH 1/2] fix: handle single line comments and add newline --- .../upload-comments-handler.ts | 41 ++++++++++++++----- test/inline-suggest.ts | 10 ++--- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/github-handler/comment-handler/make-review-handler/upload-comments-handler.ts b/src/github-handler/comment-handler/make-review-handler/upload-comments-handler.ts index 541d2dda..4d1316fa 100644 --- a/src/github-handler/comment-handler/make-review-handler/upload-comments-handler.ts +++ b/src/github-handler/comment-handler/make-review-handler/upload-comments-handler.ts @@ -34,6 +34,15 @@ interface MultilineComment { start_side: 'RIGHT' | 'LEFT'; } +interface SingleLineComment { + body: string; + path: string; + line: number; + side: 'RIGHT' | 'LEFT'; +} + +type Comment = SingleLineComment | MultilineComment; + /** * GitHub-defined type. The Octokit library/docs are probably behind since create review already * accept multi-line code comments. However, the API does not reflect that. @@ -53,19 +62,29 @@ export type PullsCreateReviewParamsComments = { */ export function buildReviewComments( suggestions: Map -): MultilineComment[] { - const fileComments: MultilineComment[] = []; +): Comment[] { + const fileComments: Comment[] = []; suggestions.forEach((patches: Patch[], fileName: string) => { patches.forEach(patch => { - const comment: MultilineComment = { - path: fileName, - body: `\`\`\`suggestion\n${patch.newContent}\`\`\``, - start_line: patch.start, - line: patch.end, - side: 'RIGHT', - start_side: 'RIGHT', - }; - fileComments.push(comment); + if (patch.start === patch.end) { + const singleComment: SingleLineComment = { + path: fileName, + body: `\`\`\`suggestion\n${patch.newContent}\n\`\`\``, + line: patch.end, + side: 'RIGHT', + }; + fileComments.push(singleComment); + } else { + const comment: MultilineComment = { + path: fileName, + body: `\`\`\`suggestion\n${patch.newContent}\n\`\`\``, + start_line: patch.start, + line: patch.end, + side: 'RIGHT', + start_side: 'RIGHT', + }; + fileComments.push(comment); + } }); }); return fileComments; diff --git a/test/inline-suggest.ts b/test/inline-suggest.ts index 046109d1..dfc16f07 100644 --- a/test/inline-suggest.ts +++ b/test/inline-suggest.ts @@ -40,7 +40,7 @@ describe('buildFileComments', () => { const comments = buildReviewComments(suggestions); expect(comments).deep.equals([ { - body: '```suggestion\nFoo```', + body: '```suggestion\nFoo\n```', path: 'foo.txt', start_line: 1, line: 2, @@ -68,7 +68,7 @@ describe('buildFileComments', () => { const comments = buildReviewComments(suggestions); expect(comments).deep.equals([ { - body: '```suggestion\nFoo```', + body: '```suggestion\nFoo\n```', path: 'bar.txt', start_line: 1, line: 2, @@ -76,7 +76,7 @@ describe('buildFileComments', () => { start_side: 'RIGHT', }, { - body: '```suggestion\nFoo```', + body: '```suggestion\nFoo\n```', path: 'foo.txt', start_line: 1, line: 2, @@ -84,7 +84,7 @@ describe('buildFileComments', () => { start_side: 'RIGHT', }, { - body: '```suggestion\nBar```', + body: '```suggestion\nBar\n```', path: 'foo.txt', start_line: 3, line: 4, @@ -209,7 +209,7 @@ describe('makeInlineSuggestions', () => { body: '', comments: ([ { - body: '```suggestion\nFoo```', + body: '```suggestion\nFoo\n```', path: 'foo.txt', start_line: 1, line: 2, From ba897b5de219cc7a4f7caf7f391841680609f566 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 5 Oct 2020 12:29:20 -0700 Subject: [PATCH 2/2] test: add test for single line comment --- test/inline-suggest.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/inline-suggest.ts b/test/inline-suggest.ts index dfc16f07..ee6ce505 100644 --- a/test/inline-suggest.ts +++ b/test/inline-suggest.ts @@ -98,6 +98,25 @@ describe('buildFileComments', () => { const comments = buildReviewComments(suggestions); expect(comments.length).deep.equals(0); }); + it('Builds single line comments', () => { + const suggestions: Map = new Map(); + const fileName1 = 'foo.txt'; + const patch1: Patch = { + start: 1, + end: 1, + newContent: 'Foo', + }; + suggestions.set(fileName1, [patch1]); + const comments = buildReviewComments(suggestions); + expect(comments).deep.equals([ + { + body: '```suggestion\nFoo\n```', + path: 'foo.txt', + line: 1, + side: 'RIGHT', + }, + ]); + }); }); describe('makeInlineSuggestions', () => {