react-native-macos/bots/code-analysis-bot.js

351 строка
8.8 KiB
JavaScript
Исходник Обычный вид История

2015-12-10 02:29:00 +03:00
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
2015-12-10 02:29:00 +03:00
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
2015-12-10 02:29:00 +03:00
*/
2015-12-10 02:29:00 +03:00
'use strict';
if (!process.env.GITHUB_OWNER) {
console.error('Missing GITHUB_OWNER. Example: facebook');
process.exit(1);
}
if (!process.env.GITHUB_REPO) {
console.error('Missing GITHUB_REPO. Example: react-native');
2015-12-10 02:29:00 +03:00
process.exit(1);
}
const path = require('path');
2015-12-10 02:29:00 +03:00
function push(arr, key, value) {
if (!arr[key]) {
arr[key] = [];
}
arr[key].push(value);
}
const converterSummary = {
eslint:
'`eslint` found some issues. Run `yarn lint --fix` to automatically fix problems.',
flow: '`flow` found some issues. Run `yarn flow check` to analyze your code and address any errors.',
Bots cleanup, avoid leaving inline reviews when N>5 (#24923) Summary: This PR cleans up some of our GitHub bots. The overall goal is to make the contribution process just a tad nicer. ### analysis-bot * The bot will continue leaving GitHub Reviews when it finds lint issues, but will abstain from leaving inline comments if they would exceed 5 in number. * The review comment left by the bot has instructions on how to reproduce the lint issues locally. This will educate PR authors on how to run lint and fix the issues without unnecessarily spamming the PR with 50+ comments, while still providing useful reviews to authors when only a handful of lint issues slip by. * Code moved to `bots/` directory for ease of discovery and co-location with pull-bot. * Added `yarn lint-ci` command. This seems like the right choice: it's running `yarn lint` and other linters, and it is only intended to run on CI. * It's still possible to run `yarn lint-ci` locally, though the script will stop short of posting a review to GitHub unless the necessary envvars are provided. * Added `yarn shellcheck` command. This can be run locally, though it requires `shellcheck` to be installed. * Outside of this PR, I added instructions on using shellcheck to https://github.com/facebook/react-native/wiki/Development-Dependencies * Updated Circle CI config to use these new commands, and streamlined the `analyze_pr` step. * Documented analysis-bot in `bots/README.md`. ### pull-bot * Bumped `danger-js` dependency. No breaking changes found in this minor bump from what I can tell. * Documented pull-bot in `bots/README.md`. ### misc * PR template: don't use jargon. ## Changelog [Internal] [Changed] - GitHub Bots cleanup Pull Request resolved: https://github.com/facebook/react-native/pull/24923 Differential Revision: D15399744 Pulled By: hramos fbshipit-source-id: 32632e775f8554424072270e3f98542de84bfb8c
2019-05-22 05:35:40 +03:00
shellcheck:
'`shellcheck` found some issues. Run `yarn shellcheck` to analyze shell scripts.',
'google-java-format':
'`google-java-format` found some issues. See https://github.com/google/google-java-format',
};
2015-12-10 02:29:00 +03:00
/**
* There is unfortunately no standard format to report an error, so we have
* to write a specific converter for each tool we want to support.
*
* Those functions take a json object as input and fill the output with the
* following format:
*
* { [ path: string ]: Array< { message: string, line: number }> }
*
* This is an object where the keys are the path of the files and values
* is an array of objects of the shape message and line.
*/
const converters = {
raw: function (output, input) {
for (let key in input) {
input[key].forEach(function (message) {
2015-12-10 02:29:00 +03:00
push(output, key, message);
});
}
},
'google-java-format': function (output, input) {
if (!input) {
return;
}
input.forEach(function (change) {
push(output, change.file, {
message: `\`google-java-format\` suggested changes:
\`\`\`diff
${change.description}
\`\`\`
`,
line: change.line,
converter: 'google-java-format',
});
});
},
flow: function (output, input) {
2015-12-10 02:29:00 +03:00
if (!input || !input.errors) {
return;
}
input.errors.forEach(function (error) {
2015-12-10 02:29:00 +03:00
push(output, error.message[0].path, {
message: error.message.map(message => message.descr).join(' '),
2015-12-10 02:29:00 +03:00
line: error.message[0].line,
converter: 'flow',
2015-12-10 02:29:00 +03:00
});
});
},
eslint: function (output, input) {
2015-12-10 02:29:00 +03:00
if (!input) {
return;
}
input.forEach(function (file) {
file.messages.forEach(function (message) {
2015-12-10 02:29:00 +03:00
push(output, file.filePath, {
message: message.ruleId + ': ' + message.message,
line: message.line,
converter: 'eslint',
2015-12-10 02:29:00 +03:00
});
});
});
},
shellcheck: function (output, input) {
if (!input) {
return;
}
input.forEach(function (report) {
push(output, report.file, {
message:
'**[SC' +
report.code +
'](https://github.com/koalaman/shellcheck/wiki/SC' +
report.code +
'):** (' +
report.level +
') ' +
report.message,
line: report.line,
endLine: report.endLine,
column: report.column,
endColumn: report.endColumn,
converter: 'shellcheck',
});
});
},
2015-12-10 02:29:00 +03:00
};
/**
* Sadly we can't just give the line number to github, we have to give the
* line number relative to the patch file which is super annoying. This
* little function builds a map of line number in the file to line number
* in the patch file
*/
function getLineMapFromPatch(patchString) {
let diffLineIndex = 0;
let fileLineIndex = 0;
let lineMap = {};
2015-12-10 02:29:00 +03:00
patchString.split('\n').forEach(line => {
2015-12-10 02:29:00 +03:00
if (line.match(/^@@/)) {
fileLineIndex = line.match(/\+([0-9]+)/)[1] - 1;
return;
}
diffLineIndex++;
if (line[0] !== '-') {
fileLineIndex++;
if (line[0] === '+') {
lineMap[fileLineIndex] = diffLineIndex;
}
}
});
return lineMap;
}
async function sendReview(
octokit,
owner,
repo,
pull_number,
commit_id,
body,
comments,
) {
if (process.env.GITHUB_TOKEN) {
if (comments.length === 0) {
// Do not leave an empty review.
return;
Bots cleanup, avoid leaving inline reviews when N>5 (#24923) Summary: This PR cleans up some of our GitHub bots. The overall goal is to make the contribution process just a tad nicer. ### analysis-bot * The bot will continue leaving GitHub Reviews when it finds lint issues, but will abstain from leaving inline comments if they would exceed 5 in number. * The review comment left by the bot has instructions on how to reproduce the lint issues locally. This will educate PR authors on how to run lint and fix the issues without unnecessarily spamming the PR with 50+ comments, while still providing useful reviews to authors when only a handful of lint issues slip by. * Code moved to `bots/` directory for ease of discovery and co-location with pull-bot. * Added `yarn lint-ci` command. This seems like the right choice: it's running `yarn lint` and other linters, and it is only intended to run on CI. * It's still possible to run `yarn lint-ci` locally, though the script will stop short of posting a review to GitHub unless the necessary envvars are provided. * Added `yarn shellcheck` command. This can be run locally, though it requires `shellcheck` to be installed. * Outside of this PR, I added instructions on using shellcheck to https://github.com/facebook/react-native/wiki/Development-Dependencies * Updated Circle CI config to use these new commands, and streamlined the `analyze_pr` step. * Documented analysis-bot in `bots/README.md`. ### pull-bot * Bumped `danger-js` dependency. No breaking changes found in this minor bump from what I can tell. * Documented pull-bot in `bots/README.md`. ### misc * PR template: don't use jargon. ## Changelog [Internal] [Changed] - GitHub Bots cleanup Pull Request resolved: https://github.com/facebook/react-native/pull/24923 Differential Revision: D15399744 Pulled By: hramos fbshipit-source-id: 32632e775f8554424072270e3f98542de84bfb8c
2019-05-22 05:35:40 +03:00
} else if (comments.length > 5) {
// Avoid noisy reviews and rely solely on the body of the review.
comments = [];
}
const event = 'REQUEST_CHANGES';
const opts = {
owner,
repo,
pull_number,
commit_id,
body,
event,
comments,
};
await octokit.pulls.createReview(opts);
} else {
if (comments.length === 0) {
console.log('No issues found.');
return;
}
if (process.env.CIRCLE_CI) {
console.error(
'Code analysis found issues, but the review cannot be posted to GitHub without an access token.',
);
process.exit(1);
}
let results = body + '\n';
comments.forEach(comment => {
results +=
comment.path + ':' + comment.position + ': ' + comment.body + '\n';
});
console.log(results);
}
}
async function main(messages, owner, repo, pull_number) {
2015-12-10 02:29:00 +03:00
// No message, we don't need to do anything :)
if (Object.keys(messages).length === 0) {
return;
}
if (!process.env.GITHUB_TOKEN) {
console.log(
Bots cleanup, avoid leaving inline reviews when N>5 (#24923) Summary: This PR cleans up some of our GitHub bots. The overall goal is to make the contribution process just a tad nicer. ### analysis-bot * The bot will continue leaving GitHub Reviews when it finds lint issues, but will abstain from leaving inline comments if they would exceed 5 in number. * The review comment left by the bot has instructions on how to reproduce the lint issues locally. This will educate PR authors on how to run lint and fix the issues without unnecessarily spamming the PR with 50+ comments, while still providing useful reviews to authors when only a handful of lint issues slip by. * Code moved to `bots/` directory for ease of discovery and co-location with pull-bot. * Added `yarn lint-ci` command. This seems like the right choice: it's running `yarn lint` and other linters, and it is only intended to run on CI. * It's still possible to run `yarn lint-ci` locally, though the script will stop short of posting a review to GitHub unless the necessary envvars are provided. * Added `yarn shellcheck` command. This can be run locally, though it requires `shellcheck` to be installed. * Outside of this PR, I added instructions on using shellcheck to https://github.com/facebook/react-native/wiki/Development-Dependencies * Updated Circle CI config to use these new commands, and streamlined the `analyze_pr` step. * Documented analysis-bot in `bots/README.md`. ### pull-bot * Bumped `danger-js` dependency. No breaking changes found in this minor bump from what I can tell. * Documented pull-bot in `bots/README.md`. ### misc * PR template: don't use jargon. ## Changelog [Internal] [Changed] - GitHub Bots cleanup Pull Request resolved: https://github.com/facebook/react-native/pull/24923 Differential Revision: D15399744 Pulled By: hramos fbshipit-source-id: 32632e775f8554424072270e3f98542de84bfb8c
2019-05-22 05:35:40 +03:00
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. Review feedback with code analysis results will not be provided on GitHub without a valid token.',
);
}
// https://octokit.github.io/rest.js/
const {Octokit} = require('@octokit/rest');
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
userAgent: 'react-native-code-analysis-bot',
});
const opts = {
owner,
repo,
pull_number,
};
const {data: pull} = await octokit.pulls.get(opts);
const {data: files} = await octokit.pulls.listFiles(opts);
const comments = [];
const convertersUsed = [];
files
.filter(file => messages[file.filename])
.forEach(file => {
// github api sometimes does not return a patch on large commits
if (!file.patch) {
return;
}
const lineMap = getLineMapFromPatch(file.patch);
messages[file.filename].forEach(message => {
if (lineMap[message.line]) {
const comment = {
path: file.filename,
position: lineMap[message.line],
body: message.message,
};
convertersUsed.push(message.converter);
comments.push(comment);
}
}); // forEach
}); // filter
let body = '**Code analysis results:**\n\n';
const uniqueconvertersUsed = [...new Set(convertersUsed)];
uniqueconvertersUsed.forEach(converter => {
body += '* ' + converterSummary[converter] + '\n';
});
await sendReview(
octokit,
owner,
repo,
pull_number,
pull.head.sha,
body,
comments,
);
2015-12-10 02:29:00 +03:00
}
let content = '';
2015-12-10 02:29:00 +03:00
process.stdin.resume();
process.stdin.on('data', function (buf) {
content += buf.toString();
});
process.stdin.on('end', function () {
let messages = {};
2015-12-10 02:29:00 +03:00
// Since we send a few http requests to setup the process, we don't want
// to run this file one time per code analysis tool. Instead, we write all
// the results in the same stdin stream.
// The format of this stream is
//
// name-of-the-converter
// {"json":"payload"}
// name-of-the-other-converter
// {"other": ["json", "payload"]}
//
// In order to generate such stream, here is a sample bash command:
//
// cat <(echo eslint; npm run lint --silent -- --format=json; echo flow; flow --json) | node code-analysis-bot.js
const lines = content.trim().split('\n');
for (let i = 0; i < Math.ceil(lines.length / 2); ++i) {
const converter = converters[lines[i * 2]];
2015-12-10 02:29:00 +03:00
if (!converter) {
throw new Error('Unknown converter ' + lines[i * 2]);
}
let json;
2015-12-10 02:29:00 +03:00
try {
json = JSON.parse(lines[i * 2 + 1]);
} catch (e) {}
converter(messages, json);
}
// The paths are returned in absolute from code analysis tools but github works
// on paths relative from the root of the project. Doing the normalization here.
const pwd = path.resolve('.');
for (let absolutePath in messages) {
const relativePath = path.relative(pwd, absolutePath);
2015-12-10 02:29:00 +03:00
if (relativePath === absolutePath) {
continue;
}
messages[relativePath] = messages[absolutePath];
delete messages[absolutePath];
}
const owner = process.env.GITHUB_OWNER;
const repo = process.env.GITHUB_REPO;
if (!process.env.GITHUB_PR_NUMBER) {
Bots cleanup, avoid leaving inline reviews when N>5 (#24923) Summary: This PR cleans up some of our GitHub bots. The overall goal is to make the contribution process just a tad nicer. ### analysis-bot * The bot will continue leaving GitHub Reviews when it finds lint issues, but will abstain from leaving inline comments if they would exceed 5 in number. * The review comment left by the bot has instructions on how to reproduce the lint issues locally. This will educate PR authors on how to run lint and fix the issues without unnecessarily spamming the PR with 50+ comments, while still providing useful reviews to authors when only a handful of lint issues slip by. * Code moved to `bots/` directory for ease of discovery and co-location with pull-bot. * Added `yarn lint-ci` command. This seems like the right choice: it's running `yarn lint` and other linters, and it is only intended to run on CI. * It's still possible to run `yarn lint-ci` locally, though the script will stop short of posting a review to GitHub unless the necessary envvars are provided. * Added `yarn shellcheck` command. This can be run locally, though it requires `shellcheck` to be installed. * Outside of this PR, I added instructions on using shellcheck to https://github.com/facebook/react-native/wiki/Development-Dependencies * Updated Circle CI config to use these new commands, and streamlined the `analyze_pr` step. * Documented analysis-bot in `bots/README.md`. ### pull-bot * Bumped `danger-js` dependency. No breaking changes found in this minor bump from what I can tell. * Documented pull-bot in `bots/README.md`. ### misc * PR template: don't use jargon. ## Changelog [Internal] [Changed] - GitHub Bots cleanup Pull Request resolved: https://github.com/facebook/react-native/pull/24923 Differential Revision: D15399744 Pulled By: hramos fbshipit-source-id: 32632e775f8554424072270e3f98542de84bfb8c
2019-05-22 05:35:40 +03:00
console.error(
'Missing GITHUB_PR_NUMBER. Example: 4687. Review feedback with code analysis results cannot be provided on GitHub without a valid pull request number.',
);
// for master branch, don't throw an error
process.exit(0);
}
const number = process.env.GITHUB_PR_NUMBER;
2015-12-10 02:29:00 +03:00
(async () => {
await main(messages, owner, repo, number);
})();
2015-12-10 02:29:00 +03:00
});