From ded015cc254167a666dc874b81fd74c915d5e918 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 4 Feb 2021 17:31:41 +0100 Subject: [PATCH 1/4] Update GitHub.js --- src/GitHub.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GitHub.js b/src/GitHub.js index b556985..bc66e1a 100644 --- a/src/GitHub.js +++ b/src/GitHub.js @@ -34,10 +34,11 @@ export default class GitHubClient { * @param {string} repo * @param {number} number * @param {number} id + * @param {number} per_page * @returns {Promise<{data:Object}>} */ - getComments({ owner, repo, number, id }) { - return this.github.pullRequests.getComments({ owner, repo, number, id }); + getComments({ owner, repo, number, id, per_page }) { + return this.github.pullRequests.getComments({ owner, repo, number, id, per_page }); } _createRepoString({ owner, repo }) { From 643ad3f24d45734dd738f3e102175688f6b1183e Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 4 Feb 2021 17:32:27 +0100 Subject: [PATCH 2/4] Update cli.js --- src/cli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli.js b/src/cli.js index 4166ba4..88d053d 100644 --- a/src/cli.js +++ b/src/cli.js @@ -41,7 +41,8 @@ module.exports = function(commitSha, flag) { owner: owner, repo: repo, id: item.id, - number: item.number + number: item.number, + per_page: 100 }); }) .then(response => { From 3e2337acd61e5700ba0855cf31cd5ebd63a97031 Mon Sep 17 00:00:00 2001 From: vbouchet Date: Fri, 5 Feb 2021 15:51:48 +0100 Subject: [PATCH 3/4] Update to use octokit. --- package.json | 4 ++-- src/GitHub.js | 47 ----------------------------------------------- src/Octokit.js | 35 +++++++++++++++++++++++++++++++++++ src/cli.js | 33 ++++++++++----------------------- src/formatter.js | 8 ++++---- 5 files changed, 51 insertions(+), 76 deletions(-) delete mode 100644 src/GitHub.js create mode 100644 src/Octokit.js diff --git a/package.json b/package.json index b79a077..97df6f4 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -46,7 +46,7 @@ "prettier": "^1.10.2" }, "dependencies": { - "github": "^9.1.0", + "@octokit/rest": "^18.1.0", "meow": "^3.7.0" }, "prettier": { diff --git a/src/GitHub.js b/src/GitHub.js deleted file mode 100644 index bc66e1a..0000000 --- a/src/GitHub.js +++ /dev/null @@ -1,47 +0,0 @@ -// MIT © 2017 azu -"use strict"; -const GitHubApi = require("github"); -export default class GitHubClient { - constructor(token) { - this.github = new GitHubApi({ - debug: false, - protocol: "https", - followRedirects: false, // default: true; there's currently an issue with non-get redirects, so allow ability to disable follow-redirects - timeout: 5000 - }); - this.github.authenticate({ - type: "oauth", - token: token - }); - } - - /** - * - * @param {string} owner - * @param {string} repo - * @param {string} commitSha - * @returns {Promise<{data:Object}>} - */ - getPR({ owner, repo, commitSha }) { - const repoPath = this._createRepoString({ owner, repo }); - return this.github.search.issues({ - q: `repo:${repoPath} type:pr ${commitSha}` - }); - } - - /** - * @param {string} owner - * @param {string} repo - * @param {number} number - * @param {number} id - * @param {number} per_page - * @returns {Promise<{data:Object}>} - */ - getComments({ owner, repo, number, id, per_page }) { - return this.github.pullRequests.getComments({ owner, repo, number, id, per_page }); - } - - _createRepoString({ owner, repo }) { - return `${owner}/${repo}`; - } -} diff --git a/src/Octokit.js b/src/Octokit.js new file mode 100644 index 0000000..5f5d668 --- /dev/null +++ b/src/Octokit.js @@ -0,0 +1,35 @@ +// MIT © 2021 vbouchet31 +"use strict"; +const { Octokit } = require("@octokit/rest"); + +export default class OctokitClient { + constructor(token) { + this.octokit = new Octokit({ + auth: token, + timeZone: "Europe/Amsterdam", + 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}`; + } +} diff --git a/src/cli.js b/src/cli.js index 88d053d..71c9a51 100644 --- a/src/cli.js +++ b/src/cli.js @@ -2,12 +2,12 @@ "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, @@ -15,42 +15,29 @@ import { getFormatter } from "./formatter"; * 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, - per_page: 100 - }); + number: number }) .then(response => { return formatter(response, { owner, repo, projectRoot, - commitSha + number }); }); }; diff --git a/src/formatter.js b/src/formatter.js index d126e3c..6787cf3 100644 --- a/src/formatter.js +++ b/src/formatter.js @@ -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, " "); @@ -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} From 3a2fad5bea7a79113f863f3627122e1d374efec2 Mon Sep 17 00:00:00 2001 From: vbouchet Date: Sun, 7 Feb 2021 22:15:24 +0100 Subject: [PATCH 4/4] Remove the specific timezone. --- src/Octokit.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Octokit.js b/src/Octokit.js index 5f5d668..f301161 100644 --- a/src/Octokit.js +++ b/src/Octokit.js @@ -6,7 +6,6 @@ export default class OctokitClient { constructor(token) { this.octokit = new Octokit({ auth: token, - timeZone: "Europe/Amsterdam", baseUrl: "https://api.github.com", request: { timeout: 5000