forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add paginate and catch everywhere
- Loading branch information
Showing
3 changed files
with
211 additions
and
194 deletions.
There are no files selected for viewing
36 changes: 11 additions & 25 deletions
36
src/handlers/comment/handlers/issue/get-collaborator-ids-for-repo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.