Skip to content

Commit

Permalink
Fix eslint code analysis bot (facebook#20822)
Browse files Browse the repository at this point in the history
Summary:
The eslint bot has not been working since the migration to Circle 2.0.
Pull Request resolved: facebook#20822

Differential Revision: D9492680

Pulled By: hramos

fbshipit-source-id: 7f2f9ac125b6cab1750902c485a6d27d6c3cf302
  • Loading branch information
hramos authored and facebook-github-bot committed Aug 24, 2018
1 parent e4621f4 commit 1fe7b40
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 66 deletions.
50 changes: 20 additions & 30 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -541,45 +541,44 @@ jobs:
# Issues will be posted to the PR itself via GitHub bots.
# This workflow should only fail if the bots fail to run.
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:
- attach_workspace:
at: ~/react-native
- checkout
- run: *setup-artifacts

- restore-cache: *restore-cache-analysis
- restore-cache: *restore-yarn-cache
- run: *yarn

- run:
name: Install Additional Dependencies
name: Analyze Code
command: |
# GITHUB_TOKEN=eslint-bot public_repo access token
if [ -n "$CIRCLE_PR_NUMBER" ]; then
yarn add [email protected]
cd bots
yarn install --non-interactive --cache-folder ~/.cache/yarn
echo -e "\\x1B[36mInstalling additional dependencies\\x1B[0m"; yarn add @octokit/[email protected]
echo -e "\\x1B[36mAnalyzing code\\x1B[0m"; GITHUB_TOKEN="af6ef0d15709bc91d""06a6217a5a826a226fb57b7" ./scripts/circleci/analyze_code.sh
else
echo "Skipping dependency installation."
echo "Skipping code analysis."
fi
- save-cache: *save-cache-analysis
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 && DANGER_GITHUB_API_TOKEN="80aa64c50f38a267e9ba""575d41d528f9c234edb8" yarn danger
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
- run:
name: Analyze Code
command: |
# GITHUB_TOKEN=eslint-bot public_repo access token
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
else
echo "Skipping code analysis."
fi
when: always
- save-cache: *save-cache-analysis

# Publishes new version onto npm
# Only works on stable branches when a properly tagged commit is pushed
Expand Down Expand Up @@ -686,15 +685,6 @@ workflows:
- test_objc
- test_android

# Only runs on PRs
analyze:
jobs:
# Checkout repo and run Yarn
- checkout_code:
filters: *filter-ignore-master-stable

# Run code checks
- analyze_pr:
filters: *filter-ignore-master-stable
requires:
- checkout_code
5 changes: 2 additions & 3 deletions scripts/circleci/analyze_code.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#!/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
STATUS=$?
if [ $STATUS == 0 ]; then
echo "Code analyzed successfully"
else
echo "Code analyzis failed, error status $STATUS"
echo "Code analysis failed, error status $STATUS"
fi

72 changes: 39 additions & 33 deletions bots/code-analysis-bot.js → scripts/circleci/code-analysis-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

'use strict';

if (!process.env.CI_USER) {
console.error('Missing CI_USER. Example: facebook');
if (!process.env.CIRCLE_PROJECT_USERNAME) {
console.error('Missing CIRCLE_PROJECT_USERNAME. Example: facebook');
process.exit(1);
}
if (!process.env.CI_REPO) {
console.error('Missing CI_REPO. Example: react-native');
if (!process.env.CIRCLE_PROJECT_REPONAME) {
console.error('Missing CIRCLE_PROJECT_REPONAME. Example: react-native');
process.exit(1);
}
if (!process.env.GITHUB_TOKEN) {
Expand All @@ -23,20 +23,17 @@ if (!process.env.GITHUB_TOKEN) {
);
process.exit(1);
}
if (!process.env.PULL_REQUEST_NUMBER) {
console.error('Missing PULL_REQUEST_NUMBER. Example: 4687');
// for master branch don't throw and error
if (!process.env.CIRCLE_PR_NUMBER) {
console.error('Missing CIRCLE_PR_NUMBER. Example: 4687');
// for master branch, don't throw an error
process.exit(0);
}

var GitHubApi = require('github');
var path = require('path');
const octokit = require('@octokit/rest')();

var github = new GitHubApi({
version: '3.0.0',
});
var path = require('path');

github.authenticate({
octokit.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN,
});
Expand Down Expand Up @@ -98,28 +95,29 @@ var converters = {
},
};

function getShaFromPullRequest(user, repo, number, callback) {
github.pullRequests.get({user, repo, number}, (error, res) => {
function getShaFromPullRequest(owner, repo, number, callback) {
octokit.pullRequests.get({owner, repo, number}, (error, res) => {
if (error) {
console.log(error);
console.error(error);
return;
}
callback(res.head.sha);

callback(res.data.head.sha);
});
}

function getFilesFromCommit(user, repo, sha, callback) {
github.repos.getCommit({user, repo, sha}, (error, res) => {
function getFilesFromCommit(owner, repo, sha, callback) {
octokit.repos.getCommit({owner, repo, sha}, (error, res) => {
if (error) {
console.log(error);
console.error(error);
return;
}
// A merge commit should not have any new changes to report
if (res.parents && res.parents.length > 1) {
return;
}

callback(res.files);
callback(res.data.files);
});
}

Expand Down Expand Up @@ -152,14 +150,14 @@ function getLineMapFromPatch(patchString) {
return lineMap;
}

function sendComment(user, repo, number, sha, filename, lineMap, message) {
function sendComment(owner, repo, number, sha, filename, lineMap, message) {
if (!lineMap[message.line]) {
// Do not send messages on lines that did not change
return;
}

var opts = {
user,
owner,
repo,
number,
sha,
Expand All @@ -168,31 +166,39 @@ function sendComment(user, repo, number, sha, filename, lineMap, message) {
body: message.message,
position: lineMap[message.line],
};
github.pullRequests.createComment(opts, function(error, res) {
octokit.pullRequests.createComment(opts, function(error, res) {
if (error) {
console.log(error);
console.error(error);
return;
}
});
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 :)
if (Object.keys(messages).length === 0) {
return;
}

getShaFromPullRequest(user, repo, number, sha => {
getFilesFromCommit(user, repo, sha, files => {
getShaFromPullRequest(owner, repo, number, sha => {
getFilesFromCommit(owner, repo, sha, files => {
files.filter(file => messages[file.filename]).forEach(file => {
// github api sometimes does not return a patch on large commits
if (!file.patch) {
return;
}
var lineMap = getLineMapFromPatch(file.patch);
messages[file.filename].forEach(message => {
sendComment(user, repo, number, sha, file.filename, lineMap, message);
sendComment(
owner,
repo,
number,
sha,
file.filename,
lineMap,
message,
);
});
});
});
Expand Down Expand Up @@ -247,10 +253,10 @@ process.stdin.on('end', function() {
delete messages[absolutePath];
}

var user = process.env.CI_USER;
var repo = process.env.CI_REPO;
var number = process.env.PULL_REQUEST_NUMBER;
var owner = process.env.CIRCLE_PROJECT_USERNAME;
var repo = process.env.CIRCLE_PROJECT_REPONAME;
var number = process.env.CIRCLE_PR_NUMBER;

// intentional lint warning to make sure that the bot is working :)
main(messages, user, repo, number);
main(messages, owner, repo, number);
});

0 comments on commit 1fe7b40

Please sign in to comment.