Skip to content

Commit

Permalink
Merge pull request #10 from vbouchet31/master
Browse files Browse the repository at this point in the history
  • Loading branch information
vbouchet31 authored May 6, 2021
2 parents 9387645 + 3a2fad5 commit 78aafc3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 74 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"name": "get-github-pr-review-comments",
"version": "1.2.2",
"description": "Get PR review comments.",
"main": "lib/GitHub.js",
"main": "lib/Octokit.js",
"bin": {
"get-github-pr-review-comments": "bin/cmd.js"
},
Expand Down Expand Up @@ -46,7 +46,7 @@
"prettier": "^1.10.2"
},
"dependencies": {
"github": "^9.1.0",
"@octokit/rest": "^18.1.0",
"meow": "^3.7.0"
},
"prettier": {
Expand Down
46 changes: 0 additions & 46 deletions src/GitHub.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/Octokit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// MIT © 2021 vbouchet31
"use strict";
const { Octokit } = require("@octokit/rest");

export default class OctokitClient {
constructor(token) {
this.octokit = new Octokit({
auth: token,
baseUrl: "https://api.github.com",
request: {
timeout: 5000
}
});
}

/**
*
* @param {string} owner
* @param {string} repo
* @param {string} number
* @returns {Promise<{data:Object}>}
*/
getComments({ owner, repo, number }) {
return this.octokit.paginate("GET /repos/{owner}/{repo}/pulls/{number}/comments", {
owner: owner,
repo: repo,
number: number
});
}

_createRepoString({ owner, repo }) {
return `${owner}/${repo}`;
}
}
32 changes: 10 additions & 22 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,42 @@
"use strict";
const assert = require("assert");
const GH_TOKEN = process.env.GH_TOKEN;
import GitHubAPI from "./GitHub";
import OctokitAPI from "./Octokit";
import { getFormatter } from "./formatter";

/**
*
* @param {string} commitSha
* @param {string} number
* @param {{
* token: string,
* projectRoot: string,
* repo: string,
* format: string
* }} flag
*/
module.exports = function(commitSha, flag) {
module.exports = function(number, flag) {
const [owner, repo] = flag.repo.split("/");
assert(owner && repo, "--repo owner/repo");
const ghToken = GH_TOKEN || flag.token;
assert(ghToken, "--token xxx or export GH_TOKEN=xxx");
const token = GH_TOKEN || flag.token;
assert(token, "--token xxx or export GH_TOKEN=xxx");
const projectRoot = flag.projectRoot;
assert(projectRoot, "--projectRoot /path/to/dir");
const outFormat = flag.format;

const github = new GitHubAPI(ghToken);
const octokit = new OctokitAPI(token);
const formatter = getFormatter(outFormat);
return github
.getPR({
return octokit
.getComments({
owner: owner,
repo: repo,
commitSha: commitSha
})
.then(response => {
if (response.data.total_count === 0) {
return;
}
const item = response.data.items[0];
return github.getComments({
owner: owner,
repo: repo,
id: item.id,
number: item.number
});
number: number
})
.then(response => {
return formatter(response, {
owner,
repo,
projectRoot,
commitSha
number
});
});
};
8 changes: 4 additions & 4 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const rawFormatter = (comment, options) => {
* @see https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
*/
const jsonFormatter = (comments, options) => {
if (!comments || (comments && !Array.isArray(comments.data))) {
if (!comments || (comments && !Array.isArray(comments))) {
return "{}"; // no comments
}
const formatData = comments.data.map(data => {
const formatData = comments.map(data => {
return rawFormatter(data, options);
});
return JSON.stringify(formatData, null, " ");
Expand All @@ -43,10 +43,10 @@ const jsonFormatter = (comments, options) => {
* @see https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
*/
const defaultFormatter = (comments, options) => {
if (!comments || (comments && !Array.isArray(comments.data))) {
if (!comments || (comments && !Array.isArray(comments))) {
return "No comments";
}
const formatData = comments.data.map(data => {
const formatData = comments.map(data => {
const js = rawFormatter(data, options);
return `@ ${js.file_path}:${js.line_number}:1
${js.review_url}
Expand Down

0 comments on commit 78aafc3

Please sign in to comment.