UNPKG

pr-sizewise

Version:

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

117 lines 4.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitLabProvider = void 0; const rest_1 = require("@gitbeaker/rest"); /** * GitLab provider implementation */ class GitLabProvider { async initialize(config) { this.gitlab = new rest_1.Gitlab({ token: config.token, host: config.host, }); this.projectId = config.projectId; } async getDiffs(pullRequestId) { const mrId = parseInt(pullRequestId, 10); const diffs = await this.gitlab.MergeRequests.allDiffs(this.projectId, mrId); const changes = diffs; return changes.map((change) => ({ oldPath: change.old_path, newPath: change.new_path, diff: change.diff, isNewFile: change.new_file, isDeletedFile: change.deleted_file, isRenamedFile: change.renamed_file, })); } async getComments(pullRequestId) { const mrId = parseInt(pullRequestId, 10); const notes = await this.gitlab.MergeRequestNotes.all(this.projectId, mrId); return notes.map((note) => ({ id: note.id.toString(), body: note.body, authorId: note.author.id.toString(), createdAt: note.created_at, updatedAt: note.updated_at, })); } async createComment(pullRequestId, body) { const mrId = parseInt(pullRequestId, 10); const note = await this.gitlab.MergeRequestNotes.create(this.projectId, mrId, body); return { id: note.id.toString(), body: note.body, authorId: note.author.id.toString(), createdAt: note.created_at, updatedAt: note.updated_at, }; } async updateComment(pullRequestId, commentId, body) { const mrId = parseInt(pullRequestId, 10); const noteId = parseInt(commentId, 10); const note = await this.gitlab.MergeRequestNotes.edit(this.projectId, mrId, noteId, { body }); return { id: note.id.toString(), body: note.body, authorId: note.author.id.toString(), createdAt: note.created_at, updatedAt: note.updated_at, }; } async getLabels(pullRequestId) { const mrId = parseInt(pullRequestId, 10); const mr = await this.gitlab.MergeRequests.show(this.projectId, mrId); return Array.isArray(mr.labels) ? mr.labels.map((label) => { if (typeof label === 'string') { return label; } if (typeof label === 'object' && label !== null) { if ('name' in label && typeof label.name === 'string') { return label.name; } if ('title' in label && typeof label.title === 'string') { return label.title; } } return String(label); }) : []; } async setLabels(pullRequestId, labels) { const mrId = parseInt(pullRequestId, 10); await this.gitlab.MergeRequests.edit(this.projectId, mrId, { labels: labels.join(','), }); } async getPullRequest(pullRequestId) { const mrId = parseInt(pullRequestId, 10); const mr = await this.gitlab.MergeRequests.show(this.projectId, mrId); return { id: mr.id.toString(), title: mr.title, description: mr.description || '', authorId: mr.author.id.toString(), state: this.mapState(mr.state), labels: Array.isArray(mr.labels) ? mr.labels : [], createdAt: mr.created_at, updatedAt: mr.updated_at, }; } mapState(gitlabState) { switch (gitlabState.toLowerCase()) { case 'opened': return 'open'; case 'merged': return 'merged'; case 'closed': return 'closed'; default: return 'open'; } } } exports.GitLabProvider = GitLabProvider; //# sourceMappingURL=gitlab.js.map