Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,30 @@ describe('githubPrParser', () => {
expect(result.description).toBe('');
});

test('fetches diff using the pull request diff_url', async () => {
const mockRequest = vi.fn().mockResolvedValue({ data: '' });
const octokit = { request: mockRequest } as unknown as Octokit;
const pr = makePullRequest({ diff_url: 'https://github.com/my-org/my-repo/pull/7.diff' });
test('fetches diff using the GitHub pulls API with diff accept header', async () => {
const mockRequest = vi.fn().mockResolvedValue({ data: '' });
const octokit = { request: mockRequest } as unknown as Octokit;
const pr = makePullRequest({
owner: 'my-org',
repo: 'my-repo',
number: 7,
diff_url: 'https://github.com/my-org/my-repo/pull/7.diff',
});

await githubPrParser(octokit, pr);
await githubPrParser(octokit, pr);

expect(mockRequest).toHaveBeenCalledWith('https://github.com/my-org/my-repo/pull/7.diff');
});
expect(mockRequest).toHaveBeenCalledWith(
'GET /repos/{owner}/{repo}/pulls/{pull_number}',
{
owner: 'my-org',
repo: 'my-repo',
pull_number: 7,
headers: {
accept: 'application/vnd.github.diff',
},
}
);
});

test('returns empty file_diffs for an empty diff', async () => {
const octokit = makeMockOctokit('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ export const githubPrParser = async (octokit: Octokit, pullRequest: GitHubPullRe

let parsedDiff: parse.File[] = [];
try {
const diff = await octokit.request(pullRequest.diff_url);
parsedDiff = parse(diff.data);
} catch (error) {
logger.error("Error fetching diff: ", error);
throw error;
}
const { owner, name: repo } = pullRequest.base.repo;
const pullNumber = pullRequest.number;

const diff = await octokit.request("GET /repos/{owner}/{repo}/pulls/{pull_number}", {
owner: owner.login,
repo,
pull_number: pullNumber,
headers: {
accept: "application/vnd.github.diff",
},
});

parsedDiff = parse(diff.data as string);
} catch (error) {
logger.error("Error fetching diff: ", error);
throw error;
}

const sourcebotFileDiffs: (sourcebot_file_diff | null)[] = parsedDiff.map((file) => {
if (!file.from || !file.to) {
Expand Down Expand Up @@ -64,4 +75,4 @@ export const githubPrParser = async (octokit: Octokit, pullRequest: GitHubPullRe
number: pullRequest.number,
head_sha: pullRequest.head.sha
}
}
}