1.0.0 • Published 5 months ago
gitlab-mr-extractor v1.0.0
GitLab Merge Request Extractor
A TypeScript library for extracting and analyzing GitLab merge request data, including code diffs and changes.
Features
- Extract merge request data from GitLab repositories
- Parse and analyze code diffs
- Export data in multiple formats (JSON, CSV, MDX)
- Save code changes and diffs to files
- TypeScript support with type definitions
- Error handling
- Simple API
Installation
npm install gitlab-mr-extractor
Quick Start
import { GitLabMergeRequestExtractor } from 'gitlab-mr-extractor';
// Initialize the extractor
const extractor = new GitLabMergeRequestExtractor({
baseUrl: 'https://gitlab.com',
privateToken: 'your-gitlab-token',
projectId: 'your-project-id'
});
// Extract merge requests
const mergeRequests = await extractor.extractMergeRequests({
maxResults: 5 // Optional: limit results
});
API Reference
GitLabMergeRequestExtractor
Main class for interacting with GitLab merge requests.
Constructor
constructor(config: GitLabConfig)
Parameters:
config
: Configuration object containing:baseUrl
: GitLab instance URLprivateToken
: GitLab API tokenprojectId
: Target project ID
Methods
extractMergeRequests
async extractMergeRequests(options?: FetchOptions): Promise<MergeRequestData[]>
Parameters:
options
: Optional configurationauthorId
: Filter by author IDmaxResults
: Limit number of results
Returns: Array of merge request data
DiffParser
Utility class for parsing Git diffs.
static parse(rawDiff: string): DiffChange[]
Data Structures
MergeRequestData
interface MergeRequestData {
id: number;
iid: number;
title: string;
description: string;
state: string;
merged_at: string;
author: {
id: number;
name: string;
username: string;
};
changes: CodeDiff[];
}
CodeDiff
interface CodeDiff {
old_path: string;
new_path: string;
diff: string;
changes: DiffChange[];
}
Examples
Basic Usage
import { GitLabMergeRequestExtractor } from 'gitlab-mr-extractor';
async function main() {
const extractor = new GitLabMergeRequestExtractor({
baseUrl: 'https://gitlab.com',
privateToken: process.env.GITLAB_TOKEN,
projectId: process.env.GITLAB_PROJECT_ID
});
const mergeRequests = await extractor.extractMergeRequests();
console.log(`Found ${mergeRequests.length} merge requests`);
}
Saving Results
// Save as JSON
fs.writeFileSync('results.json', JSON.stringify(mergeRequests, null, 2));
// Save as CSV
import * as json2csv from 'json2csv';
const csv = json2csv.parse(mergeRequests);
fs.writeFileSync('results.csv', csv);
// Save as MDX
const mdxContent = generateMdxContent(mergeRequests);
fs.writeFileSync('results.mdx', mdxContent);
Error Handling
try {
const mergeRequests = await extractor.extractMergeRequests();
} catch (error) {
if (error instanceof GitLabApiError) {
console.error('GitLab API Error:', error.message);
console.error('Status:', error.status);
} else if (error instanceof InvalidConfigError) {
console.error('Configuration Error:', error.message);
}
}
Contributing
To contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/new-feature
) - Commit your changes (
git commit -m 'Add new feature'
) - Push to the branch (
git push origin feature/new-feature
) - Open a Pull Request
Requirements:
- Write tests for new features
- Update documentation as needed
- Follow the existing code style
- Write clear commit messages
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support:
Acknowledgments
- GitLab API Team
- Contributors
Created by Bhargav
1.0.0
5 months ago