mirror of
https://github.com/bendtherules/github-delete-unused-repos.git
synced 2026-08-18 13:52:59 +00:00
Add generic pagination util + use in branch fetch
This commit is contained in:
@@ -8,14 +8,27 @@ octokit.authenticate({
|
||||
secret: '2228539a48032f0622d6c12a66f56253d0a30d60',
|
||||
});
|
||||
|
||||
interface ResponseWithDataArray<T> {
|
||||
data: T[];
|
||||
}
|
||||
|
||||
interface ResponseWithMetaLink {
|
||||
meta: {
|
||||
link: string
|
||||
};
|
||||
}
|
||||
|
||||
interface ResponseWithPaginationAndMeta<T> extends ResponseWithDataArray<T>, ResponseWithMetaLink {
|
||||
|
||||
}
|
||||
|
||||
interface ResponseFromGetUserRepo {
|
||||
data: RepoFromGetUserRepo[];
|
||||
meta: {};
|
||||
}
|
||||
|
||||
interface ResponseFromGetBranches {
|
||||
interface ResponseFromGetBranches extends ResponseWithMetaLink {
|
||||
data: BranchFromGetBranches[];
|
||||
meta: {};
|
||||
}
|
||||
|
||||
interface ResponseFromGetRepo {
|
||||
@@ -103,15 +116,46 @@ interface RepoNameWithBranchesAndParent
|
||||
|
||||
const username = 'bendtherules';
|
||||
|
||||
interface ObjectWithPerPage {
|
||||
per_page?: number;
|
||||
}
|
||||
|
||||
async function paginate<TFirstParam extends ObjectWithPerPage, TDataElement>(
|
||||
method: (args: TFirstParam) => ResponseWithPaginationAndMeta<TDataElement>,
|
||||
args: TFirstParam
|
||||
): Promise<ResponseWithDataArray<TDataElement>> {
|
||||
|
||||
// Set per_page
|
||||
args.per_page = 100;
|
||||
|
||||
let response: ResponseWithPaginationAndMeta<TDataElement> = await method(args);
|
||||
|
||||
// Concat all data
|
||||
let { data } = response;
|
||||
while (octokit.hasNextPage(response)) {
|
||||
response = await octokit.getNextPage(response);
|
||||
data = data.concat(response.data);
|
||||
}
|
||||
|
||||
return {
|
||||
data
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchRepoNameWithBranches(
|
||||
repoName: string
|
||||
): Promise<RepoNameWithBranches> {
|
||||
const branchesResponse: ResponseFromGetBranches = await octokit.repos.getBranches(
|
||||
{
|
||||
const params: rest.ReposGetBranchesParams = {
|
||||
owner: username,
|
||||
repo: repoName,
|
||||
per_page: 10, // change this
|
||||
}
|
||||
};
|
||||
|
||||
const branchesResponse: ResponseWithDataArray<BranchFromGetBranches> =
|
||||
await paginate(
|
||||
(tmpFirstParam: rest.ReposGetBranchesParams): ResponseFromGetBranches => {
|
||||
return octokit.repos.getBranches(tmpFirstParam) as any as ResponseFromGetBranches;
|
||||
},
|
||||
params
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user