Add new branch versioning (MINOR)

This commit is contained in:
Paul Hatcherian 2023-09-27 20:17:26 -04:00
parent d3c0da227f
commit 61963e734d
21 changed files with 447 additions and 34 deletions

View file

@ -13,7 +13,11 @@ inputs:
required: false required: false
default: "v" default: "v"
use_branches: use_branches:
description: "Use branches instead of tags" description: "(Deprecated) Use branches instead of tags"
required: false
default: "false"
version_from_branch:
description: If true, the branch will be used to select the maximum version
required: false required: false
default: "false" default: "false"
major_pattern: major_pattern:

138
dist/index.js vendored
View file

@ -81,6 +81,7 @@ exports.cmd = cmd;
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ConfigurationProvider = void 0; exports.ConfigurationProvider = void 0;
const CsvUserFormatter_1 = __nccwpck_require__(9105); const CsvUserFormatter_1 = __nccwpck_require__(9105);
const BranchVersioningTagFormatter_1 = __nccwpck_require__(807);
const DefaultTagFormatter_1 = __nccwpck_require__(4808); const DefaultTagFormatter_1 = __nccwpck_require__(4808);
const DefaultVersionFormatter_1 = __nccwpck_require__(8524); const DefaultVersionFormatter_1 = __nccwpck_require__(8524);
const JsonUserFormatter_1 = __nccwpck_require__(7892); const JsonUserFormatter_1 = __nccwpck_require__(7892);
@ -105,7 +106,12 @@ class ConfigurationProvider {
return new DefaultVersionClassifier_1.DefaultVersionClassifier(this.config); return new DefaultVersionClassifier_1.DefaultVersionClassifier(this.config);
} }
GetVersionFormatter() { return new DefaultVersionFormatter_1.DefaultVersionFormatter(this.config); } GetVersionFormatter() { return new DefaultVersionFormatter_1.DefaultVersionFormatter(this.config); }
GetTagFormatter() { return new DefaultTagFormatter_1.DefaultTagFormatter(this.config); } GetTagFormatter(branchName) {
if (this.config.versionFromBranch) {
return new BranchVersioningTagFormatter_1.BranchVersioningTagFormatter(this.config, branchName);
}
return new DefaultTagFormatter_1.DefaultTagFormatter(this.config);
}
GetUserFormatter() { GetUserFormatter() {
switch (this.config.userFormatType) { switch (this.config.userFormatType) {
case 'json': return new JsonUserFormatter_1.JsonUserFormatter(this.config); case 'json': return new JsonUserFormatter_1.JsonUserFormatter(this.config);
@ -300,12 +306,12 @@ function runAction(configurationProvider) {
const commitsProvider = configurationProvider.GetCommitsProvider(); const commitsProvider = configurationProvider.GetCommitsProvider();
const versionClassifier = configurationProvider.GetVersionClassifier(); const versionClassifier = configurationProvider.GetVersionClassifier();
const versionFormatter = configurationProvider.GetVersionFormatter(); const versionFormatter = configurationProvider.GetVersionFormatter();
const tagFormatter = configurationProvider.GetTagFormatter(); const tagFormatter = configurationProvider.GetTagFormatter(yield currentCommitResolver.ResolveBranchNameAsync());
const userFormatter = configurationProvider.GetUserFormatter(); const userFormatter = configurationProvider.GetUserFormatter();
const debugManager = DebugManager_1.DebugManager.getInstance(); const debugManager = DebugManager_1.DebugManager.getInstance();
if (yield currentCommitResolver.IsEmptyRepoAsync()) { if (yield currentCommitResolver.IsEmptyRepoAsync()) {
const versionInfo = new VersionInformation_1.VersionInformation(0, 0, 0, 0, VersionType_1.VersionType.None, [], false, false); const versionInfo = new VersionInformation_1.VersionInformation(0, 0, 0, 0, VersionType_1.VersionType.None, [], false, false);
return new VersionResult_1.VersionResult(versionInfo.major, versionInfo.minor, versionInfo.patch, versionInfo.increment, versionInfo.type, versionFormatter.Format(versionInfo), tagFormatter.Format(versionInfo), versionInfo.changed, versionInfo.isTagged, userFormatter.Format('author', []), '', '', '0.0.0', debugManager.getDebugOutput(true)); return new VersionResult_1.VersionResult(versionInfo.major, versionInfo.minor, versionInfo.patch, versionInfo.increment, versionInfo.type, versionFormatter.Format(versionInfo), tagFormatter.Format(versionInfo), versionInfo.changed, versionInfo.isTagged, userFormatter.Format('author', []), '', '', tagFormatter.Parse(tagFormatter.Format(versionInfo)).join('.'), debugManager.getDebugOutput(true));
} }
const currentCommit = yield currentCommitResolver.ResolveAsync(); const currentCommit = yield currentCommitResolver.ResolveAsync();
const lastRelease = yield lastReleaseResolver.ResolveAsync(currentCommit, tagFormatter); const lastRelease = yield lastReleaseResolver.ResolveAsync(currentCommit, tagFormatter);
@ -333,6 +339,80 @@ function runAction(configurationProvider) {
exports.runAction = runAction; exports.runAction = runAction;
/***/ }),
/***/ 807:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.BranchVersioningTagFormatter = void 0;
const DefaultTagFormatter_1 = __nccwpck_require__(4808);
/** Default tag formatter which allows a prefix to be specified */
class BranchVersioningTagFormatter extends DefaultTagFormatter_1.DefaultTagFormatter {
getRegex(pattern) {
if (/^\/.+\/[i]*$/.test(pattern)) {
const regexEnd = pattern.lastIndexOf('/');
const parsedFlags = pattern.slice(pattern.lastIndexOf('/') + 1);
return new RegExp(pattern.slice(1, regexEnd), parsedFlags);
}
return new RegExp(pattern);
}
constructor(config, branchName) {
super(config);
this.branchName = branchName;
const pattern = config.versionFromBranch === true ?
new RegExp("[0-9]+.[0-9]+$|[0-9]+$") :
this.getRegex(config.versionFromBranch);
const result = pattern.exec(branchName);
let branchVersion;
switch (result === null || result === void 0 ? void 0 : result.length) {
case 1:
branchVersion = result[0];
break;
case 2:
branchVersion = result[1];
break;
default:
throw new Error(`Unable to parse version from branch named '${branchName}' using pattern '${pattern}'`);
}
const versionValues = branchVersion.split('.');
if (versionValues.length > 2) {
throw new Error(`The version string '${branchVersion}' parsed from branch '${branchName}' is invalid. It must be in the format 'major.minor' or 'major'`);
}
this.major = parseInt(versionValues[0]);
if (isNaN(this.major)) {
throw new Error(`The major version '${versionValues[0]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
if (versionValues.length > 1) {
this.minor = parseInt(versionValues[1]);
if (isNaN(this.minor)) {
throw new Error(`The minor version '${versionValues[1]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
}
}
IsValid(tag) {
if (!super.IsValid(tag)) {
return false;
}
const parsed = super.Parse(tag);
if (parsed[0] !== this.major) {
return false;
}
if (this.minor !== undefined && parsed[1] !== this.minor) {
return false;
}
return true;
}
Parse(tag) {
const parsed = super.Parse(tag);
return [this.major, this.minor || parsed[1], parsed[2]];
}
}
exports.BranchVersioningTagFormatter = BranchVersioningTagFormatter;
/***/ }), /***/ }),
/***/ 9105: /***/ 9105:
@ -383,6 +463,9 @@ class DefaultTagFormatter {
return `${this.tagPrefix}*[0-9].*[0-9].*[0-9]`; return `${this.tagPrefix}*[0-9].*[0-9].*[0-9]`;
} }
Parse(tag) { Parse(tag) {
if (tag === '') {
return [0, 0, 0];
}
let tagParts = tag let tagParts = tag
.replace(this.tagPrefix, '<--!PREFIX!-->') .replace(this.tagPrefix, '<--!PREFIX!-->')
.replace(this.namespace, '<--!NAMESPACE!-->') .replace(this.namespace, '<--!NAMESPACE!-->')
@ -534,10 +617,29 @@ function setOutput(versionResult) {
} }
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
function toBool(value) {
if (!value || value.toLowerCase() === 'false') {
return false;
}
else if (value.toLowerCase() === 'true') {
return true;
}
return false;
}
function toStringOrBool(value) {
if (!value || value === 'false') {
return false;
}
if (value === 'true') {
return true;
}
return value;
}
const config = { const config = {
branch: core.getInput('branch'), branch: core.getInput('branch'),
tagPrefix: core.getInput('tag_prefix'), tagPrefix: core.getInput('tag_prefix'),
useBranches: core.getInput('use_branches') === 'true', useBranches: toBool(core.getInput('use_branches')),
versionFromBranch: toStringOrBool(core.getInput('version_from_branch')),
majorPattern: core.getInput('major_pattern'), majorPattern: core.getInput('major_pattern'),
minorPattern: core.getInput('minor_pattern'), minorPattern: core.getInput('minor_pattern'),
majorFlags: core.getInput('major_regexp_flags'), majorFlags: core.getInput('major_regexp_flags'),
@ -545,14 +647,17 @@ function run() {
versionFormat: core.getInput('version_format'), versionFormat: core.getInput('version_format'),
changePath: core.getInput('change_path'), changePath: core.getInput('change_path'),
namespace: core.getInput('namespace'), namespace: core.getInput('namespace'),
bumpEachCommit: core.getInput('bump_each_commit') === 'true', bumpEachCommit: toBool(core.getInput('bump_each_commit')),
searchCommitBody: core.getInput('search_commit_body') === 'true', searchCommitBody: toBool(core.getInput('search_commit_body')),
userFormatType: core.getInput('user_format_type'), userFormatType: core.getInput('user_format_type'),
enablePrereleaseMode: core.getInput('enable_prerelease_mode') === 'true', enablePrereleaseMode: toBool(core.getInput('enable_prerelease_mode')),
bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'), bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'),
debug: core.getInput('debug') === 'true', debug: toBool(core.getInput('debug')),
replay: '' replay: ''
}; };
if (config.useBranches) {
core.warning(`The 'use_branches' input option is deprecated, please see the documentation for more information on how to use branches`);
}
if (config.versionFormat === '' && core.getInput('format') !== '') { if (config.versionFormat === '' && core.getInput('format') !== '') {
core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`); core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`);
config.versionFormat = core.getInput('format'); config.versionFormat = core.getInput('format');
@ -825,6 +930,14 @@ class DefaultCurrentCommitResolver {
return lastCommitAll === ''; return lastCommitAll === '';
}); });
} }
ResolveBranchNameAsync() {
return __awaiter(this, void 0, void 0, function* () {
const branchName = this.branch == 'HEAD' ?
process.env.GITHUB_REF_NAME || (yield (0, CommandRunner_1.cmd)('git', 'rev-parse', '--abbrev-ref', 'HEAD'))
: this.branch;
return branchName.trim();
});
}
} }
exports.DefaultCurrentCommitResolver = DefaultCurrentCommitResolver; exports.DefaultCurrentCommitResolver = DefaultCurrentCommitResolver;
@ -922,8 +1035,9 @@ class DefaultLastReleaseResolver {
core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.');
} }
} }
const [major, minor, patch] = tagFormatter.Parse('');
// no release tags yet, use the initial commit as the root // no release tags yet, use the initial commit as the root
return new ReleaseInformation_1.ReleaseInformation(0, 0, 0, '', currentMajor, currentMinor, currentPatch, isTagged); return new ReleaseInformation_1.ReleaseInformation(major, minor, patch, '', currentMajor, currentMinor, currentPatch, isTagged);
} }
// parse the version tag // parse the version tag
const [major, minor, patch] = tagFormatter.Parse(tag); const [major, minor, patch] = tagFormatter.Parse(tag);
@ -4713,7 +4827,7 @@ exports["default"] = _default;
/***/ }), /***/ }),
/***/ 807: /***/ 8292:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { /***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
"use strict"; "use strict";
@ -4831,7 +4945,7 @@ Object.defineProperty(exports, "__esModule", ({
})); }));
exports["default"] = void 0; exports["default"] = void 0;
var _rng = _interopRequireDefault(__nccwpck_require__(807)); var _rng = _interopRequireDefault(__nccwpck_require__(8292));
var _stringify = _interopRequireDefault(__nccwpck_require__(8950)); var _stringify = _interopRequireDefault(__nccwpck_require__(8950));
@ -5053,7 +5167,7 @@ Object.defineProperty(exports, "__esModule", ({
})); }));
exports["default"] = void 0; exports["default"] = void 0;
var _rng = _interopRequireDefault(__nccwpck_require__(807)); var _rng = _interopRequireDefault(__nccwpck_require__(8292));
var _stringify = _interopRequireDefault(__nccwpck_require__(8950)); var _stringify = _interopRequireDefault(__nccwpck_require__(8950));

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -8,8 +8,10 @@ class ActionConfig {
this.branch = "HEAD"; this.branch = "HEAD";
/** The prefix to use to identify tags */ /** The prefix to use to identify tags */
this.tagPrefix = "v"; this.tagPrefix = "v";
/** Use branches instead of tags */ /** (Deprecated) Use branches instead of tags */
this.useBranches = false; this.useBranches = false;
/** If true, the branch will be used to select the maximum version. */
this.versionFromBranch = false;
/** A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression. */ /** A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression. */
this.majorPattern = "(MAJOR)"; this.majorPattern = "(MAJOR)";
/** A string which indicates the flags used by the `majorPattern` regular expression. */ /** A string which indicates the flags used by the `majorPattern` regular expression. */

View file

@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurationProvider = void 0; exports.ConfigurationProvider = void 0;
const CsvUserFormatter_1 = require("./formatting/CsvUserFormatter"); const CsvUserFormatter_1 = require("./formatting/CsvUserFormatter");
const BranchVersioningTagFormatter_1 = require("./formatting/BranchVersioningTagFormatter");
const DefaultTagFormatter_1 = require("./formatting/DefaultTagFormatter"); const DefaultTagFormatter_1 = require("./formatting/DefaultTagFormatter");
const DefaultVersionFormatter_1 = require("./formatting/DefaultVersionFormatter"); const DefaultVersionFormatter_1 = require("./formatting/DefaultVersionFormatter");
const JsonUserFormatter_1 = require("./formatting/JsonUserFormatter"); const JsonUserFormatter_1 = require("./formatting/JsonUserFormatter");
@ -26,7 +27,12 @@ class ConfigurationProvider {
return new DefaultVersionClassifier_1.DefaultVersionClassifier(this.config); return new DefaultVersionClassifier_1.DefaultVersionClassifier(this.config);
} }
GetVersionFormatter() { return new DefaultVersionFormatter_1.DefaultVersionFormatter(this.config); } GetVersionFormatter() { return new DefaultVersionFormatter_1.DefaultVersionFormatter(this.config); }
GetTagFormatter() { return new DefaultTagFormatter_1.DefaultTagFormatter(this.config); } GetTagFormatter(branchName) {
if (this.config.versionFromBranch) {
return new BranchVersioningTagFormatter_1.BranchVersioningTagFormatter(this.config, branchName);
}
return new DefaultTagFormatter_1.DefaultTagFormatter(this.config);
}
GetUserFormatter() { GetUserFormatter() {
switch (this.config.userFormatType) { switch (this.config.userFormatType) {
case 'json': return new JsonUserFormatter_1.JsonUserFormatter(this.config); case 'json': return new JsonUserFormatter_1.JsonUserFormatter(this.config);

View file

@ -22,12 +22,12 @@ function runAction(configurationProvider) {
const commitsProvider = configurationProvider.GetCommitsProvider(); const commitsProvider = configurationProvider.GetCommitsProvider();
const versionClassifier = configurationProvider.GetVersionClassifier(); const versionClassifier = configurationProvider.GetVersionClassifier();
const versionFormatter = configurationProvider.GetVersionFormatter(); const versionFormatter = configurationProvider.GetVersionFormatter();
const tagFormatter = configurationProvider.GetTagFormatter(); const tagFormatter = configurationProvider.GetTagFormatter(yield currentCommitResolver.ResolveBranchNameAsync());
const userFormatter = configurationProvider.GetUserFormatter(); const userFormatter = configurationProvider.GetUserFormatter();
const debugManager = DebugManager_1.DebugManager.getInstance(); const debugManager = DebugManager_1.DebugManager.getInstance();
if (yield currentCommitResolver.IsEmptyRepoAsync()) { if (yield currentCommitResolver.IsEmptyRepoAsync()) {
const versionInfo = new VersionInformation_1.VersionInformation(0, 0, 0, 0, VersionType_1.VersionType.None, [], false, false); const versionInfo = new VersionInformation_1.VersionInformation(0, 0, 0, 0, VersionType_1.VersionType.None, [], false, false);
return new VersionResult_1.VersionResult(versionInfo.major, versionInfo.minor, versionInfo.patch, versionInfo.increment, versionInfo.type, versionFormatter.Format(versionInfo), tagFormatter.Format(versionInfo), versionInfo.changed, versionInfo.isTagged, userFormatter.Format('author', []), '', '', '0.0.0', debugManager.getDebugOutput(true)); return new VersionResult_1.VersionResult(versionInfo.major, versionInfo.minor, versionInfo.patch, versionInfo.increment, versionInfo.type, versionFormatter.Format(versionInfo), tagFormatter.Format(versionInfo), versionInfo.changed, versionInfo.isTagged, userFormatter.Format('author', []), '', '', tagFormatter.Parse(tagFormatter.Format(versionInfo)).join('.'), debugManager.getDebugOutput(true));
} }
const currentCommit = yield currentCommitResolver.ResolveAsync(); const currentCommit = yield currentCommitResolver.ResolveAsync();
const lastRelease = yield lastReleaseResolver.ResolveAsync(currentCommit, tagFormatter); const lastRelease = yield lastReleaseResolver.ResolveAsync(currentCommit, tagFormatter);

View file

@ -0,0 +1,66 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BranchVersioningTagFormatter = void 0;
const DefaultTagFormatter_1 = require("./DefaultTagFormatter");
/** Default tag formatter which allows a prefix to be specified */
class BranchVersioningTagFormatter extends DefaultTagFormatter_1.DefaultTagFormatter {
getRegex(pattern) {
if (/^\/.+\/[i]*$/.test(pattern)) {
const regexEnd = pattern.lastIndexOf('/');
const parsedFlags = pattern.slice(pattern.lastIndexOf('/') + 1);
return new RegExp(pattern.slice(1, regexEnd), parsedFlags);
}
return new RegExp(pattern);
}
constructor(config, branchName) {
super(config);
this.branchName = branchName;
const pattern = config.versionFromBranch === true ?
new RegExp("[0-9]+.[0-9]+$|[0-9]+$") :
this.getRegex(config.versionFromBranch);
const result = pattern.exec(branchName);
let branchVersion;
switch (result === null || result === void 0 ? void 0 : result.length) {
case 1:
branchVersion = result[0];
break;
case 2:
branchVersion = result[1];
break;
default:
throw new Error(`Unable to parse version from branch named '${branchName}' using pattern '${pattern}'`);
}
const versionValues = branchVersion.split('.');
if (versionValues.length > 2) {
throw new Error(`The version string '${branchVersion}' parsed from branch '${branchName}' is invalid. It must be in the format 'major.minor' or 'major'`);
}
this.major = parseInt(versionValues[0]);
if (isNaN(this.major)) {
throw new Error(`The major version '${versionValues[0]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
if (versionValues.length > 1) {
this.minor = parseInt(versionValues[1]);
if (isNaN(this.minor)) {
throw new Error(`The minor version '${versionValues[1]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
}
}
IsValid(tag) {
if (!super.IsValid(tag)) {
return false;
}
const parsed = super.Parse(tag);
if (parsed[0] !== this.major) {
return false;
}
if (this.minor !== undefined && parsed[1] !== this.minor) {
return false;
}
return true;
}
Parse(tag) {
const parsed = super.Parse(tag);
return [this.major, this.minor || parsed[1], parsed[2]];
}
}
exports.BranchVersioningTagFormatter = BranchVersioningTagFormatter;

View file

@ -22,6 +22,9 @@ class DefaultTagFormatter {
return `${this.tagPrefix}*[0-9].*[0-9].*[0-9]`; return `${this.tagPrefix}*[0-9].*[0-9].*[0-9]`;
} }
Parse(tag) { Parse(tag) {
if (tag === '') {
return [0, 0, 0];
}
let tagParts = tag let tagParts = tag
.replace(this.tagPrefix, '<--!PREFIX!-->') .replace(this.tagPrefix, '<--!PREFIX!-->')
.replace(this.namespace, '<--!NAMESPACE!-->') .replace(this.namespace, '<--!NAMESPACE!-->')

View file

@ -64,10 +64,29 @@ function setOutput(versionResult) {
} }
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
function toBool(value) {
if (!value || value.toLowerCase() === 'false') {
return false;
}
else if (value.toLowerCase() === 'true') {
return true;
}
return false;
}
function toStringOrBool(value) {
if (!value || value === 'false') {
return false;
}
if (value === 'true') {
return true;
}
return value;
}
const config = { const config = {
branch: core.getInput('branch'), branch: core.getInput('branch'),
tagPrefix: core.getInput('tag_prefix'), tagPrefix: core.getInput('tag_prefix'),
useBranches: core.getInput('use_branches') === 'true', useBranches: toBool(core.getInput('use_branches')),
versionFromBranch: toStringOrBool(core.getInput('version_from_branch')),
majorPattern: core.getInput('major_pattern'), majorPattern: core.getInput('major_pattern'),
minorPattern: core.getInput('minor_pattern'), minorPattern: core.getInput('minor_pattern'),
majorFlags: core.getInput('major_regexp_flags'), majorFlags: core.getInput('major_regexp_flags'),
@ -75,14 +94,17 @@ function run() {
versionFormat: core.getInput('version_format'), versionFormat: core.getInput('version_format'),
changePath: core.getInput('change_path'), changePath: core.getInput('change_path'),
namespace: core.getInput('namespace'), namespace: core.getInput('namespace'),
bumpEachCommit: core.getInput('bump_each_commit') === 'true', bumpEachCommit: toBool(core.getInput('bump_each_commit')),
searchCommitBody: core.getInput('search_commit_body') === 'true', searchCommitBody: toBool(core.getInput('search_commit_body')),
userFormatType: core.getInput('user_format_type'), userFormatType: core.getInput('user_format_type'),
enablePrereleaseMode: core.getInput('enable_prerelease_mode') === 'true', enablePrereleaseMode: toBool(core.getInput('enable_prerelease_mode')),
bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'), bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'),
debug: core.getInput('debug') === 'true', debug: toBool(core.getInput('debug')),
replay: '' replay: ''
}; };
if (config.useBranches) {
core.warning(`The 'use_branches' input option is deprecated, please see the documentation for more information on how to use branches`);
}
if (config.versionFormat === '' && core.getInput('format') !== '') { if (config.versionFormat === '' && core.getInput('format') !== '') {
core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`); core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`);
config.versionFormat = core.getInput('format'); config.versionFormat = core.getInput('format');

View file

@ -29,5 +29,13 @@ class DefaultCurrentCommitResolver {
return lastCommitAll === ''; return lastCommitAll === '';
}); });
} }
ResolveBranchNameAsync() {
return __awaiter(this, void 0, void 0, function* () {
const branchName = this.branch == 'HEAD' ?
process.env.GITHUB_REF_NAME || (yield (0, CommandRunner_1.cmd)('git', 'rev-parse', '--abbrev-ref', 'HEAD'))
: this.branch;
return branchName.trim();
});
}
} }
exports.DefaultCurrentCommitResolver = DefaultCurrentCommitResolver; exports.DefaultCurrentCommitResolver = DefaultCurrentCommitResolver;

View file

@ -85,8 +85,9 @@ class DefaultLastReleaseResolver {
core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.');
} }
} }
const [major, minor, patch] = tagFormatter.Parse('');
// no release tags yet, use the initial commit as the root // no release tags yet, use the initial commit as the root
return new ReleaseInformation_1.ReleaseInformation(0, 0, 0, '', currentMajor, currentMinor, currentPatch, isTagged); return new ReleaseInformation_1.ReleaseInformation(major, minor, patch, '', currentMajor, currentMinor, currentPatch, isTagged);
} }
// parse the version tag // parse the version tag
const [major, minor, patch] = tagFormatter.Parse(tag); const [major, minor, patch] = tagFormatter.Parse(tag);

View file

@ -4,8 +4,10 @@ export class ActionConfig {
public branch: string = "HEAD"; public branch: string = "HEAD";
/** The prefix to use to identify tags */ /** The prefix to use to identify tags */
public tagPrefix: string = "v"; public tagPrefix: string = "v";
/** Use branches instead of tags */ /** (Deprecated) Use branches instead of tags */
public useBranches: boolean = false; public useBranches: boolean = false;
/** If true, the branch will be used to select the maximum version. */
public versionFromBranch: string | boolean = false;
/** A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression. */ /** A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression. */
public majorPattern: string = "(MAJOR)"; public majorPattern: string = "(MAJOR)";
/** A string which indicates the flags used by the `majorPattern` regular expression. */ /** A string which indicates the flags used by the `majorPattern` regular expression. */

View file

@ -1,4 +1,5 @@
import { CsvUserFormatter } from './formatting/CsvUserFormatter' import { CsvUserFormatter } from './formatting/CsvUserFormatter'
import { BranchVersioningTagFormatter } from './formatting/BranchVersioningTagFormatter'
import { DefaultTagFormatter } from './formatting/DefaultTagFormatter' import { DefaultTagFormatter } from './formatting/DefaultTagFormatter'
import { DefaultVersionFormatter } from './formatting/DefaultVersionFormatter' import { DefaultVersionFormatter } from './formatting/DefaultVersionFormatter'
import { JsonUserFormatter } from './formatting/JsonUserFormatter' import { JsonUserFormatter } from './formatting/JsonUserFormatter'
@ -41,7 +42,12 @@ export class ConfigurationProvider {
public GetVersionFormatter(): VersionFormatter { return new DefaultVersionFormatter(this.config); } public GetVersionFormatter(): VersionFormatter { return new DefaultVersionFormatter(this.config); }
public GetTagFormatter(): TagFormatter { return new DefaultTagFormatter(this.config); } public GetTagFormatter(branchName: string): TagFormatter {
if (this.config.versionFromBranch) {
return new BranchVersioningTagFormatter(this.config, branchName);
}
return new DefaultTagFormatter(this.config);
}
public GetUserFormatter(): UserFormatter { public GetUserFormatter(): UserFormatter {
switch (this.config.userFormatType) { switch (this.config.userFormatType) {

View file

@ -12,12 +12,13 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
const commitsProvider = configurationProvider.GetCommitsProvider(); const commitsProvider = configurationProvider.GetCommitsProvider();
const versionClassifier = configurationProvider.GetVersionClassifier(); const versionClassifier = configurationProvider.GetVersionClassifier();
const versionFormatter = configurationProvider.GetVersionFormatter(); const versionFormatter = configurationProvider.GetVersionFormatter();
const tagFormatter = configurationProvider.GetTagFormatter(); const tagFormatter = configurationProvider.GetTagFormatter(await currentCommitResolver.ResolveBranchNameAsync());
const userFormatter = configurationProvider.GetUserFormatter(); const userFormatter = configurationProvider.GetUserFormatter();
const debugManager = DebugManager.getInstance(); const debugManager = DebugManager.getInstance();
if (await currentCommitResolver.IsEmptyRepoAsync()) { if (await currentCommitResolver.IsEmptyRepoAsync()) {
const versionInfo = new VersionInformation(0, 0, 0, 0, VersionType.None, [], false, false); const versionInfo = new VersionInformation(0, 0, 0, 0, VersionType.None, [], false, false);
return new VersionResult( return new VersionResult(
versionInfo.major, versionInfo.major,
@ -32,7 +33,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
userFormatter.Format('author', []), userFormatter.Format('author', []),
'', '',
'', '',
'0.0.0', tagFormatter.Parse(tagFormatter.Format(versionInfo)).join('.'),
debugManager.getDebugOutput(true) debugManager.getDebugOutput(true)
); );
} }

View file

@ -0,0 +1,74 @@
import { ActionConfig } from '../ActionConfig';
import { DefaultTagFormatter } from './DefaultTagFormatter';
/** Default tag formatter which allows a prefix to be specified */
export class BranchVersioningTagFormatter extends DefaultTagFormatter {
private major: number;
private minor?: number;
private getRegex(pattern: string) {
if (/^\/.+\/[i]*$/.test(pattern)) {
const regexEnd = pattern.lastIndexOf('/');
const parsedFlags = pattern.slice(pattern.lastIndexOf('/') + 1);
return new RegExp(pattern.slice(1, regexEnd), parsedFlags);
}
return new RegExp(pattern);
}
constructor(config: ActionConfig, private branchName: string) {
super(config);
const pattern = config.versionFromBranch === true ?
new RegExp("[0-9]+.[0-9]+$|[0-9]+$") :
this.getRegex(config.versionFromBranch as string);
const result = pattern.exec(branchName);
let branchVersion: string;
switch (result?.length) {
case 1:
branchVersion = result[0];
break;
case 2:
branchVersion = result[1];
break;
default:
throw new Error(`Unable to parse version from branch named '${branchName}' using pattern '${pattern}'`);
}
const versionValues = branchVersion.split('.');
if (versionValues.length > 2) {
throw new Error(`The version string '${branchVersion}' parsed from branch '${branchName}' is invalid. It must be in the format 'major.minor' or 'major'`);
}
this.major = parseInt(versionValues[0]);
if (isNaN(this.major)) {
throw new Error(`The major version '${versionValues[0]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
if (versionValues.length > 1) {
this.minor = parseInt(versionValues[1]);
if (isNaN(this.minor)) {
throw new Error(`The minor version '${versionValues[1]}' parsed from branch '${branchName}' is invalid. It must be a number.`);
}
}
}
override IsValid(tag: string): boolean {
if (!super.IsValid(tag)) {
return false;
}
const parsed = super.Parse(tag);
if (parsed[0] !== this.major) {
return false;
}
if (this.minor !== undefined && parsed[1] !== this.minor) {
return false;
}
return true;
}
override Parse(tag: string): [major: number, minor: number, patch: number] {
const parsed = super.Parse(tag);
return [this.major, this.minor || parsed[1], parsed[2]];
}
}

View file

@ -36,6 +36,10 @@ export class DefaultTagFormatter implements TagFormatter {
public Parse(tag: string): [major: number, minor: number, patch: number] { public Parse(tag: string): [major: number, minor: number, patch: number] {
if(tag === '') {
return [0, 0, 0];
}
let tagParts = tag let tagParts = tag
.replace(this.tagPrefix, '<--!PREFIX!-->') .replace(this.tagPrefix, '<--!PREFIX!-->')
.replace(this.namespace, '<--!NAMESPACE!-->') .replace(this.namespace, '<--!NAMESPACE!-->')

View file

@ -1057,4 +1057,63 @@ test('Debug records and replays expected data', async () => {
}, timeout); }, timeout);
test('Version branch using major version ignores other tags', async () => {
const repo = createTestRepo({ versionFromBranch: true });
repo.makeCommit('Initial Commit');
repo.makeCommit(`Second Commit`);
repo.exec("git checkout -b release/v3");
repo.exec('git tag v3.0.0');
repo.makeCommit(`Third Commit`);
repo.exec('git tag v4.0.0');
repo.makeCommit(`Fourth Commit`);
repo.makeCommit(`Fifth Commit`);
const result = await repo.runAction();
expect(result.formattedVersion).toBe('3.0.1+2');
}, timeout);
test('Versioning from branch always takes version from branch name even without tags', async () => {
const repo = createTestRepo({ versionFromBranch: true });
repo.makeCommit('Initial Commit');
repo.makeCommit(`Second Commit`);
repo.exec("git checkout -b release/v3.2");
repo.makeCommit(`Third Commit`);
repo.makeCommit(`Fourth Commit`);
repo.makeCommit(`Fifth Commit`);
const result = await repo.runAction();
expect(result.formattedVersion).toBe('3.2.1+4');
}, timeout);
test('Prerelease mode does not increment to 1.x.x', async () => {
const repo = createTestRepo({ tagPrefix: 'v', versionFormat: "${major}.${minor}.${patch}-prerelease.${increment}", enablePrereleaseMode: true });
repo.makeCommit('Initial Commit');
repo.exec('git tag v1.0.0');
var result = await repo.runAction();
expect(result.formattedVersion).toBe('1.0.0-prerelease.0');
expect(result.isTagged).toBe(true);
repo.makeCommit('Second Commit');
result = await repo.runAction();
expect(result.formattedVersion).toBe('1.0.1-prerelease.0')
expect(result.isTagged).toBe(false);
repo.makeCommit('Third Commit (MINOR)');
result = await repo.runAction();
expect(result.formattedVersion).toBe('1.1.0-prerelease.0');
expect(result.isTagged).toBe(false);
repo.makeCommit('Fourth Commit (MINOR)');
repo.exec('git tag v1.1.0')
result = await repo.runAction();
expect(result.formattedVersion).toBe('1.1.0-prerelease.1');
expect(result.isTagged).toBe(true);
}, timeout);

View file

@ -37,10 +37,30 @@ function setOutput(versionResult: VersionResult) {
export async function run() { export async function run() {
function toBool(value: string): boolean {
if (!value || value.toLowerCase() === 'false') {
return false;
} else if (value.toLowerCase() === 'true') {
return true;
}
return false;
}
function toStringOrBool(value: string): string | boolean {
if (!value || value === 'false') {
return false;
}
if (value === 'true') {
return true;
}
return value;
}
const config: ActionConfig = { const config: ActionConfig = {
branch: core.getInput('branch'), branch: core.getInput('branch'),
tagPrefix: core.getInput('tag_prefix'), tagPrefix: core.getInput('tag_prefix'),
useBranches: core.getInput('use_branches') === 'true', useBranches: toBool(core.getInput('use_branches')),
versionFromBranch: toStringOrBool(core.getInput('version_from_branch')),
majorPattern: core.getInput('major_pattern'), majorPattern: core.getInput('major_pattern'),
minorPattern: core.getInput('minor_pattern'), minorPattern: core.getInput('minor_pattern'),
majorFlags: core.getInput('major_regexp_flags'), majorFlags: core.getInput('major_regexp_flags'),
@ -48,15 +68,19 @@ export async function run() {
versionFormat: core.getInput('version_format'), versionFormat: core.getInput('version_format'),
changePath: core.getInput('change_path'), changePath: core.getInput('change_path'),
namespace: core.getInput('namespace'), namespace: core.getInput('namespace'),
bumpEachCommit: core.getInput('bump_each_commit') === 'true', bumpEachCommit: toBool(core.getInput('bump_each_commit')),
searchCommitBody: core.getInput('search_commit_body') === 'true', searchCommitBody: toBool(core.getInput('search_commit_body')),
userFormatType: core.getInput('user_format_type'), userFormatType: core.getInput('user_format_type'),
enablePrereleaseMode: core.getInput('enable_prerelease_mode') === 'true', enablePrereleaseMode: toBool(core.getInput('enable_prerelease_mode')),
bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'), bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern'),
debug: core.getInput('debug') === 'true', debug: toBool(core.getInput('debug')),
replay: '' replay: ''
}; };
if (config.useBranches) {
core.warning(`The 'use_branches' input option is deprecated, please see the documentation for more information on how to use branches`);
}
if (config.versionFormat === '' && core.getInput('format') !== '') { if (config.versionFormat === '' && core.getInput('format') !== '') {
core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`); core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`);
config.versionFormat = core.getInput('format'); config.versionFormat = core.getInput('format');

View file

@ -10,4 +10,10 @@ export interface CurrentCommitResolver {
* @returns True if the repository is empty * @returns True if the repository is empty
*/ */
IsEmptyRepoAsync(): Promise<boolean>; IsEmptyRepoAsync(): Promise<boolean>;
/**
* Returns the current branch
* @returns The current branch
*/
ResolveBranchNameAsync(): Promise<string>;
} }

View file

@ -22,4 +22,14 @@ export class DefaultCurrentCommitResolver implements CurrentCommitResolver {
let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim(); let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim();
return lastCommitAll === ''; return lastCommitAll === '';
} }
public async ResolveBranchNameAsync(): Promise<string> {
const branchName =
this.branch == 'HEAD' ?
process.env.GITHUB_REF_NAME || await cmd('git', 'rev-parse', '--abbrev-ref', 'HEAD')
: this.branch;
return branchName.trim();
}
} }

View file

@ -68,8 +68,9 @@ export class DefaultLastReleaseResolver implements LastReleaseResolver {
core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.');
} }
} }
const [major, minor, patch] = tagFormatter.Parse('');
// no release tags yet, use the initial commit as the root // no release tags yet, use the initial commit as the root
return new ReleaseInformation(0, 0, 0, '', currentMajor, currentMinor, currentPatch, isTagged); return new ReleaseInformation(major, minor, patch, '', currentMajor, currentMinor, currentPatch, isTagged);
} }
// parse the version tag // parse the version tag