Fix eslint code analysis bot (#20822)
Summary: The eslint bot has not been working since the migration to Circle 2.0. Pull Request resolved: https://github.com/facebook/react-native/pull/20822 Differential Revision: D9492680 Pulled By: hramos fbshipit-source-id: 7f2f9ac125b6cab1750902c485a6d27d6c3cf302
This commit is contained in:
parent
e4621f4ce1
commit
1fe7b40a33
|
@ -541,46 +541,45 @@ jobs:
|
||||||
# Issues will be posted to the PR itself via GitHub bots.
|
# Issues will be posted to the PR itself via GitHub bots.
|
||||||
# This workflow should only fail if the bots fail to run.
|
# This workflow should only fail if the bots fail to run.
|
||||||
analyze_pr:
|
analyze_pr:
|
||||||
<<: *js_defaults
|
<<: *defaults
|
||||||
|
docker:
|
||||||
|
- image: circleci/node:10
|
||||||
|
environment:
|
||||||
|
- PATH: "/opt/yarn/yarn-v1.5.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||||
steps:
|
steps:
|
||||||
- attach_workspace:
|
- checkout
|
||||||
at: ~/react-native
|
- run: *setup-artifacts
|
||||||
|
|
||||||
- restore-cache: *restore-cache-analysis
|
- restore-cache: *restore-yarn-cache
|
||||||
- run: *yarn
|
- run: *yarn
|
||||||
- run:
|
|
||||||
name: Install Additional Dependencies
|
|
||||||
command: |
|
|
||||||
if [ -n "$CIRCLE_PR_NUMBER" ]; then
|
|
||||||
yarn add github@0.2.4
|
|
||||||
cd bots
|
|
||||||
yarn install --non-interactive --cache-folder ~/.cache/yarn
|
|
||||||
else
|
|
||||||
echo "Skipping dependency installation."
|
|
||||||
fi
|
|
||||||
- save-cache: *save-cache-analysis
|
|
||||||
|
|
||||||
- run:
|
|
||||||
name: Analyze Pull Request
|
|
||||||
command: |
|
|
||||||
# DANGER_GITHUB_API_TOKEN=React-Linter public_repo access token
|
|
||||||
if [ -n "$CIRCLE_PR_NUMBER" ]; then
|
|
||||||
cd bots && DANGER_GITHUB_API_TOKEN="80aa64c50f38a267e9ba""575d41d528f9c234edb8" yarn danger
|
|
||||||
else
|
|
||||||
echo "Skipping pull request analysis."
|
|
||||||
fi
|
|
||||||
when: always
|
|
||||||
- run:
|
- run:
|
||||||
name: Analyze Code
|
name: Analyze Code
|
||||||
command: |
|
command: |
|
||||||
# GITHUB_TOKEN=eslint-bot public_repo access token
|
# GITHUB_TOKEN=eslint-bot public_repo access token
|
||||||
if [ -n "$CIRCLE_PR_NUMBER" ]; then
|
if [ -n "$CIRCLE_PR_NUMBER" ]; then
|
||||||
GITHUB_TOKEN="af6ef0d15709bc91d""06a6217a5a826a226fb57b7" CI_USER=$CIRCLE_PROJECT_USERNAME CI_REPO=$CIRCLE_PROJECT_REPONAME PULL_REQUEST_NUMBER=$CIRCLE_PR_NUMBER scripts/circleci/analyze_code.sh
|
echo -e "\\x1B[36mInstalling additional dependencies\\x1B[0m"; yarn add @octokit/rest@15.10.0
|
||||||
|
echo -e "\\x1B[36mAnalyzing code\\x1B[0m"; GITHUB_TOKEN="af6ef0d15709bc91d""06a6217a5a826a226fb57b7" ./scripts/circleci/analyze_code.sh
|
||||||
else
|
else
|
||||||
echo "Skipping code analysis."
|
echo "Skipping code analysis."
|
||||||
fi
|
fi
|
||||||
when: always
|
when: always
|
||||||
|
|
||||||
|
- restore-cache: *restore-cache-analysis
|
||||||
|
- run:
|
||||||
|
name: Analyze Pull Request
|
||||||
|
command: |
|
||||||
|
# DANGER_GITHUB_API_TOKEN=React-Linter public_repo access token
|
||||||
|
if [ -n "$CIRCLE_PR_NUMBER" ]; then
|
||||||
|
cd bots
|
||||||
|
yarn install --non-interactive --cache-folder ~/.cache/yarn
|
||||||
|
DANGER_GITHUB_API_TOKEN="80aa64c50f38a267e9ba""575d41d528f9c234edb8" yarn danger
|
||||||
|
else
|
||||||
|
echo "Skipping pull request analysis."
|
||||||
|
fi
|
||||||
|
when: always
|
||||||
|
- save-cache: *save-cache-analysis
|
||||||
|
|
||||||
# Publishes new version onto npm
|
# Publishes new version onto npm
|
||||||
# Only works on stable branches when a properly tagged commit is pushed
|
# Only works on stable branches when a properly tagged commit is pushed
|
||||||
publish_npm_package:
|
publish_npm_package:
|
||||||
|
@ -686,15 +685,6 @@ workflows:
|
||||||
- test_objc
|
- test_objc
|
||||||
- test_android
|
- test_android
|
||||||
|
|
||||||
# Only runs on PRs
|
|
||||||
analyze:
|
|
||||||
jobs:
|
|
||||||
# Checkout repo and run Yarn
|
|
||||||
- checkout_code:
|
|
||||||
filters: *filter-ignore-master-stable
|
|
||||||
|
|
||||||
# Run code checks
|
# Run code checks
|
||||||
- analyze_pr:
|
- analyze_pr:
|
||||||
filters: *filter-ignore-master-stable
|
filters: *filter-ignore-master-stable
|
||||||
requires:
|
|
||||||
- checkout_code
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
cat <(echo eslint; yarn --silent lint --format=json; echo flow; yarn --silent flow check --json) | GITHUB_TOKEN=$GITHUB_TOKEN CI_USER=$CI_USER CI_REPO=$CI_REPO PULL_REQUEST_NUMBER=$PULL_REQUEST_NUMBER node bots/code-analysis-bot.js
|
cat <(echo eslint; npm run lint --silent -- --format=json; echo flow; npm run flow --silent -- check --json) | node scripts/circleci/code-analysis-bot.js
|
||||||
|
|
||||||
# check status
|
# check status
|
||||||
STATUS=$?
|
STATUS=$?
|
||||||
if [ $STATUS == 0 ]; then
|
if [ $STATUS == 0 ]; then
|
||||||
echo "Code analyzed successfully"
|
echo "Code analyzed successfully"
|
||||||
else
|
else
|
||||||
echo "Code analyzis failed, error status $STATUS"
|
echo "Code analysis failed, error status $STATUS"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,12 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
if (!process.env.CI_USER) {
|
if (!process.env.CIRCLE_PROJECT_USERNAME) {
|
||||||
console.error('Missing CI_USER. Example: facebook');
|
console.error('Missing CIRCLE_PROJECT_USERNAME. Example: facebook');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if (!process.env.CI_REPO) {
|
if (!process.env.CIRCLE_PROJECT_REPONAME) {
|
||||||
console.error('Missing CI_REPO. Example: react-native');
|
console.error('Missing CIRCLE_PROJECT_REPONAME. Example: react-native');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if (!process.env.GITHUB_TOKEN) {
|
if (!process.env.GITHUB_TOKEN) {
|
||||||
|
@ -23,20 +23,17 @@ if (!process.env.GITHUB_TOKEN) {
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
if (!process.env.PULL_REQUEST_NUMBER) {
|
if (!process.env.CIRCLE_PR_NUMBER) {
|
||||||
console.error('Missing PULL_REQUEST_NUMBER. Example: 4687');
|
console.error('Missing CIRCLE_PR_NUMBER. Example: 4687');
|
||||||
// for master branch don't throw and error
|
// for master branch, don't throw an error
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var GitHubApi = require('github');
|
const octokit = require('@octokit/rest')();
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var github = new GitHubApi({
|
octokit.authenticate({
|
||||||
version: '3.0.0',
|
|
||||||
});
|
|
||||||
|
|
||||||
github.authenticate({
|
|
||||||
type: 'oauth',
|
type: 'oauth',
|
||||||
token: process.env.GITHUB_TOKEN,
|
token: process.env.GITHUB_TOKEN,
|
||||||
});
|
});
|
||||||
|
@ -98,20 +95,21 @@ var converters = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function getShaFromPullRequest(user, repo, number, callback) {
|
function getShaFromPullRequest(owner, repo, number, callback) {
|
||||||
github.pullRequests.get({user, repo, number}, (error, res) => {
|
octokit.pullRequests.get({owner, repo, number}, (error, res) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(error);
|
console.error(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
callback(res.head.sha);
|
|
||||||
|
callback(res.data.head.sha);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilesFromCommit(user, repo, sha, callback) {
|
function getFilesFromCommit(owner, repo, sha, callback) {
|
||||||
github.repos.getCommit({user, repo, sha}, (error, res) => {
|
octokit.repos.getCommit({owner, repo, sha}, (error, res) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(error);
|
console.error(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// A merge commit should not have any new changes to report
|
// A merge commit should not have any new changes to report
|
||||||
|
@ -119,7 +117,7 @@ function getFilesFromCommit(user, repo, sha, callback) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(res.files);
|
callback(res.data.files);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,14 +150,14 @@ function getLineMapFromPatch(patchString) {
|
||||||
return lineMap;
|
return lineMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendComment(user, repo, number, sha, filename, lineMap, message) {
|
function sendComment(owner, repo, number, sha, filename, lineMap, message) {
|
||||||
if (!lineMap[message.line]) {
|
if (!lineMap[message.line]) {
|
||||||
// Do not send messages on lines that did not change
|
// Do not send messages on lines that did not change
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var opts = {
|
var opts = {
|
||||||
user,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
number,
|
number,
|
||||||
sha,
|
sha,
|
||||||
|
@ -168,23 +166,23 @@ function sendComment(user, repo, number, sha, filename, lineMap, message) {
|
||||||
body: message.message,
|
body: message.message,
|
||||||
position: lineMap[message.line],
|
position: lineMap[message.line],
|
||||||
};
|
};
|
||||||
github.pullRequests.createComment(opts, function(error, res) {
|
octokit.pullRequests.createComment(opts, function(error, res) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(error);
|
console.error(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log('Sending comment', opts);
|
console.log('Sending comment', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
function main(messages, user, repo, number) {
|
function main(messages, owner, repo, number) {
|
||||||
// No message, we don't need to do anything :)
|
// No message, we don't need to do anything :)
|
||||||
if (Object.keys(messages).length === 0) {
|
if (Object.keys(messages).length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
getShaFromPullRequest(user, repo, number, sha => {
|
getShaFromPullRequest(owner, repo, number, sha => {
|
||||||
getFilesFromCommit(user, repo, sha, files => {
|
getFilesFromCommit(owner, repo, sha, files => {
|
||||||
files.filter(file => messages[file.filename]).forEach(file => {
|
files.filter(file => messages[file.filename]).forEach(file => {
|
||||||
// github api sometimes does not return a patch on large commits
|
// github api sometimes does not return a patch on large commits
|
||||||
if (!file.patch) {
|
if (!file.patch) {
|
||||||
|
@ -192,7 +190,15 @@ function main(messages, user, repo, number) {
|
||||||
}
|
}
|
||||||
var lineMap = getLineMapFromPatch(file.patch);
|
var lineMap = getLineMapFromPatch(file.patch);
|
||||||
messages[file.filename].forEach(message => {
|
messages[file.filename].forEach(message => {
|
||||||
sendComment(user, repo, number, sha, file.filename, lineMap, message);
|
sendComment(
|
||||||
|
owner,
|
||||||
|
repo,
|
||||||
|
number,
|
||||||
|
sha,
|
||||||
|
file.filename,
|
||||||
|
lineMap,
|
||||||
|
message,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -247,10 +253,10 @@ process.stdin.on('end', function() {
|
||||||
delete messages[absolutePath];
|
delete messages[absolutePath];
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = process.env.CI_USER;
|
var owner = process.env.CIRCLE_PROJECT_USERNAME;
|
||||||
var repo = process.env.CI_REPO;
|
var repo = process.env.CIRCLE_PROJECT_REPONAME;
|
||||||
var number = process.env.PULL_REQUEST_NUMBER;
|
var number = process.env.CIRCLE_PR_NUMBER;
|
||||||
|
|
||||||
// intentional lint warning to make sure that the bot is working :)
|
// intentional lint warning to make sure that the bot is working :)
|
||||||
main(messages, user, repo, number);
|
main(messages, owner, repo, number);
|
||||||
});
|
});
|
Loading…
Reference in New Issue