Skip to content

Commit

Permalink
ARCH-1919 - Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielle-casella-adams committed Oct 13, 2023
1 parent 35669b4 commit 878ca9e
Show file tree
Hide file tree
Showing 10 changed files with 845 additions and 101 deletions.
711 changes: 668 additions & 43 deletions .github/workflows/build-and-review-pr.yml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .github/workflows/increment-version-on-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ on:
# if this action should be incremented and if new tags should be pushed to the repo based
# on the same criteria used in the build-and-review-pr.yml workflow.


# ------------------------------------------------------------------------------------
# NOTE: This repo duplicates the reusable increment workflow in im-open/.github that
# the rest of the actions use. If changes are needed in this workflow they
# should also be made in im-open/.github. This workflow is duplicated because
# it uses the local copy of itself in the workflow which allows us to test the
# increment build with git-version-lite changes before we merge those changes.
# ------------------------------------------------------------------------------------

jobs:
increment-version:
runs-on: ubuntu-latest
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This template can be used to calculate a release or pre-release version.
- [Source Code Changes](#source-code-changes)
- [Recompiling Manually](#recompiling-manually)
- [Updating the README.md](#updating-the-readmemd)
- [Tests](#tests)
- [Code of Conduct](#code-of-conduct)
- [License](#license)

Expand Down Expand Up @@ -137,6 +138,7 @@ When creating PRs, please review the following guidelines:
- [ ] At least one of the commit messages contains the appropriate `+semver:` keywords listed under [Incrementing the Version] for major and minor increments.
- [ ] The action has been recompiled. See [Recompiling Manually] for details.
- [ ] The README.md has been updated with the latest version of the action. See [Updating the README.md] for details.
- [ ] Any tests in the [build-and-review-pr] workflow are passing

### Incrementing the Version

Expand Down Expand Up @@ -171,6 +173,10 @@ npm run build

If changes are made to the action's [source code], the [usage examples] section of this file should be updated with the next version of the action. Each instance of this action should be updated. This helps users know what the latest tag is without having to navigate to the Tags page of the repository. See [Incrementing the Version] for details on how to determine what the next version will be or consult the first workflow run for the PR which will also calculate the next version.

### Tests

The build and review PR workflow includes tests which are linked to a status check. That status check needs to succeed before a PR is merged to the default branch. When a PR comes from a branch, there should not be any issues running the tests. When a PR comes from a fork, tests may not have the required permissions or access to run since the `GITHUB_TOKEN` only has `read` access set for all scopes. Also, forks cannot access other secrets in the repository. In these scenarios, a fork may need to be merged into an intermediate branch by the repository owners to ensure the tests run successfully prior to merging to the default branch.

## Code of Conduct

This project has adopted the [im-open's Code of Conduct](https://github.com/im-open/.github/blob/main/CODE_OF_CONDUCT.md).
Expand All @@ -189,4 +195,3 @@ Copyright © 2023, Extend Health, LLC. Code released under the [MIT license]
[increment-version-on-merge]: ./.github/workflows/increment-version-on-merge.yml
[esbuild]: https://esbuild.github.io/getting-started/#bundling-for-node
[git-version-lite]: https://github.com/im-open/git-version-lite
[create a ref]: https://docs.github.com/en/rest/reference/git#create-a-reference
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: An action to calculate the next tag for the repository based on com
inputs:
calculate-prerelease-version:
description: 'Flag indicating whether to calculate a pre-release version rather than a release version. Accepts: true|false.'
required: true
required: false
default: 'false'

branch-name:
Expand All @@ -14,17 +14,17 @@ inputs:

tag-prefix:
description: 'By default the action strips the prefixes off, but any value provided here will be prepended to the next calculated version.'
required: true
required: false
default: 'v'

fallback-to-no-prefix-search:
description: 'Flag indicating whether it should fallback to a prefix-less search if no tags are found with the current prefix. Helpful when starting to use prefixes with tags. Accepted values: true|false.'
required: true
required: false
default: 'true'

default-release-type:
description: 'The default release type that should be used when no tags are detected. Defaults to major. Accepted values: major|minor|patch'
required: true
required: false
default: 'major'

outputs:
Expand Down
58 changes: 33 additions & 25 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5176,9 +5176,13 @@ Setting Release Type to '${releaseType}' based on empty base commit.`);
The base commit was found. The prior release version is: ${priorReleaseVersion}`);
releaseType = determineReleaseTypeFromGitLog(baseCommit.abbreviatedCommitHash, 'HEAD');
}
const priorSemver = new SemVer(priorReleaseVersion);
const nextSemver = new SemVer(semver.inc(priorReleaseVersion, releaseType));
return {
priorVersion: new SemVer(priorReleaseVersion),
nextVersion: new SemVer(semver.inc(priorReleaseVersion, releaseType))
priorVersion: priorSemver.toString(),
nextPatch: nextSemver.toString(),
nextMinor: `${nextSemver.major}.${nextSemver.minor}`,
nextMajor: `${nextSemver.major}`
};
}
function nextPrereleaseVersion2(
Expand All @@ -5193,8 +5197,8 @@ The base commit was found. The prior release version is: ${priorReleaseVersion}
} catch (error) {
core2.info(`An error occurred retrieving the tags for the repository: ${error.message}`);
}
let currentHeadCommit = git.commitMetadata('HEAD');
let formattedDate = dateToPreReleaseComponent(currentHeadCommit.committerDate);
const currentHeadCommit = git.commitMetadata('HEAD');
const formattedDate = dateToPreReleaseComponent(currentHeadCommit.committerDate);
let priorReleaseVersion;
let releaseType;
if (baseCommit === null) {
Expand All @@ -5210,12 +5214,16 @@ Setting Release Type to '${releaseType}' based on empty base commit.`);
The base commit was found. The prior release version is: ${priorReleaseVersion}`);
releaseType = determineReleaseTypeFromGitLog(baseCommit.abbreviatedCommitHash, 'HEAD');
}
let nextReleaseVersion3 = semver.inc(priorReleaseVersion, releaseType);
let prereleaseVersion = `${nextReleaseVersion3}-${label}.${formattedDate}`;
const nextReleaseVersion3 = semver.inc(priorReleaseVersion, releaseType);
const prereleaseVersion = `${nextReleaseVersion3}-${label}.${formattedDate}`;
core2.info(`Cleaned Branch Name: '${label}'`);
const priorSemver = new SemVer(priorReleaseVersion);
const nextSemver = new SemVer(prereleaseVersion);
return {
priorVersion: new SemVer(priorReleaseVersion),
nextVersion: new SemVer(prereleaseVersion)
priorVersion: priorSemver.toString(),
nextPatch: nextSemver.toString(),
nextMinor: `${nextSemver.major}.${nextSemver.minor}`,
nextMajor: `${nextSemver.major}`
};
}
module2.exports = {
Expand All @@ -5239,6 +5247,16 @@ var tagPrefix = core.getInput('tag-prefix');
if (tagPrefix.toLowerCase() == 'none') {
tagPrefix = '';
}
function setTheOutputs(name, value, tagPrefix2) {
const valueWithTag = `${tagPrefix2}${value}`;
core.setOutput(name, valueWithTag);
core.exportVariable(name, valueWithTag);
core.info(`${name}: ${valueWithTag}`);
const noPrefixName = `${name}_NO_PREFIX`;
core.setOutput(noPrefixName, value);
core.exportVariable(noPrefixName, value);
core.info(`${noPrefixName}: ${value}`);
}
async function run() {
try {
const expectedReleaseTypes = ['major', 'minor', 'patch'];
Expand All @@ -5261,23 +5279,13 @@ async function run() {
core.info(`Calculating a release version...`);
versionToBuild = nextReleaseVersion(defaultReleaseType, tagPrefix, fallbackToNoPrefixSearch);
}
const { nextVersion, priorVersion } = versionToBuild;
const outputVersionEntries = Object.entries({
NEXT_VERSION: nextVersion.toString(),
NEXT_MINOR_VERSION: `${nextVersion.major}.${nextVersion.minor}`,
NEXT_MAJOR_VERSION: nextVersion.major,
PRIOR_VERSION: priorVersion.toString()
});
core.info(`
Finished examining the git history. The following outputs will be set:`);
[
...outputVersionEntries.map(([name, value]) => [name, `${tagPrefix}${value}`]),
...outputVersionEntries.map(([name, value]) => [`${name}_NO_PREFIX`, value])
].forEach(entry => {
core.setOutput(...entry);
core.exportVariable(...entry);
console.info(...entry);
});
console.log('version to build:');
console.log(versionToBuild);
const { nextPatch, nextMinor, nextMajor, priorVersion } = versionToBuild;
setTheOutputs('PRIOR_VERSION', priorVersion, tagPrefix);
setTheOutputs('NEXT_VERSION', nextPatch, tagPrefix);
setTheOutputs('NEXT_MINOR_VERSION', nextMinor, tagPrefix);
setTheOutputs('NEXT_MAJOR_VERSION', nextMajor, tagPrefix);
} catch (error) {
const versionTxt = calculatePrereleaseVersion ? 'pre-release' : 'release';
core.setFailed(
Expand Down
38 changes: 21 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ if (tagPrefix.toLowerCase() == 'none') {
tagPrefix = ''; //action.yml sets it to v by default so the user wouldn't be able to set an empty string themselves.
}

function setTheOutputs(name, value, tagPrefix) {
// Set the regular version (it has a tag prefix)
const valueWithTag = `${tagPrefix}${value}`;
core.setOutput(name, valueWithTag);
core.exportVariable(name, valueWithTag);
core.info(`${name}: ${valueWithTag}`);

// Set the version without the tag prefix
const noPrefixName = `${name}_NO_PREFIX`;
core.setOutput(noPrefixName, value);
core.exportVariable(noPrefixName, value);
core.info(`${noPrefixName}: ${value}`);
}

async function run() {
try {
const expectedReleaseTypes = ['major', 'minor', 'patch'];
Expand Down Expand Up @@ -44,24 +58,14 @@ async function run() {
versionToBuild = nextReleaseVersion(defaultReleaseType, tagPrefix, fallbackToNoPrefixSearch);
}

const { nextVersion, priorVersion } = versionToBuild;

const outputVersionEntries = Object.entries({
NEXT_VERSION: nextVersion.toString(),
NEXT_MINOR_VERSION: `${nextVersion.major}.${nextVersion.minor}`,
NEXT_MAJOR_VERSION: nextVersion.major,
PRIOR_VERSION: priorVersion.toString()
});
console.log('version to build:');
console.log(versionToBuild);

core.info(`\nFinished examining the git history. The following outputs will be set:`);
[
...outputVersionEntries.map(([name, value]) => [name, `${tagPrefix}${value}`]),
...outputVersionEntries.map(([name, value]) => [`${name}_NO_PREFIX`, value])
].forEach(entry => {
core.setOutput(...entry);
core.exportVariable(...entry);
console.info(...entry);
});
const { nextPatch, nextMinor, nextMajor, priorVersion } = versionToBuild;
setTheOutputs('PRIOR_VERSION', priorVersion, tagPrefix);
setTheOutputs('NEXT_VERSION', nextPatch, tagPrefix);
setTheOutputs('NEXT_MINOR_VERSION', nextMinor, tagPrefix);
setTheOutputs('NEXT_MAJOR_VERSION', nextMajor, tagPrefix);
} catch (error) {
const versionTxt = calculatePrereleaseVersion ? 'pre-release' : 'release';
core.setFailed(
Expand Down
32 changes: 21 additions & 11 deletions src/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,17 @@ function dateToPreReleaseComponent(input) {

/**
* @typedef {{
* priorVersion: SemVer,
* nextVersion: SemVer
* priorVersion: string,
* nextPatch: string,
* nextMinor: string,
* nextMajor: string
* }} ReleaseBucket
*/

/**
* @param defaultReleaseType {string} The default release type to use if no tags are detected
* @param tagPrefix {string} The value to pre-pend to the calculated release
* @returns {ReleaseBucket} a SemVer next and prior versions based on the Git history since the last tagged release
* @returns {ReleaseBucket} next and prior versions based on the Git history since the last tagged release
*/
function nextReleaseVersion(defaultReleaseType, tagPrefix, fallbackToNoPrefixSearch) {
let baseCommit;
Expand All @@ -155,9 +157,13 @@ function nextReleaseVersion(defaultReleaseType, tagPrefix, fallbackToNoPrefixSea
releaseType = determineReleaseTypeFromGitLog(baseCommit.abbreviatedCommitHash, 'HEAD');
}

const priorSemver = new SemVer(priorReleaseVersion);
const nextSemver = new SemVer(semver.inc(priorReleaseVersion, releaseType));
return {
priorVersion: new SemVer(priorReleaseVersion),
nextVersion: new SemVer(semver.inc(priorReleaseVersion, releaseType))
priorVersion: priorSemver.toString(),
nextPatch: nextSemver.toString(),
nextMinor: `${nextSemver.major}.${nextSemver.minor}`,
nextMajor: `${nextSemver.major}`
};
}

Expand All @@ -175,9 +181,9 @@ function nextPrereleaseVersion(label, defaultReleaseType, tagPrefix, fallbackToN
} catch (error) {
core.info(`An error occurred retrieving the tags for the repository: ${error.message}`);
}
let currentHeadCommit = git.commitMetadata('HEAD');

let formattedDate = dateToPreReleaseComponent(currentHeadCommit.committerDate);
const currentHeadCommit = git.commitMetadata('HEAD');
const formattedDate = dateToPreReleaseComponent(currentHeadCommit.committerDate);

let priorReleaseVersion;
let releaseType;
Expand All @@ -193,13 +199,17 @@ function nextPrereleaseVersion(label, defaultReleaseType, tagPrefix, fallbackToN
core.info(`\nThe base commit was found. The prior release version is: ${priorReleaseVersion}`);
releaseType = determineReleaseTypeFromGitLog(baseCommit.abbreviatedCommitHash, 'HEAD');
}
let nextReleaseVersion = semver.inc(priorReleaseVersion, releaseType);
let prereleaseVersion = `${nextReleaseVersion}-${label}.${formattedDate}`;
const nextReleaseVersion = semver.inc(priorReleaseVersion, releaseType);
const prereleaseVersion = `${nextReleaseVersion}-${label}.${formattedDate}`;
core.info(`Cleaned Branch Name: '${label}'`);

const priorSemver = new SemVer(priorReleaseVersion);
const nextSemver = new SemVer(prereleaseVersion);
return {
priorVersion: new SemVer(priorReleaseVersion),
nextVersion: new SemVer(prereleaseVersion)
priorVersion: priorSemver.toString(),
nextPatch: nextSemver.toString(),
nextMinor: `${nextSemver.major}.${nextSemver.minor}`,
nextMajor: `${nextSemver.major}`
};
}

Expand Down
38 changes: 38 additions & 0 deletions test/assert-string-contains-substring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

name=''
string=''
substring=''


for arg in "$@"; do
case $arg in
--name)
name=$2
shift # Remove argument --name from `$@`
shift # Remove argument value from `$@`
;;
--string)
string=$2
shift # Remove argument --expected from `$@`
shift # Remove argument value from `$@`
;;
--substring)
substring=$2
shift # Remove argument --actual from `$@`
shift # Remove argument value from `$@`
;;

esac
done

echo "
Full value of $name: '$string'
Substring for $name: '$substring'"

if [[ $string == *"$substring"* ]]; then
echo "The substring was found in $name"
else
echo "The substring was not found in $name"
exit 1
fi
10 changes: 10 additions & 0 deletions test/setup/reset-env-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

echo "PRIOR_VERSION=" >> $GITHUB_ENV
echo "NEXT_VERSION=" >> $GITHUB_ENV
echo "NEXT_MINOR_VERSION=" >> $GITHUB_ENV
echo "NEXT_MAJOR_VERSION=" >> $GITHUB_ENV
echo "PRIOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_MINOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_MAJOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV
35 changes: 35 additions & 0 deletions test/setup/reset-repo-and-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

commitMessage=''

for arg in "$@"; do
case $arg in
--message)
commitMessage=$2
shift # Remove argument --name from `$@`
shift # Remove argument value from `$@`
;;
esac
done

echo "Remove the last commit message"
git reset --soft HEAD~1

echo "
Make a message only commit:";
git commit --allow-empty -m "$commitMessage"

echo "
Print the last five commits:";
git log -5 --oneline

echo "
Reset the environment variables that git-version-lite will set";
echo "PRIOR_VERSION=" >> $GITHUB_ENV
echo "NEXT_VERSION=" >> $GITHUB_ENV
echo "NEXT_MINOR_VERSION=" >> $GITHUB_ENV
echo "NEXT_MAJOR_VERSION=" >> $GITHUB_ENV
echo "PRIOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_MINOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV
echo "NEXT_MAJOR_VERSION_NO_PREFIX=" >> $GITHUB_ENV

0 comments on commit 878ca9e

Please sign in to comment.