mirror of
https://github.com/garronej/ts-ci.git
synced 2025-12-01 05:43:06 +00:00
Follow up from https://github.com/garronej/github_actions_toolkit
This commit is contained in:
commit
5297ab00ab
54 changed files with 18162 additions and 0 deletions
93
src/tools/octokit-addons/getPullRequestAsyncIterable.ts
Normal file
93
src/tools/octokit-addons/getPullRequestAsyncIterable.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
|
||||
import type { AsyncReturnType } from "evt/dist/tools/typeSafety/AsyncReturnType";
|
||||
import type { Octokit } from "@octokit/rest";
|
||||
|
||||
|
||||
|
||||
/** Alias for the non exported PullsListResponseData type alias */
|
||||
export type PullRequest = AsyncReturnType<Octokit["pulls"]["list"]>["data"][number];
|
||||
|
||||
const per_page = 99;
|
||||
|
||||
export function getPullRequestAsyncIterableFactory(params: { octokit: Octokit; }) {
|
||||
|
||||
const { octokit } = params;
|
||||
|
||||
function getPullRequestAsyncIterable(
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
state: "open" | "closed" | "all"
|
||||
}
|
||||
): AsyncIterable<PullRequest> {
|
||||
|
||||
const { owner, repo, state } = params;
|
||||
|
||||
let pullRequests: PullRequest[] = [];
|
||||
|
||||
let page = 0;
|
||||
|
||||
let isLastPage: boolean | undefined = undefined;
|
||||
|
||||
const getPullsListResponseData = (params: { page: number }) =>
|
||||
octokit.pulls.list({
|
||||
owner,
|
||||
repo,
|
||||
state,
|
||||
per_page,
|
||||
"page": params.page
|
||||
}).then(({ data }) => data)
|
||||
;
|
||||
|
||||
return {
|
||||
[Symbol.asyncIterator]() {
|
||||
return {
|
||||
"next": async ()=> {
|
||||
|
||||
if (pullRequests.length === 0) {
|
||||
|
||||
if (isLastPage) {
|
||||
return { "done": true, "value": undefined };
|
||||
}
|
||||
|
||||
page++;
|
||||
|
||||
pullRequests = await getPullsListResponseData({ page });
|
||||
|
||||
if (pullRequests.length === 0) {
|
||||
return { "done": true, "value": undefined };
|
||||
}
|
||||
|
||||
isLastPage =
|
||||
pullRequests.length !== per_page ||
|
||||
(await getPullsListResponseData({ "page": page + 1 })).length === 0
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
const [pullRequest, ...rest] = pullRequests;
|
||||
|
||||
pullRequests = rest;
|
||||
|
||||
return {
|
||||
"value": pullRequest,
|
||||
"done": false
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
return { getPullRequestAsyncIterable };
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue