fix: fetch private pull request diffs through authenticated API

This commit is contained in:
tdurieux
2026-09-06 09:19:28 +02:00
parent 4aecc1d92f
commit f312105ec3
2 changed files with 21 additions and 14 deletions
+4 -14
View File
@@ -7,7 +7,6 @@ import AnonymousError from "./AnonymousError";
import { IAnonymizedPullRequestDocument } from "./model/anonymizedPullRequests/anonymizedPullRequests.types"; import { IAnonymizedPullRequestDocument } from "./model/anonymizedPullRequests/anonymizedPullRequests.types";
import AnonymizedPullRequestModel from "./model/anonymizedPullRequests/anonymizedPullRequests.model"; import AnonymizedPullRequestModel from "./model/anonymizedPullRequests/anonymizedPullRequests.model";
import config from "../config"; import config from "../config";
import got, { HTTPError } from "got";
import { octokit } from "./GitHubUtils"; import { octokit } from "./GitHubUtils";
import { ContentAnonimizer } from "./anonymize-utils"; import { ContentAnonimizer } from "./anonymize-utils";
import { createLogger } from "./logger"; import { createLogger } from "./logger";
@@ -103,24 +102,15 @@ export default class PullRequest {
throw err; throw err;
}); });
const diffPromise = got( const diffPromise = oct.rest.pulls.get({
`https://github.com/${owner}/${repo}/pull/${pull_number}.diff` owner, repo, pull_number,
).catch((err) => { mediaType: { format: "diff" },
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 [comments, diff] = await Promise.all([commentsPromise, diffPromise]); const [comments, diff] = await Promise.all([commentsPromise, diffPromise]);
this._model.pullRequest = { this._model.pullRequest = {
diff: diff.body, diff: diff.data as unknown as string,
title: prInfo.data.title, title: prInfo.data.title,
body: prInfo.data.body || "", body: prInfo.data.body || "",
creationDate: new Date(prInfo.data.created_at), creationDate: new Date(prInfo.data.created_at),
+17
View File
@@ -175,6 +175,23 @@ describe("production regressions", function () {
await gist.download(); await gist.download();
expect(gist.toJSON().gist.files[0].content).to.equal("complete gist content"); 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 () { it("checks GitHub authorization before returning shared cached metadata", async function () {
const CachedRepoModel = require("../src/core/model/repositories/repositories.model").default; const CachedRepoModel = require("../src/core/model/repositories/repositories.model").default;
stub(db, "isConnected", true); stub(db, "isConnected", true);