post-merge
Version:
A reusable library for handling post-merge operations including version bumping and git tagging
93 lines • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.configureGitUser = configureGitUser;
exports.configureGitRemote = configureGitRemote;
exports.commitChanges = commitChanges;
exports.ensureBranchExists = ensureBranchExists;
exports.pushChanges = pushChanges;
exports.createAndPushTag = createAndPushTag;
exports.buildRemoteUrl = buildRemoteUrl;
exports.createGitConfigFromEnv = createGitConfigFromEnv;
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Configures git user settings
*/
async function configureGitUser(userName, userEmail) {
await execAsync(`git config --global user.name "${userName}"`);
await execAsync(`git config --global user.email "${userEmail}"`);
console.log(`Git user configured: ${userName} <${userEmail}>`);
}
/**
* Configures git remote URL with access token
*/
async function configureGitRemote(remoteUrl) {
await execAsync(`git remote set-url origin ${remoteUrl}`);
console.log('Git remote URL configured with access token');
}
/**
* Commits changes with a specific message
*/
async function commitChanges(message) {
await execAsync('git add package.json');
await execAsync(`git commit -m "${message}"`);
console.log(`Changes committed: ${message}`);
}
/**
* Ensures branch exists and checks it out if needed
*/
async function ensureBranchExists(branchName) {
try {
// Check if local branch exists
await execAsync(`git show-ref --verify --quiet refs/heads/${branchName}`);
console.log(`Local branch ${branchName} exists`);
}
catch (error) {
console.log(`Local branch ${branchName} does not exist, creating it from current HEAD...`);
await execAsync(`git checkout -b ${branchName}`);
}
}
/**
* Pushes changes to remote branch
*/
async function pushChanges(branchName) {
console.log(`Pushing to origin ${branchName}`);
await execAsync(`git push origin HEAD:${branchName}`);
console.log('Changes pushed successfully');
}
/**
* Creates and pushes a git tag
*/
async function createAndPushTag(tagName) {
console.log(`Creating and pushing tag ${tagName}...`);
await execAsync(`git tag "${tagName}"`);
await execAsync(`git push origin "${tagName}"`);
console.log(`Tag ${tagName} created and pushed successfully`);
}
/**
* Builds git remote URL with access token
*/
function buildRemoteUrl(serverHost, projectPath, accessToken) {
return `https://oauth2:${accessToken}@${serverHost}/${projectPath}.git`;
}
/**
* Creates GitConfig from environment variables and overrides
*/
function createGitConfigFromEnv(overrides = {}) {
const serverHost = process.env.CI_SERVER_HOST || 'gitlab.com';
const projectPath = process.env.CI_PROJECT_PATH || '';
const accessToken = overrides.accessToken || process.env.CI_PUSH_TOKEN || '';
const branchName = process.env.CI_COMMIT_BRANCH || 'main';
const userName = process.env.GITLAB_USER_NAME || 'GitLab CI';
const userEmail = process.env.GITLAB_USER_EMAIL || 'ci@gitlab.com';
return {
userName,
userEmail,
remoteUrl: buildRemoteUrl(serverHost, projectPath, accessToken),
branchName,
accessToken,
...overrides
};
}
//# sourceMappingURL=git-utils.js.map