UNPKG

github-pr-automation

Version:

MCP server and CLI for automated GitHub PR management, review resolution, and workflow optimization

124 lines 4.36 kB
/** * Parallel request handling for GitHub API * * Batches related API calls to improve performance and reduce * the number of round trips to GitHub's API. */ import { parsePRIdentifier } from "../utils/parser.js"; /** * Handles parallel GitHub API requests with concurrency control * * Manages batching and parallel execution of GitHub API calls * to improve performance while respecting rate limits. */ export class ParallelRequestHandler { client; maxConcurrency; /** * Create a new parallel request handler * @param client - GitHub client instance * @param maxConcurrency - Maximum concurrent requests (default: 5) */ constructor(client, maxConcurrency = 5) { this.client = client; this.maxConcurrency = maxConcurrency; } /** * Execute multiple requests in parallel with concurrency control * @param requests - Array of batch requests to execute * @returns Promise resolving to array of batch results */ async executeBatch(requests) { const results = []; // Sort by priority const sortedRequests = requests.sort((a, b) => { const priorityOrder = { high: 0, normal: 1, low: 2 }; return priorityOrder[a.priority] - priorityOrder[b.priority]; }); // Process in chunks to control concurrency for (let i = 0; i < sortedRequests.length; i += this.maxConcurrency) { const chunk = sortedRequests.slice(i, i + this.maxConcurrency); const chunkPromises = chunk.map((request) => this.executeRequest(request)); const chunkResults = await Promise.all(chunkPromises); results.push(...chunkResults); } return results; } /** * Execute a single request * @param request - Batch request to execute * @returns Promise resolving to batch result */ async executeRequest(request) { try { const data = await request.fn(); return { key: request.key, data }; } catch (error) { return { key: request.key, error: error instanceof Error ? error : new Error(String(error)), }; } } /** * Batch PR metadata requests * @param prs - Array of PR identifiers * @returns Promise resolving to array of batch results */ async batchPRMetadata(prs) { const requests = prs.map((pr) => ({ key: pr, fn: () => { const parsed = parsePRIdentifier(pr); return this.client.getPullRequest(parsed); }, priority: "high", })); return this.executeBatch(requests); } /** * Batch check runs requests * @param commits - Array of commit objects with owner, repo, and sha * @returns Promise resolving to array of batch results */ async batchCheckRuns(commits) { const requests = commits.map((commit) => ({ key: `${commit.owner}/${commit.repo}@${commit.sha}`, fn: () => this.client.getOctokit().rest.checks.listForRef({ owner: commit.owner, repo: commit.repo, ref: commit.sha, }), priority: "normal", })); return this.executeBatch(requests); } /** * Batch comment requests * @param prs - Array of PR objects with owner, repo, and number * @returns Promise resolving to array of batch results */ async batchComments(prs) { const requests = prs.map((pr) => ({ key: `${pr.owner}/${pr.repo}#${pr.number}`, fn: () => this.client.getOctokit().rest.issues.listComments({ owner: pr.owner, repo: pr.repo, issue_number: pr.number, }), priority: "normal", })); return this.executeBatch(requests); } } /** * Create a parallel request handler for a GitHub client * @param client - GitHub client instance * @param maxConcurrency - Maximum concurrent requests (default: 5) * @returns New parallel request handler instance */ export function createParallelHandler(client, maxConcurrency = 5) { return new ParallelRequestHandler(client, maxConcurrency); } //# sourceMappingURL=parallel-requests.js.map