mirror of
https://github.com/garronej/ts-ci.git
synced 2025-11-30 21:43:05 +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
100
src/tools/octokit-addons/getCommitAsyncIterable.ts
Normal file
100
src/tools/octokit-addons/getCommitAsyncIterable.ts
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
|
||||
import type { AsyncReturnType } from "evt/dist/tools/typeSafety/AsyncReturnType";
|
||||
import type { Octokit } from "@octokit/rest";
|
||||
|
||||
|
||||
/*
|
||||
.sha
|
||||
.commit.message
|
||||
.author.type
|
||||
.author.type !== "User"
|
||||
*/
|
||||
|
||||
/** Alias for the non exported ReposListCommitsResponseData type alias */
|
||||
export type Commit = AsyncReturnType<Octokit["repos"]["listCommits"]>["data"][number];
|
||||
|
||||
const per_page = 30;
|
||||
|
||||
/** Iterate over the commits of a repo's branch */
|
||||
export function getCommitAsyncIterableFactory(params: { octokit: Octokit; }) {
|
||||
|
||||
const { octokit } = params;
|
||||
|
||||
function getCommitAsyncIterable(
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
branch: string;
|
||||
}
|
||||
): AsyncIterable<Commit> {
|
||||
|
||||
const { owner, repo, branch } = params;
|
||||
|
||||
let commits: Commit[] = [];
|
||||
|
||||
let page = 0;
|
||||
|
||||
let isLastPage: boolean | undefined = undefined;
|
||||
|
||||
const getReposListCommitsResponseData = (params: { page: number }) =>
|
||||
octokit.repos.listCommits({
|
||||
owner,
|
||||
repo,
|
||||
per_page,
|
||||
"page": params.page,
|
||||
"sha": branch
|
||||
}).then(({ data }) => data)
|
||||
;
|
||||
|
||||
return {
|
||||
[Symbol.asyncIterator]() {
|
||||
return {
|
||||
"next": async ()=> {
|
||||
|
||||
if (commits.length === 0) {
|
||||
|
||||
if (isLastPage) {
|
||||
return { "done": true, "value": undefined };
|
||||
}
|
||||
|
||||
page++;
|
||||
|
||||
commits = await getReposListCommitsResponseData({ page });
|
||||
|
||||
if (commits.length === 0) {
|
||||
return { "done": true, "value": undefined };
|
||||
}
|
||||
|
||||
isLastPage =
|
||||
commits.length !== per_page ||
|
||||
(await getReposListCommitsResponseData({ "page": page + 1 })).length === 0
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
const [commit, ...rest] = commits;
|
||||
|
||||
commits = rest;
|
||||
|
||||
return {
|
||||
"value": commit,
|
||||
"done": false
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
return { getCommitAsyncIterable };
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue