From e84bf99d40ca59641ec6c25eaf5b2e158180064f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Thu, 30 Aug 2018 16:31:54 -0700 Subject: [PATCH] Use PR reviews (#20927) Summary: Use GitHub PR Reviews instead of individual comments. The result is similar to the existing implementation, but there will be a top level comment indicating possible next steps for the PR author. Verified on Circle. Pull Request resolved: https://github.com/facebook/react-native/pull/20927 Differential Revision: D9596595 Pulled By: hramos fbshipit-source-id: 3628b0097aa9a21a40089f2cbe1859bd64ccd8b7 --- scripts/circleci/code-analysis-bot.js | 62 ++++++++++++++++++++------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/scripts/circleci/code-analysis-bot.js b/scripts/circleci/code-analysis-bot.js index 8c47b9e0d..42cdd00e8 100644 --- a/scripts/circleci/code-analysis-bot.js +++ b/scripts/circleci/code-analysis-bot.js @@ -29,6 +29,7 @@ if (!process.env.CIRCLE_PR_NUMBER) { process.exit(0); } +// https://octokit.github.io/rest.js/ const octokit = require('@octokit/rest')(); var path = require('path'); @@ -150,6 +151,34 @@ function getLineMapFromPatch(patchString) { return lineMap; } +function sendReview(owner, repo, number, commit_id, comments) { + if (comments.length === 0) { + // Do not leave an empty review. + return; + } + + const body = + '`eslint` found some issues. You may run `yarn prettier` or `npm run prettier` to fix these.'; + const event = 'REQUEST_CHANGES'; + + const opts = { + owner, + repo, + number, + commit_id, + body, + event, + comments, + }; + + octokit.pullRequests.createReview(opts, function(error, res) { + if (error) { + console.error(error); + return; + } + }); +} + function sendComment(owner, repo, number, sha, filename, lineMap, message) { if (!lineMap[message.line]) { // Do not send messages on lines that did not change @@ -161,10 +190,10 @@ function sendComment(owner, repo, number, sha, filename, lineMap, message) { repo, number, sha, - path: filename, commit_id: sha, - body: message.message, + path: filename, position: lineMap[message.line], + body: message.message, }; octokit.pullRequests.createComment(opts, function(error, res) { if (error) { @@ -183,6 +212,7 @@ function main(messages, owner, repo, number) { getShaFromPullRequest(owner, repo, number, sha => { getFilesFromCommit(owner, repo, sha, files => { + var comments = []; files.filter(file => messages[file.filename]).forEach(file => { // github api sometimes does not return a patch on large commits if (!file.patch) { @@ -190,19 +220,21 @@ function main(messages, owner, repo, number) { } var lineMap = getLineMapFromPatch(file.patch); messages[file.filename].forEach(message => { - sendComment( - owner, - repo, - number, - sha, - file.filename, - lineMap, - message, - ); - }); - }); - }); - }); + if (lineMap[message.line]) { + var comment = { + path: file.filename, + position: lineMap[message.line], + body: message.message, + }; + + comments.push(comment); + } + }); // forEach + }); // filter + + sendReview(owner, repo, number, sha, comments); + }); // getFilesFromCommit + }); // getShaFromPullRequest } var content = '';