Skip to content

Commit

Permalink
feat: add paginate and catch everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Nov 25, 2023
1 parent 23ef147 commit b700ce5
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import { Context, Payload, User } from "../../../../types";
import { Context, User } from "../../../../types";

export async function getCollaboratorsForRepo(context: Context): Promise<User[]> {
const payload = context.event.payload as Payload;
const collaboratorUsers: User[] = [];
const payload = context.payload;

try {
let page = 1;
let shouldFetch = true;

while (shouldFetch) {
const res = await context.event.octokit.rest.repos.listCollaborators({
owner: payload.repository.owner.login,
repo: payload.repository.name,
per_page: 100,
page: page,
});

if (res.data.length > 0) {
res.data.forEach((collaborator) => collaboratorUsers.push(collaborator as User));
page++;
} else {
shouldFetch = false;
}
}
} catch (e: unknown) {
context.logger.error("Fetching collaborator IDs for repo failed!", e);
const collaboratorUsers = (await context.octokit.paginate(context.octokit.rest.repos.listCollaborators, {
owner: payload.repository.owner.login,
repo: payload.repository.name,
per_page: 100,
})) as User[];
return collaboratorUsers;
} catch (err: unknown) {
context.logger.error("Failed to fetch lists of collaborators", err);
return [];
}

return collaboratorUsers;
}
61 changes: 23 additions & 38 deletions src/handlers/wildcard/unassign/unassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,55 +310,40 @@ function getActiveAssigneesInDisqualifyDuration(
}

async function getAllEvents({ context, owner, repo, issueNumber }: GetAllEvents) {
let allEvents: IssuesListEventsResponseData = [];
let page = 1;
let events = await context.event.octokit.issues.listEvents({
owner,
repo,
issue_number: issueNumber,
per_page: 100,
page: page,
});

while (events.data.length > 0) {
allEvents = allEvents.concat(events.data.filter(isCorrectType) as IssuesListEventsResponseData);

events = await context.event.octokit.issues.listEvents({
owner,
repo,
issue_number: issueNumber,
per_page: 100,
page: ++page,
});
try {
const events = (await context.octokit.paginate(
context.octokit.rest.issues.listEvents,
{
owner,
repo,
issue_number: issueNumber,
per_page: 100,
},
(response) => response.data.filter((event) => isCorrectType(event as IssuesListEventsResponseData[0]))
)) as IssuesListEventsResponseData;
return events;
} catch (err: unknown) {
context.logger.error("Failed to fetch lists of events", err);
return [];
}
return allEvents;
}

async function getAllCommitsFromPullRequest({ context, owner, repo, pullNumber }: GetAllCommits) {
let allCommits = [] as Commit[];
let commitPage = 1;
let hasMoreCommits = true;

while (hasMoreCommits) {
const commits = await context.event.octokit.pulls.listCommits({
try {
const commits = (await context.octokit.paginate(context.octokit.pulls.listCommits, {
owner,
repo,
pull_number: pullNumber,
per_page: 100,
page: commitPage,
});

if (commits.data.length === 0) {
hasMoreCommits = false;
} else {
allCommits = allCommits.concat(commits.data as Commit[]);
commitPage++;
}
})) as Commit[];
return commits;
} catch (err: unknown) {
context.logger.error("Failed to fetch lists of commits", err);
return [];
}
return allCommits;
}

function isCorrectType(event: any): event is IssuesListEventsResponseData {
function isCorrectType(event: IssuesListEventsResponseData[0]) {
return event && typeof event.id === "number";
}

Expand Down
Loading

0 comments on commit b700ce5

Please sign in to comment.