Skip to content

Commit

Permalink
Merge pull request #1 from GauzStudio/fred-update-semantic-versioning
Browse files Browse the repository at this point in the history
Fred update semantic versioning
  • Loading branch information
FredericoGauz authored Mar 3, 2021
2 parents e8084f9 + 18cf38f commit 893d7c0
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 182 deletions.
48 changes: 22 additions & 26 deletions .github/actions/bumpVersion/bumpVersion.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const {exec} = require('child_process');
const fs = require('fs');

// const fs = require('fs');
const core = require('@actions/core');
const github = require('@actions/github');
const functions = require('./functions');

// Use Github Actions' default environment variables to get repo information
// https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables
const [repoOwner, repoName] = process.env.GITHUB_REPOSITORY.split('/');
// const [repoOwner, repoName] = process.env.GITHUB_REPOSITORY.split('/');

const MAX_RETRIES = 10;
let errCount = 0;
Expand All @@ -29,38 +31,32 @@ do {
`retryCount: ${++errCount}`,
);
shouldRetry = true;
const {version} = JSON.parse(fs.readFileSync('./package.json'));
const currentPatchVersion = `v${version.slice(0, -4)}`;
console.log('Current patch version:', currentPatchVersion);

// Get the highest build version git tag from the repo
// const { version } = JSON.parse(fs.readFileSync('./package.json'));

// const currentPatchVersion = `v${version.slice(0, -4)}`;
// console.log('Current patch version:', currentPatchVersion);

console.log('Fetching tags from github...');
const octokit = github.getOctokit(core.getInput('GITHUB_TOKEN', {required: true}));
octokit.repos.listTags({
owner: repoOwner,
repo: repoName,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
})
.then((response) => {
const tags = response.data.map(tag => tag.name);
console.log('Tags: ', tags);
const highestBuildNumber = Math.max(
...(tags
.filter(tag => tag.startsWith(currentPatchVersion))
.map(tag => tag.split('-')[1])
),
);
console.log('Highest build number from current patch version:', highestBuildNumber);

const newBuildNumber = `${currentPatchVersion}-${highestBuildNumber + 1}`;
console.log(`Setting npm version for this PR to ${newBuildNumber}`);
exec(`npm version ${newBuildNumber} -m "Update version to ${newBuildNumber}"`,
// eslint-disable-next-line no-shadow
(err, stdout, stderr) => {
console.log(stdout);
if (err) {
console.log(stderr);
}
});
// tags come from latest to oldest
const highestVersion = tags[0];
console.log(highestVersion);

// should SEMVER_LEVEL default to BUILD?
const semanticVersionLevel = core.getInput('SEMVER_LEVEL', {require: true});
const newVersion = functions.incrementVersion(highestVersion, semanticVersionLevel);

core.setOutput('VERSION', newVersion);

functions.execUpdateToNewVersion(newVersion);
})
.catch(exception => core.setFailed(exception));
} else {
Expand Down
83 changes: 83 additions & 0 deletions .github/actions/bumpVersion/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {exec} = require('child_process');

const semanticVersionLevels = {
major: 'MAJOR',
minor: 'MINOR',
patch: 'PATCH',
build: 'BUILD',
};
const maxIncrements = 999;

const getVersionNumberFromString = (versionString) => {
const [version, build] = versionString.slice(1).split('-');
const [major, minor, patch] = version.split('.').map(n => Number(n));
return [major, minor, patch, build ? Number(build) : undefined];
};

const getVersionStringFromNumber = (major, minor, patch, build) => {
if (build) { return `v${major}.${minor}.${patch}-${build}`; }
return `v${major}.${minor}.${patch}`;
};

const incrementMinor = (major, minor) => {
if (minor < maxIncrements) { return getVersionStringFromNumber(major, minor + 1, 0); }
return getVersionStringFromNumber(major + 1, 0, 0);
};

const incrementPatch = (major, minor, patch) => {
if (patch < maxIncrements) { return getVersionStringFromNumber(major, minor, patch + 1); }
return incrementMinor(major, minor);
};

const incrementVersion = (version, level) => {
const [major, minor, patch, build] = getVersionNumberFromString(
version,
);

// majors will always be incremented
if (level === semanticVersionLevels.major) { return getVersionStringFromNumber(major + 1, 0, 0); }

if (level === semanticVersionLevels.minor) {
return incrementMinor(major, minor);
}
if (level === semanticVersionLevels.patch) {
return incrementPatch(major, minor, patch);
}
if (build === undefined) { return getVersionStringFromNumber(major, minor, patch, 1); }
if (build < maxIncrements) {
return getVersionStringFromNumber(major, minor, patch, build + 1);
}
return incrementPatch(major, minor, patch);
};

const execUpdateToNewVersion = (version) => {
exec(
`npm version ${version} -m "Update version to ${version}"`,
// eslint-disable-next-line no-shadow
(err, stdout, stderr) => {
console.log(stdout);
if (err) {
console.log(stderr);
}
},
);
};

module.exports = {
execUpdateToNewVersion,
getVersionNumberFromString,
getVersionStringFromNumber,
incrementVersion,

// for the tests
maxIncrements,
semanticVersionLevels,
incrementMinor,
incrementPatch,
};

// const getHighestBuildNumberFromPatchVersion = (tags, currentPatchVersion) => Math.max(
// ...tags
// .filter(tag => tag.startsWith(currentPatchVersion))
// .map(tag => tag.split('-')[1]),
// );
Loading

0 comments on commit 893d7c0

Please sign in to comment.