From 1fe7b40a335fb7b85b2f2ac59b7d986c21e881bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Thu, 23 Aug 2018 20:23:32 -0700 Subject: [PATCH] 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 --- .circleci/config.yml | 60 +++++++--------- scripts/circleci/analyze_code.sh | 5 +- .../circleci}/code-analysis-bot.js | 72 ++++++++++--------- 3 files changed, 66 insertions(+), 71 deletions(-) rename {bots => scripts/circleci}/code-analysis-bot.js (77%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 67ebb9a4f..f30107e86 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -541,46 +541,45 @@ 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 - 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: 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 + 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 echo "Skipping code analysis." fi 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 # Only works on stable branches when a properly tagged commit is pushed publish_npm_package: @@ -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 diff --git a/scripts/circleci/analyze_code.sh b/scripts/circleci/analyze_code.sh index 9569a4fa8..971f6cb15 100755 --- a/scripts/circleci/analyze_code.sh +++ b/scripts/circleci/analyze_code.sh @@ -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 - diff --git a/bots/code-analysis-bot.js b/scripts/circleci/code-analysis-bot.js similarity index 77% rename from bots/code-analysis-bot.js rename to scripts/circleci/code-analysis-bot.js index d39120e53..8c47b9e0d 100644 --- a/bots/code-analysis-bot.js +++ b/scripts/circleci/code-analysis-bot.js @@ -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) { @@ -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'); +const octokit = require('@octokit/rest')(); + var path = require('path'); -var github = new GitHubApi({ - version: '3.0.0', -}); - -github.authenticate({ +octokit.authenticate({ type: 'oauth', token: process.env.GITHUB_TOKEN, }); @@ -98,20 +95,21 @@ 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 @@ -119,7 +117,7 @@ function getFilesFromCommit(user, repo, sha, callback) { return; } - callback(res.files); + callback(res.data.files); }); } @@ -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, @@ -168,23 +166,23 @@ 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) { @@ -192,7 +190,15 @@ function main(messages, user, repo, number) { } 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, + ); }); }); }); @@ -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); });