Add support for using branches instead of tags

This commit is contained in:
Paul Hatcherian 2022-04-15 13:12:45 -04:00
parent cd16d71443
commit 3a712b126c
12 changed files with 235 additions and 101 deletions

View file

@ -4,6 +4,8 @@ export class ActionConfig {
public branch: string = "HEAD";
/** The prefix to use to identify tags */
public tagPrefix: string = "v";
/** Use branches instead of tags */
public useBranches: 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, if present in a git commit, indicates that a change represents a minor (feature) change. Wrap with '/' to match using a regular expression. */

View file

@ -11,7 +11,7 @@ import { DefaultCommitsProvider } from './providers/DefaultCommitsProvider'
import { DefaultCurrentCommitResolver } from './providers/DefaultCurrentCommitResolver'
import { DefaultVersionClassifier } from './providers/DefaultVersionClassifier'
import { LastReleaseResolver } from './providers/LastReleaseResolver'
import { TagLastReleaseResolver } from './providers/TagLastReleaseResolver'
import { DefaultLastReleaseResolver } from './providers/DefaultLastReleaseResolver'
import { VersionClassifier } from './providers/VersionClassifier'
import { BumpAlwaysVersionClassifier } from './providers/BumpAlwaysVersionClassifier'
import { ActionConfig } from './ActionConfig';
@ -26,7 +26,7 @@ export class ConfigurationProvider {
public GetCurrentCommitResolver(): CurrentCommitResolver { return new DefaultCurrentCommitResolver(this.config); }
public GetLastReleaseResolver(): LastReleaseResolver { return new TagLastReleaseResolver(this.config); }
public GetLastReleaseResolver(): LastReleaseResolver { return new DefaultLastReleaseResolver(this.config); }
public GetCommitsProvider(): CommitsProvider { return new DefaultCommitsProvider(this.config); }

View file

@ -546,4 +546,37 @@ test('Correct tag is detected when versions pass 10s place', async () => {
const result = await repo.runAction();
expect(result.versionTag).toBe('v10.15.1');
}, 15000);
test('Tags on unmerged branches are not considered', async () => {
const repo = createTestRepo({ tagPrefix: 'v' }); // 0.0.0
repo.makeCommit('Initial Commit');
repo.makeCommit('Commit 1');
repo.exec('git checkout -b feature/branch1');
repo.makeCommit('Commit 2');
repo.exec('git tag v2.0.0');
repo.makeCommit('Commit 3');
repo.exec('git checkout master');
repo.makeCommit('Commit 4');
repo.exec('git tag v1.0.0');
repo.makeCommit('Commit 5');
const result = await repo.runAction();
expect(result.versionTag).toBe('v1.0.1');
}, 15000);
test('Can use branches instead of tags', async () => {
const repo = createTestRepo({ tagPrefix: 'release/', useBranches: true }); // 0.0.0
repo.makeCommit('Initial Commit');
repo.makeCommit('Commit 1');
repo.exec('git checkout -b release/1.0.0');
repo.makeCommit('Commit 2');
repo.exec('git checkout master');
repo.exec('git merge release/1.0.0');
repo.makeCommit('Commit 3');
const result = await repo.runAction();
expect(result.versionTag).toBe('release/1.0.1');
}, 15000);

View file

@ -36,6 +36,7 @@ export async function run() {
const config: ActionConfig = {
branch: core.getInput('branch'),
tagPrefix: core.getInput('tag_prefix'),
useBranches: core.getInput('use_branches') === 'true',
majorPattern: core.getInput('major_pattern'),
minorPattern: core.getInput('minor_pattern'),
versionFormat: core.getInput('version_format'),

View file

@ -5,12 +5,14 @@ import { ReleaseInformation } from "./ReleaseInformation";
import { ActionConfig } from "../ActionConfig";
import * as core from '@actions/core';
export class TagLastReleaseResolver implements LastReleaseResolver {
export class DefaultLastReleaseResolver implements LastReleaseResolver {
private changePath: string;
private useBranches: boolean;
constructor(config: ActionConfig) {
this.changePath = config.changePath;
this.useBranches = config.useBranches;
}
async ResolveAsync(current: string, tagFormatter: TagFormatter): Promise<ReleaseInformation> {
@ -23,14 +25,15 @@ export class TagLastReleaseResolver implements LastReleaseResolver {
let tag = '';
try {
const refPrefixPattern = this.useBranches ? 'refs/heads/' : 'refs/tags/';
if (!!currentTag) {
// If we already have the current branch tagged, we are checking for the previous one
// so that we will have an accurate increment (assuming the new tag is the expected one)
const command = `git for-each-ref --count=2 --sort=-v:*refname --format=%(refname:short) --merged=${current} refs/tags/${releasePattern}`;
const command = `git for-each-ref --count=2 --sort=-v:*refname --format=%(refname:short) --merged=${current} ${refPrefixPattern}${releasePattern}`;
tag = await cmd(command);
tag = tag.split('\n').at(-1) || '';
} else {
const command = `git for-each-ref --count=1 --sort=-v:*refname --format=%(refname:short) --merged=${current} refs/tags/${releasePattern}`;
const command = `git for-each-ref --count=1 --sort=-v:*refname --format=%(refname:short) --merged=${current} ${refPrefixPattern}${releasePattern}`;
tag = await cmd(command);
}