Skip to content

Commit

Permalink
Merge pull request #13 from PaulHatch/feature/bump-commit
Browse files Browse the repository at this point in the history
Add support to bump the version on each commit (MINOR)
  • Loading branch information
PaulHatch authored Dec 20, 2020
2 parents 80758ec + 1412b42 commit 0c009e3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ inputs:
description: "If false, only full versions, i.e. 'v1.0.0', will be supported as tags. If true, tags will support truncated minor and patch versions such as 'v1' (default)."
required: true
default: "true"
bump_each_commit:
description: "If true, every commit will be treated as a bump to the version."
required: true
default: "false"
outputs:
major:
description: "Current major number"
Expand Down
19 changes: 19 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ const eol = '\n';
const tagPrefix = core.getInput('tag_prefix') || '';
const namespace = core.getInput('namespace') || '';
const shortTags = core.getInput('short_tags') === 'true';
const bumpEachCommit = core.getInput('bump_each_commit') === 'true';

const cmd = async (command, ...args) => {
let output = '', errors = '';
Expand Down Expand Up @@ -1199,6 +1200,24 @@ async function run() {
.split(eol)
.reverse();

if (bumpEachCommit) {
core.info(history)
history.forEach(line => {
if (line.includes(majorPattern)) {
major += 1;
minor = 0;
patch = 0;
} else if (line.includes(minorPattern)) {
minor += 1;
patch = 0;
} else {
patch += 1;
}
});
setOutput(major, minor, patch, increment, changed, branch, namespace);
return;
}

// Discover the change time from the history log by finding the oldest log
// that could set the version.

Expand Down
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const eol = '\n';
const tagPrefix = core.getInput('tag_prefix') || '';
const namespace = core.getInput('namespace') || '';
const shortTags = core.getInput('short_tags') === 'true';
const bumpEachCommit = core.getInput('bump_each_commit') === 'true';

const cmd = async (command, ...args) => {
let output = '', errors = '';
Expand Down Expand Up @@ -178,6 +179,24 @@ async function run() {
.split(eol)
.reverse();

if (bumpEachCommit) {
core.info(history)
history.forEach(line => {
if (line.includes(majorPattern)) {
major += 1;
minor = 0;
patch = 0;
} else if (line.includes(minorPattern)) {
minor += 1;
patch = 0;
} else {
patch += 1;
}
});
setOutput(major, minor, patch, increment, changed, branch, namespace);
return;
}

// Discover the change time from the history log by finding the oldest log
// that could set the version.

Expand Down
42 changes: 41 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const defaultInputs = {
major_pattern: "(MAJOR)",
minor_pattern: "(MINOR)",
format: "${major}.${minor}.${patch}",
short_tags: true
short_tags: true,
bump_each_commit: false
};

// Creates a randomly named git repository and returns a function to execute commands in it
Expand Down Expand Up @@ -461,5 +462,44 @@ test('Short tag can be switched off', () => {

expect(result).toMatch('Version is 0.0.1+2');

repo.clean();
});

test('Bump each commit works', () => {
const repo = createTestRepo({ tag_prefix: '', bump_each_commit: true }); // 0.0.0

expect(repo.runAction()).toMatch('Version is 0.0.0+0');
repo.makeCommit('Initial Commit');
expect(repo.runAction()).toMatch('Version is 0.0.1+0');
repo.makeCommit('Second Commit');
expect(repo.runAction()).toMatch('Version is 0.0.2+0');
repo.makeCommit('Third Commit');
expect(repo.runAction()).toMatch('Version is 0.0.3+0');
repo.makeCommit('Fourth Commit (MINOR)');
expect(repo.runAction()).toMatch('Version is 0.1.0+0');
repo.makeCommit('Fifth Commit');
expect(repo.runAction()).toMatch('Version is 0.1.1+0');
repo.makeCommit('Sixth Commit (MAJOR)');
expect(repo.runAction()).toMatch('Version is 1.0.0+0');
repo.makeCommit('Seventh Commit');
expect(repo.runAction()).toMatch('Version is 1.0.1+0');

repo.clean();
});

test('Bump each commit picks up tags', () => {
const repo = createTestRepo({ tag_prefix: '', bump_each_commit: true }); // 0.0.0

expect(repo.runAction()).toMatch('Version is 0.0.0+0');
repo.makeCommit('Initial Commit');
expect(repo.runAction()).toMatch('Version is 0.0.1+0');
repo.makeCommit('Second Commit');
expect(repo.runAction()).toMatch('Version is 0.0.2+0');
repo.makeCommit('Third Commit');
repo.exec('git tag 3.0.0');
expect(repo.runAction()).toMatch('Version is 3.0.0+0');
repo.makeCommit('Fourth Commit');
expect(repo.runAction()).toMatch('Version is 3.0.1+0');

repo.clean();
});
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ it will be given the new version if the build were to be retriggered, for exampl
# Indicate whether short tags like 'v1' should be supported. If false only full
# tags like 'v1.0.0' will be recognized.
short_tags: true
# If this is set to true, *every* commit will be treated as a new version.
bump_each_commit: false
```
## Using Multiple Versions in the Same Repository
Expand Down

0 comments on commit 0c009e3

Please sign in to comment.