UNPKG

post-merge

Version:

A reusable library for handling post-merge operations including version bumping and git tagging

96 lines 4.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostMergeHandler = void 0; const version_utils_1 = require("./version-utils"); const git_utils_1 = require("./git-utils"); /** * Main class for handling hotfix post-merge operations */ class PostMergeHandler { constructor(config = {}) { // Set default configuration this.config = { packageJsonPath: './package.json', remoteUrlPattern: '', accessToken: process.env.CI_PUSH_TOKEN || '', branchName: process.env.CI_COMMIT_BRANCH || 'main', createTags: true, versionStrategy: 'auto', prereleaseId: 'release', commitMessageTemplate: 'chore: bump version to {version} after hotfix merge', gitUserName: process.env.GITLAB_USER_NAME || 'GitLab CI', gitUserEmail: process.env.GITLAB_USER_EMAIL || 'ci@gitlab.com', nodejsImageUrl: process.env.NODEJS_IMAGE_URL || 'node:14', branchPattern: process.env.branchPattern || '', ...config }; } /** * Executes the complete hotfix post-merge process */ async execute() { try { console.log(`Hotfix push detected on branch ${this.config.branchName}`); // Configure git user await (0, git_utils_1.configureGitUser)(this.config.gitUserName, this.config.gitUserEmail); // Read current package info const packageInfo = await (0, version_utils_1.readPackageInfo)(this.config.packageJsonPath); console.log(`Current version: ${packageInfo.version}`); // Determine version strategy let strategy = this.config.versionStrategy; if (strategy === 'auto') { strategy = await (0, version_utils_1.determineVersionStrategy)(packageInfo.version, this.config.prereleaseId); } // Bump version const versionInfo = await (0, version_utils_1.bumpVersion)(strategy, this.config.prereleaseId); // Commit changes const commitMessage = this.config.commitMessageTemplate.replace('{version}', versionInfo.newVersion); await (0, git_utils_1.commitChanges)(commitMessage); // Configure git remote with access token const gitConfig = (0, git_utils_1.createGitConfigFromEnv)({ branchName: this.config.branchName, accessToken: this.config.accessToken, userName: this.config.gitUserName, userEmail: this.config.gitUserEmail }); await (0, git_utils_1.configureGitRemote)(gitConfig.remoteUrl); // Ensure branch exists await (0, git_utils_1.ensureBranchExists)(this.config.branchName); // Push changes await (0, git_utils_1.pushChanges)(this.config.branchName); let tagName; if (this.config.createTags) { tagName = `v${versionInfo.newVersion}`; await (0, git_utils_1.createAndPushTag)(tagName); } return { success: true, versionInfo, tagName }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); console.error('Hotfix post-merge process failed:', errorMessage); return { success: false, versionInfo: { currentVersion: '', newVersion: '', isPrerelease: false }, error: errorMessage }; } } /** * Static method to check if current CI environment matches hotfix criteria */ static shouldExecute() { // cause the library is used in the post-merge pipeline // "rules" in gitlab-ci.yml config is theoretically the validator. return true; } } exports.PostMergeHandler = PostMergeHandler; //# sourceMappingURL=hotfix-handler.js.map