Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduces tag creation for hot fix releases #37

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const flow = require('./utils/flow.js');
const options = require('./utils/command.js').options;
const templateUtils = require('./utils/template-utils.js');
const boxen = require('boxen');
const { exec } = require('child_process');

runYaba();

Expand All @@ -17,18 +18,21 @@ async function runYaba() {
// https://www.npmjs.com/package/tiny-updater OR https://www.npmjs.com/package/update-notifier
// can be used instead below method.

await tool.checkUpdate(); // check if the yaba cli has newer version
// check if the yaba cli has newer version
await tool.checkUpdate();

// check required ENV variables

flow.checkRequiredEnvVariables();

// check if the current directory is git repo
checkDirectory();

checkDirectory()
// check internet connection

await flow.checkInternetConnection();
// prepare username, repoOwner and releaseRepo

checkHotfixRelease();

// prepare username, repoOwner and releaseRepo
const username = await flow.retrieveUsername();
const repoOwner = helper.retrieveOwner(options.owner, username);
const releaseRepo = helper.retrieveReleaseRepo(options.repo);
Expand All @@ -44,7 +48,7 @@ async function runYaba() {

// show only changelog
if (canShowChangelog(changeLog)) {
printChangelog(releaseRepo, changeLog);
printChangelog(changeLog);
}

// create the release
Expand All @@ -67,9 +71,9 @@ async function prepareRelease(changeLog, repoOwner, releaseRepo, lastReleaseTag)
if (hasReleaseCreatePermission) {
let preparedChangeLog = helper.prepareChangeLog(options.body, changeLog);
let changeLogDetails = templateUtils.generateChangelog(preparedChangeLog, repoOwner, releaseRepo, lastReleaseTag, helper.releaseTagName(options.tag));
let releaseName = helper.releaseName(options.releaseName)
let releaseName = helper.releaseName(options.releaseName, options.hotfix);
const releaseUrl = await flow.createRelease(repoOwner, releaseRepo, options.draft, releaseName,
changeLogDetails, options.tag);
changeLogDetails, options.tag, options.hotfix);

// play yaba sound if the release successfully created
helper.playSound(options.sound);
Expand Down Expand Up @@ -99,7 +103,28 @@ function checkDirectory() {
}
}

function printChangelog(repoName, changeLog) {
function checkHotfixRelease() {
// check if the current branch is not main/master and command has hotfix flag
exec('git branch --show-current', (err, stdout, stderr) => {
if (err) {
console.log(`An error occurred: ${err.message}`);
process.exit();
}

if (stderr) {
console.error(`An error occurred: ${stderr}`);
process.exit();
}

const currentBranch = stdout.trim();
if (currentBranch === 'master' || currentBranch === 'main') {
console.log(kleur.red("you can not create hotfix from main or master branches!"));
process.exit();
}
});
}

function printChangelog(changeLog) {
const changelogBoxOptions = {
padding: 1,
title: 'Changelog',
Expand Down
5 changes: 5 additions & 0 deletions bin/utils/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const commands = yargs
describe: "Creates the release as draft.",
type: "boolean"
})
.option("f", {
alias: "hotfix",
describe: "Prepares the release with hotfix tag.",
type: "boolean"
})
.option("c", {
alias: "changelog",
describe: "Shows only changelog without creating the release.",
Expand Down
7 changes: 4 additions & 3 deletions bin/utils/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ module.exports = {
* @param name the name/title of the release
* @param body the changelog to be defined in the release
* @param tag_name the tag name
* @param hotfix checks if the tag name must be created as hotfix or not
* @returns {Promise<void>}
*/
createRelease: async function (owner, repo, draft, name, body, tag_name) {
createRelease: async function (owner, repo, draft, name, body, tag_name, hotfix) {

try {
spinner.start('Preparing the release...');
Expand All @@ -180,9 +181,9 @@ module.exports = {
owner: owner,
repo: repo,
draft: draft,
name: helper.releaseName(name),
name: name,
body: body,
tag_name: helper.releaseTagName(tag_name)
tag_name: helper.releaseTagName(tag_name, hotfix)
});

// if the user terminal supports hyperlink, this will be a clickable link, otherwise prints plain text.
Expand Down
15 changes: 12 additions & 3 deletions bin/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ module.exports = {
* this prepares the tag name in 'prod_global_yyyy-MM-dd.1' format.
*
* @param tagName the given tag name
* @param hotfix checks if the tag name must be created as hotfix or not
* @returns {string|*} the prepared tag name
*/
releaseTagName: function (tagName) {
releaseTagName: function (tagName, hotfix) {

if (stringUtils.isBlank(tagName)) {
const currentDate = format(new Date(), constants.TAG_DATE_FORMAT);
if (hotfix) {
return `hotfix_prod_globl_${currentDate}.1`;
}
return `prod_global_${currentDate}.1`;
}

Expand All @@ -38,10 +42,15 @@ module.exports = {
* prepares the title of the release. if the given {@code name} is blank, this prepares
* the title in 'Global release yyyy-MM-dd' format.
* @param name the given release name
* @param hotfix checks if the release name must be created as hotfix or not
* @returns {string|*}
*/
releaseName: function (name) {
return stringUtils.isBlank(name) ? `Global release ${this.releaseDate()}` : name;
releaseName: function (name, hotfix) {
if(stringUtils.isBlank(name)) {
let releaseName = `Global release ${this.releaseDate()}`;
return hotfix ? 'Hotfix - ' + releaseName : releaseName;
}
return name;
},

/**
Expand Down