Skip to content

Commit

Permalink
test: add tests for getAllPullRequestsWithRetry
Browse files Browse the repository at this point in the history
Introduce mock context and data to validate pull request retrieval.
  • Loading branch information
gentlementlegen committed Oct 29, 2024
1 parent 3d760e3 commit f6232c0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/get-pull-requests-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getAllPullRequestsFallback(
const organization = context.payload.repository.owner.login;

try {
const repositories = await octokit.paginate(octokit.repos.listForOrg, {
const repositories = await octokit.paginate(octokit.rest.repos.listForOrg, {
org: organization,
per_page: 100,
type: "all",
Expand All @@ -29,7 +29,7 @@ export async function getAllPullRequestsFallback(

const tasks = repositories.map(async (repo) => {
try {
const prs = await octokit.paginate(octokit.pulls.list, {
const prs = await octokit.paginate(octokit.rest.pulls.list, {
owner: organization,
repo: repo.name,
state,
Expand Down
49 changes: 49 additions & 0 deletions tests/fallbacks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { RestEndpointMethodTypes } from "@octokit/rest";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import { Context } from "../src/types/context";
import { getAllPullRequestsWithRetry } from "../src/utils/issue";

const username = "private-user";

const mockPullRequestData = [
{ id: 1, number: 123, state: "open", user: { login: username } },
{ id: 2, number: 124, state: "open", user: { login: "public-user" } },
] as unknown as RestEndpointMethodTypes["pulls"]["list"]["response"]["data"];

const mockOctokit = {
paginate: jest.fn().mockResolvedValue(mockPullRequestData),
rest: {
pulls: {
list: jest.fn().mockResolvedValue(mockPullRequestData),
},
repos: {
listForOrg: jest.fn().mockResolvedValue(mockPullRequestData),
},
},
};

// Mock context
const context: Context = {
eventName: "pull_request",
payload: {
repository: {
name: "test-repo",
owner: {
login: "test-owner",
},
},
},
octokit: mockOctokit as unknown as Context["octokit"],
logger: new Logs("debug"),
adapters: {},
} as unknown as Context;

describe("getAllPullRequestsWithRetry", () => {
it("should return pull requests even if user information is private", async () => {
const pullRequests = await getAllPullRequestsWithRetry(context, "all", username);
expect(pullRequests).toHaveLength(2);
expect(pullRequests[0].user?.login).toBe(username);
expect(pullRequests[1].user?.login).toBe(username);
console.log(pullRequests);
});
});

0 comments on commit f6232c0

Please sign in to comment.