Skip to content

Commit

Permalink
feat: suggestion-bot
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Jul 21, 2020
0 parents commit bf10551
Show file tree
Hide file tree
Showing 12 changed files with 5,260 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{js,json,sh,ts,yml}]
indent_size = 2
max_line_length = 80
guidelines = 80
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"es6": true,
"node": true
},
"extends": ["eslint:recommended"],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build
on:
push:
branches:
- master
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 12
- name: Cache /node_modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-${{ hashFiles('yarn.lock') }}
- name: Install
run: yarn
- name: Lint
run: yarn lint
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tgz
node_modules/
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.tgz
.editorconfig
.eslintrc.json
.gitattributes
.github/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Tommy Nguyen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# suggestion-bot

`suggestion-bot` submits code reviews with suggestions based on your diffs.
11 changes: 11 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

//
// Copyright (c) Tommy Nguyen
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//

const { [2]: diff } = process.argv;
require("./index")(diff);
103 changes: 103 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// Copyright (c) Tommy Nguyen
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//

/**
* Returns the pull request number of the current build.
* @returns {number}
*/
function getPullRequestNumber() {
const fs = require("fs");
const e = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8"));
return e.pull_request.number;
}

/**
* Creates a comment that can be submitted to GitHub.
* @param {string} file
* @param {{
changes: ({ type: "add" | "del" | "normal"; content: string; })[];
oldStart: number;
oldLines: number;
}} chunk
* @returns {{
path: string;
line: number;
side: "LEFT" | "RIGHT";
start_line?: number;
start_side?: "LEFT" | "RIGHT";
body: string;
}}
*/
function makeComment(file, { changes, oldStart, oldLines }) {
const context =
changes.reduce(
(count, line) => (line.type === "normal" ? count + 1 : count),
0
) / 2;
const line = oldStart + oldLines - context - 1;
const startLine = oldStart + context;
return {
path: file,
line,
side: "RIGHT",
...(startLine !== line
? {
start_line: startLine,
start_side: "RIGHT",
}
: undefined),
body: [
"```suggestion",
changes
.filter((line) => line.type === "add")
.map((line) => line.content.slice(1))
.join("\n"),
"```",
"",
].join("\n"),
};
}

/**
* Submits a code review with suggestions with specified diff.
* @param {string} diff
*/
function suggest(diff) {
const parse = require("parse-diff");
const files = parse(diff);
if (files.length <= 0) {
return process.exit(0);
}

const { GITHUB_REPOSITORY, GITHUB_TOKEN } = process.env;

const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({ auth: GITHUB_TOKEN });

const [owner, repo] = GITHUB_REPOSITORY.split("/");
const review = {
accept: "application/vnd.github.comfort-fade-preview+json",
owner,
repo,
pull_number: getPullRequestNumber(),
event: "COMMENT",
comments: files.reduce((comments, file) => {
const { chunks, to } = file;
return chunks.reduce((comments, chunk) => {
comments.push(makeComment(to, chunk));
return comments;
}, comments);
}, []),
};
octokit.pulls.createReview(review).catch((e) => {
console.error(e);
console.dir(review, undefined, { depth: null });
return process.exit(1);
});
}

module.exports = suggest;
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "suggestion-bot",
"license": "MIT",
"author": {
"name": "Tommy Nguyen",
"email": "tn0502@gmail.com"
},
"main": "index.js",
"bin": "cli.js",
"repository": {
"type": "git",
"url": "https://github.com/tido64/suggestion-bot.git"
},
"version": "0.0.1-dev",
"description": "suggestion-bot submits code reviews with suggestions based on your diffs",
"keywords": [
"code review",
"diff",
"github",
"pull request",
"review",
"suggestion"
],
"homepage": "https://github.com/tido64/suggestion-bot",
"scripts": {
"format": "prettier --write $(git ls-files '*.js' '*.yml')",
"lint": "eslint $(git ls-files '*.js')"
},
"dependencies": {
"@octokit/rest": "^17.0.0",
"parse-diff": "^0.7.0"
},
"devDependencies": {
"eslint": "^7.5.0",
"eslint-plugin-prettier": "^3.1.4",
"prettier": "2.0.5",
"semantic-release": "^17.0.0"
},
"release": {
"tagFormat": "${version}"
}
}
Loading

0 comments on commit bf10551

Please sign in to comment.