mirror of
https://github.com/PaulHatch/semantic-version.git
synced 2026-04-13 19:04:18 +00:00
Add new branch versioning (MINOR)
This commit is contained in:
parent
d3c0da227f
commit
61963e734d
21 changed files with 447 additions and 34 deletions
|
|
@ -4,8 +4,10 @@ export class ActionConfig {
|
|||
public branch: string = "HEAD";
|
||||
/** The prefix to use to identify tags */
|
||||
public tagPrefix: string = "v";
|
||||
/** Use branches instead of tags */
|
||||
/** (Deprecated) Use branches instead of tags */
|
||||
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. */
|
||||
public majorPattern: string = "(MAJOR)";
|
||||
/** A string which indicates the flags used by the `majorPattern` regular expression. */
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { CsvUserFormatter } from './formatting/CsvUserFormatter'
|
||||
import { BranchVersioningTagFormatter } from './formatting/BranchVersioningTagFormatter'
|
||||
import { DefaultTagFormatter } from './formatting/DefaultTagFormatter'
|
||||
import { DefaultVersionFormatter } from './formatting/DefaultVersionFormatter'
|
||||
import { JsonUserFormatter } from './formatting/JsonUserFormatter'
|
||||
|
|
@ -41,7 +42,12 @@ export class ConfigurationProvider {
|
|||
|
||||
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 {
|
||||
switch (this.config.userFormatType) {
|
||||
|
|
|
|||
|
|
@ -12,12 +12,13 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
const commitsProvider = configurationProvider.GetCommitsProvider();
|
||||
const versionClassifier = configurationProvider.GetVersionClassifier();
|
||||
const versionFormatter = configurationProvider.GetVersionFormatter();
|
||||
const tagFormatter = configurationProvider.GetTagFormatter();
|
||||
const tagFormatter = configurationProvider.GetTagFormatter(await currentCommitResolver.ResolveBranchNameAsync());
|
||||
const userFormatter = configurationProvider.GetUserFormatter();
|
||||
|
||||
const debugManager = DebugManager.getInstance();
|
||||
|
||||
if (await currentCommitResolver.IsEmptyRepoAsync()) {
|
||||
|
||||
const versionInfo = new VersionInformation(0, 0, 0, 0, VersionType.None, [], false, false);
|
||||
return new VersionResult(
|
||||
versionInfo.major,
|
||||
|
|
@ -32,7 +33,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
userFormatter.Format('author', []),
|
||||
'',
|
||||
'',
|
||||
'0.0.0',
|
||||
tagFormatter.Parse(tagFormatter.Format(versionInfo)).join('.'),
|
||||
debugManager.getDebugOutput(true)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
74
src/formatting/BranchVersioningTagFormatter.ts
Normal file
74
src/formatting/BranchVersioningTagFormatter.ts
Normal 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]];
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,10 @@ export class DefaultTagFormatter implements TagFormatter {
|
|||
|
||||
public Parse(tag: string): [major: number, minor: number, patch: number] {
|
||||
|
||||
if(tag === '') {
|
||||
return [0, 0, 0];
|
||||
}
|
||||
|
||||
let tagParts = tag
|
||||
.replace(this.tagPrefix, '<--!PREFIX!-->')
|
||||
.replace(this.namespace, '<--!NAMESPACE!-->')
|
||||
|
|
|
|||
|
|
@ -1057,4 +1057,63 @@ test('Debug records and replays expected data', async () => {
|
|||
|
||||
}, 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);
|
||||
34
src/main.ts
34
src/main.ts
|
|
@ -37,10 +37,30 @@ function setOutput(versionResult: VersionResult) {
|
|||
|
||||
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 = {
|
||||
branch: core.getInput('branch'),
|
||||
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'),
|
||||
minorPattern: core.getInput('minor_pattern'),
|
||||
majorFlags: core.getInput('major_regexp_flags'),
|
||||
|
|
@ -48,15 +68,19 @@ export async function run() {
|
|||
versionFormat: core.getInput('version_format'),
|
||||
changePath: core.getInput('change_path'),
|
||||
namespace: core.getInput('namespace'),
|
||||
bumpEachCommit: core.getInput('bump_each_commit') === 'true',
|
||||
searchCommitBody: core.getInput('search_commit_body') === 'true',
|
||||
bumpEachCommit: toBool(core.getInput('bump_each_commit')),
|
||||
searchCommitBody: toBool(core.getInput('search_commit_body')),
|
||||
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'),
|
||||
debug: core.getInput('debug') === 'true',
|
||||
debug: toBool(core.getInput('debug')),
|
||||
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') !== '') {
|
||||
core.warning(`The 'format' input is deprecated, use 'versionFormat' instead`);
|
||||
config.versionFormat = core.getInput('format');
|
||||
|
|
|
|||
|
|
@ -10,4 +10,10 @@ export interface CurrentCommitResolver {
|
|||
* @returns True if the repository is empty
|
||||
*/
|
||||
IsEmptyRepoAsync(): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Returns the current branch
|
||||
* @returns The current branch
|
||||
*/
|
||||
ResolveBranchNameAsync(): Promise<string>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,4 +22,14 @@ export class DefaultCurrentCommitResolver implements CurrentCommitResolver {
|
|||
let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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.');
|
||||
}
|
||||
}
|
||||
const [major, minor, patch] = tagFormatter.Parse('');
|
||||
// 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue