@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
54 lines (53 loc) • 2.15 kB
JavaScript
import { forgeFailure, ForgeError } from "./errors.js";
import { gitboyIssues } from "./gitboy.js";
import { githubIssues } from "./github.js";
import { issueLimit, orderIssues } from "./provider.js";
import { forgeKind, parseRepo, repoParts } from "./repositories.js";
export { ForgeError } from "./errors.js";
export { parseRepo, parseRepoForProject, repoForProject } from "./repositories.js";
export function providerFor(repo) {
return repoParts(repo).kind === "gitboy" ? gitboyIssues : githubIssues;
}
export class IssueListError extends ForgeError {
errors;
constructor(errors) {
const first = errors[0];
super(first.code, errors.length === 1 ? first.error : `Could not list issues from any of the ${errors.length} repositories.`);
this.errors = errors;
this.name = "IssueListError";
}
}
export async function listAcross(query) {
const repos = new Map();
const errors = [];
for (const input of query.repos ?? []) {
let repo = "(invalid repository)";
try {
repo = parseRepo(input);
forgeKind(repo.split("/")[0]);
repos.set(repo.toLowerCase(), repo);
}
catch {
errors.push({ repo, code: "invalid", error: "Use a github.com or gitboy repository." });
}
}
const pending = repos.values();
const issues = [];
let successful = 0;
await Promise.all(Array.from({ length: Math.min(4, repos.size) }, async () => {
for (const repo of pending) {
try {
issues.push(...await providerFor(repo).list({ ...query, repos: [repo] }));
successful++;
}
catch (error) {
const failure = error instanceof ForgeError ? error : forgeFailure(0);
errors.push({ repo, code: failure.code, error: failure.message });
}
}
}));
const [first, ...rest] = errors.sort((a, b) => a.repo.localeCompare(b.repo));
if (!successful && first)
throw new IssueListError([first, ...rest]);
return { issues: orderIssues(issues).slice(0, issueLimit(query.limit)), errors };
}