UNPKG

pr-sizewise

Version:

A CLI tool that measures and reports pull request sizes for GitHub and GitLab, helping teams maintain manageable code changes.

152 lines 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHubProvider = void 0; const rest_1 = require("@octokit/rest"); const errors_1 = require("../utils/errors"); /** * GitHub provider implementation */ class GitHubProvider { async initialize(config) { this.octokit = new rest_1.Octokit({ auth: config.token, baseUrl: config.host.includes('github.com') ? undefined : `${config.host}/api/v3`, }); // Parse owner/repo from projectId (format: "owner/repo") const [owner, repo] = config.projectId.split('/'); if (!owner || !repo) { throw new errors_1.ValidationError('GitHub projectId must be in format "owner/repo"'); } this.owner = owner; this.repo = repo; } async getDiffs(pullRequestId) { try { const prNumber = parseInt(pullRequestId, 10); // Get the list of files in the PR const { data: files } = await this.octokit.pulls.listFiles({ owner: this.owner, repo: this.repo, pull_number: prNumber, }); const diffs = []; for (const file of files) { // For each file, get the actual diff content const diff = file.patch ?? ''; diffs.push({ oldPath: file.previous_filename ?? file.filename, newPath: file.filename, diff, isNewFile: file.status === 'added', isDeletedFile: file.status === 'removed', isRenamedFile: file.status === 'renamed', }); } return diffs; } catch (error) { if (error instanceof Error && 'status' in error) { throw new errors_1.APIError(`Failed to get PR diffs: ${error.message}`, error.status); } throw new errors_1.APIError(`Failed to get PR diffs: ${error instanceof Error ? error.message : String(error)}`); } } async getComments(pullRequestId) { const prNumber = parseInt(pullRequestId, 10); const { data: comments } = await this.octokit.issues.listComments({ owner: this.owner, repo: this.repo, issue_number: prNumber, }); return comments.map((comment) => ({ id: comment.id.toString(), body: comment.body ?? '', authorId: comment.user?.id.toString() ?? '', createdAt: comment.created_at, updatedAt: comment.updated_at, })); } async createComment(pullRequestId, body) { const prNumber = parseInt(pullRequestId, 10); const { data: comment } = await this.octokit.issues.createComment({ owner: this.owner, repo: this.repo, issue_number: prNumber, body, }); return { id: comment.id.toString(), body: comment.body ?? '', authorId: comment.user?.id.toString() ?? '', createdAt: comment.created_at, updatedAt: comment.updated_at, }; } async updateComment(pullRequestId, commentId, body) { const commentIdNum = parseInt(commentId, 10); const { data: comment } = await this.octokit.issues.updateComment({ owner: this.owner, repo: this.repo, comment_id: commentIdNum, body, }); return { id: comment.id.toString(), body: comment.body ?? '', authorId: comment.user?.id.toString() ?? '', createdAt: comment.created_at, updatedAt: comment.updated_at, }; } async getLabels(pullRequestId) { const prNumber = parseInt(pullRequestId, 10); const { data: pr } = await this.octokit.pulls.get({ owner: this.owner, repo: this.repo, pull_number: prNumber, }); return pr.labels?.map(label => typeof label === 'string' ? label : label.name ?? '').filter(Boolean) || []; } async setLabels(pullRequestId, labels) { const prNumber = parseInt(pullRequestId, 10); await this.octokit.issues.setLabels({ owner: this.owner, repo: this.repo, issue_number: prNumber, labels, }); } async getPullRequest(pullRequestId) { const prNumber = parseInt(pullRequestId, 10); const { data: pr } = await this.octokit.pulls.get({ owner: this.owner, repo: this.repo, pull_number: prNumber, }); return { id: pr.id.toString(), title: pr.title, description: pr.body ?? '', authorId: pr.user?.id.toString() ?? '', state: this.mapState(pr.state, pr.merged), labels: pr.labels?.map(label => typeof label === 'string' ? label : label.name ?? '').filter(Boolean) || [], createdAt: pr.created_at, updatedAt: pr.updated_at, }; } mapState(prState, merged) { if (merged) { return 'merged'; } switch (prState.toLowerCase()) { case 'open': return 'open'; case 'closed': return 'closed'; default: return 'open'; } } } exports.GitHubProvider = GitHubProvider; //# sourceMappingURL=github.js.map