@bioinsights/release-it-gitlab-mr-changelog
Version:
A release-it plugin that generates a changelog from GitLab merge requests using conventional-changelog format
170 lines (149 loc) • 5.05 kB
JavaScript
const path = require('path');
const fs = require('fs');
const https = require('https');
// Mock environment variables
// process.env.GITLAB_TOKEN = 'your_gitlab_token'; // Replace with your actual token for testing
// process.env.CI_PROJECT_ID = '2';
// process.env.CI_API_V4_URL = 'https://gitlab.bioinsights.com/api/v4';
// Mock the Plugin class from release-it
class MockPlugin {
constructor(options) {
this.options = options.options || {};
this.config = {
getContext: () => ({
version: '1.0.0',
tagName: 'v1.0.0',
previousTag: 'v0.9.0',
repo: {
host: 'gitlab.bioinsights.com',
repository: 'bioinsights-api/api'
}
}),
isDryRun: false
};
this.log = {
warn: console.warn,
exec: console.log
};
this.debug = console.debug;
this.context = {};
}
getContext(path) {
return path ? this.context[path] : this.context;
}
setContext(context) {
Object.assign(this.context, context);
}
exec(command, options) {
console.log(`Executing: ${command}`);
return '';
}
}
// Helper function to fetch data from GitLab API
async function fetchFromGitLab(endpoint) {
return new Promise((resolve, reject) => {
const url = `${process.env.CI_API_V4_URL}/${endpoint}`;
const options = {
headers: {
'PRIVATE-TOKEN': process.env.GITLAB_TOKEN
}
};
console.log(`Fetching from: ${url}`);
https.get(url, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
try {
const result = JSON.parse(data);
resolve(result);
}
catch (error) {
reject(new Error(`Failed to parse response: ${error.message}`));
}
});
}).on('error', (err) => {
reject(new Error(`Error fetching from GitLab: ${err.message}`));
});
});
}
// Import our plugin
const GitlabMergeRequestChangelog = require('./plugins/release-it-gitlab-mr-changelog');
// Create an instance of our plugin with mock options
const plugin = new GitlabMergeRequestChangelog({
namespace: 'gitlab-mr-changelog',
options: {
'gitlab-mr-changelog': {
infile: 'CHANGELOG.md'
}
},
container: {
config: {
getContext: () => ({
version: '1.0.0',
tagName: 'v1.0.0',
previousTag: 'v0.9.0',
repo: {
host: 'gitlab.bioinsights.com',
repository: 'bioinsights-api/api'
}
}),
isDryRun: false,
setContext: (context) => console.log('Setting context:', context)
},
log: {
warn: console.warn,
exec: console.log,
verbose: console.log,
debug: console.debug
},
shell: {
exec: (command, options) => {
console.log(`Executing: ${command}`);
return '';
}
},
prompt: {
register: () => {
},
show: () => Promise.resolve()
},
spinner: {
show: () => Promise.resolve()
}
}
});
// Test the plugin
async function testPlugin() {
try {
console.log('Testing GitLab API connection...');
// Test GitLab API connection by fetching project info
const projectInfo = await fetchFromGitLab(`projects/${process.env.CI_PROJECT_ID}`);
console.log('Project info:', projectInfo.name);
// Test fetching merge requests
console.log('Fetching merge requests...');
const mergeRequests = await fetchFromGitLab(`projects/${process.env.CI_PROJECT_ID}/merge_requests`);
console.log(`Fetched ${mergeRequests.length} merge requests`);
// Test fetching releases
console.log('Fetching releases...');
const releases = await fetchFromGitLab(`projects/${process.env.CI_PROJECT_ID}/releases`);
console.log(`Fetched ${releases.length} releases`);
// Initialize the plugin
console.log('Initializing plugin...');
await plugin.init();
// Generate a changelog
console.log('Generating changelog...');
const changelog = await plugin.getChangelog('0.9.0');
console.log('Generated changelog:');
console.log(changelog);
// Write the changelog to a test file
fs.writeFileSync('test-CHANGELOG.md', changelog);
console.log('Changelog written to test-CHANGELOG.md');
}
catch (error) {
console.error('Error testing plugin:', error);
}
}
testPlugin();