diff --git a/src/core/PullRequest.ts b/src/core/PullRequest.ts index cf4c518..a5f9aed 100644 --- a/src/core/PullRequest.ts +++ b/src/core/PullRequest.ts @@ -7,7 +7,6 @@ import AnonymousError from "./AnonymousError"; import { IAnonymizedPullRequestDocument } from "./model/anonymizedPullRequests/anonymizedPullRequests.types"; import AnonymizedPullRequestModel from "./model/anonymizedPullRequests/anonymizedPullRequests.model"; import config from "../config"; -import got, { HTTPError } from "got"; import { octokit } from "./GitHubUtils"; import { ContentAnonimizer } from "./anonymize-utils"; import { createLogger } from "./logger"; @@ -103,24 +102,15 @@ export default class PullRequest { throw err; }); - const diffPromise = got( - `https://github.com/${owner}/${repo}/pull/${pull_number}.diff` - ).catch((err) => { - if (err instanceof HTTPError && err.response.statusCode === 404) { - logger.warn("PR diff 404, continuing without it", { - code: "pr_diff_not_found", - httpStatus: 404, - pr: `${owner}/${repo}#${pull_number}`, - }); - return { body: "" }; - } - throw err; + const diffPromise = oct.rest.pulls.get({ + owner, repo, pull_number, + mediaType: { format: "diff" }, }); const [comments, diff] = await Promise.all([commentsPromise, diffPromise]); this._model.pullRequest = { - diff: diff.body, + diff: diff.data as unknown as string, title: prInfo.data.title, body: prInfo.data.body || "", creationDate: new Date(prInfo.data.created_at), diff --git a/test/production-regressions.test.js b/test/production-regressions.test.js index ac056c7..1132f76 100644 --- a/test/production-regressions.test.js +++ b/test/production-regressions.test.js @@ -175,6 +175,23 @@ describe("production regressions", function () { await gist.download(); expect(gist.toJSON().gist.files[0].content).to.equal("complete gist content"); }); + + it("retrieves private PR diffs with the authenticated API", async function () { + const PR = require("../src/core/PullRequest").default; + const pr = new PR(new (require("../src/core/model/anonymizedPullRequests/anonymizedPullRequests.model").default)({ source: { repositoryFullName: "owner/repo", pullRequestId: 1 } })); + pr.getToken = async () => "private-token"; + let diffRequested = false; + stub(gh, "octokit", token => { + expect(token).to.equal("private-token"); + return { rest: { pulls: { get: async options => { + if (options.mediaType?.format === "diff") { diffRequested = true; return { data: "private diff" }; } + return { data: { title: "PR", base: { repo: { full_name: "owner/repo" } }, head: {} } }; + } } }, paginate: async () => [] }; + }); + await pr.download(); + expect(diffRequested).to.equal(true); + expect(pr.model.pullRequest.diff).to.equal("private diff"); + }); it("checks GitHub authorization before returning shared cached metadata", async function () { const CachedRepoModel = require("../src/core/model/repositories/repositories.model").default; stub(db, "isConnected", true);