SYNERGY-999 Fix script that generates version number

This commit is contained in:
Serhii Hadzhilov 2021-05-25 14:06:22 +03:00
parent 6080b6937d
commit c3f6ae708a
2 changed files with 86 additions and 50 deletions

View file

@ -1,4 +1,5 @@
const core = require('@actions/core'); const core = require('@actions/core');
const { Octokit } = require("@octokit/core");
const fs = require('fs'); const fs = require('fs');
function VersionPart(part) { function VersionPart(part) {
@ -85,28 +86,35 @@ function Version(version) {
m_build.toString()); m_build.toString());
} }
this.isEmpty = function(){
return (!this.getMajor() && !this.getMinor() && !this.getPatch());
}
this.nextBuild = function() { this.nextBuild = function() {
m_build.next(); m_build.next();
} }
function init() { function init() {
let versionParts = version.split(PARTS_DELIMITER); if (version)
{
let versionParts = version.split(PARTS_DELIMITER);
if (versionParts.length == 3) { if (versionParts.length == 3) {
m_major.setPart(versionParts[0]); m_major.setPart(versionParts[0]);
m_minor.setPart(versionParts[1]); m_minor.setPart(versionParts[1]);
m_build.setPart(versionParts[2]); m_build.setPart(versionParts[2]);
m_patch.setNumber(m_build.getNumber()); m_patch.setNumber(m_build.getNumber());
m_build.setNumber(0); m_build.setNumber(0);
} }
else if (versionParts.length == 4) { else if (versionParts.length == 4) {
m_major.setPart(versionParts[0]); m_major.setPart(versionParts[0]);
m_minor.setPart(versionParts[1]); m_minor.setPart(versionParts[1]);
m_patch.setPart(versionParts[2]); m_patch.setPart(versionParts[2]);
m_build.setPart(versionParts[3]); m_build.setPart(versionParts[3]);
} }
else { else {
core.setFailed('ERROR: previous-build contains wrong version number <' + version + '>'); core.setFailed('ERROR: wrong version number <' + version + '>');
}
} }
} }
@ -129,8 +137,7 @@ function Version(version) {
this.isSamePatch = function(version) { this.isSamePatch = function(version) {
return (this.getMajor() == version.getMajor() && return (this.getMajor() == version.getMajor() &&
this.getMinor() == version.getMinor() && this.getMinor() == version.getMinor() &&
this.getPatch() == version.getPatch() && this.getPatch() == version.getPatch());
this.getStage() == version.getStage());
} }
init(); init();
@ -148,35 +155,68 @@ function getOptionValue(source, option) {
} }
} }
function getVersionFromFile(){ async function getVersionApi()
let options = fs.readFileSync('cmake/Version.cmake'); {
let major = getOptionValue(options, 'SYNERGY_VERSION_MAJOR'); const octokit = new Octokit({});
let minor = getOptionValue(options, 'SYNERGY_VERSION_MINOR');
let patch = getOptionValue(options, 'SYNERGY_VERSION_PATCH');
let stage = getOptionValue(options, 'SYNERGY_VERSION_STAGE');
return (major + '.' + minor + '.' + patch + '-' + stage); let response = await octokit.request('GET /repos/{owner}/{repo}/releases?per_page=100', {
owner: 'symless',
repo: 'synergy-core'
});
return response.data;
} }
try { async function getVersionsFromGithub(cmakeVersion)
let version = null; {
let gitVersion = new Version(core.getInput('previous-build')); let gitVersion = new Version();
let cmakeVersion = new Version(getVersionFromFile()); let versions = await getVersionApi();
console.log('INFO: Version from Git: <' + gitVersion.toString() + '>'); for (let i = 0; i < versions.length; ++i) {
console.log('INFO: Version from cmake: <' + cmakeVersion.toString() + '>'); let version = new Version(versions[i].tag_name);
if (version.isSamePatch(cmakeVersion)) {
if (cmakeVersion.isSamePatch(gitVersion)) { gitVersion = version;
gitVersion.nextBuild(); break;
version = gitVersion; }
}
else {
version = cmakeVersion;
} }
console.log('INFO: Generate build version: <' + version.toString() + '>'); return gitVersion;
core.setOutput('next-build', version.toString()); }
} catch (error) { function getVersionFromFile(){
core.setFailed(error.message); let options = fs.readFileSync('cmake/Version.cmake');
} let major = getOptionValue(options, 'SYNERGY_VERSION_MAJOR');
let minor = getOptionValue(options, 'SYNERGY_VERSION_MINOR');
let patch = getOptionValue(options, 'SYNERGY_VERSION_PATCH');
let stage = getOptionValue(options, 'SYNERGY_VERSION_STAGE');
return new Version(major + '.' + minor + '.' + patch + '-' + stage);
}
async function main()
{
try {
let version = null;
let cmakeVersion = getVersionFromFile();
let gitVersion = await getVersionsFromGithub(cmakeVersion);
console.log('INFO: Version from Git: <' + gitVersion.toString() + '>');
console.log('INFO: Version from cmake: <' + cmakeVersion.toString() + '>');
if (gitVersion.isEmpty()) {
version = cmakeVersion;
}
else {
gitVersion.nextBuild();
version = gitVersion;
}
console.log('INFO: Generate build version: <' + version.toString() + '>');
core.setOutput('next-build', version.toString());
} catch (error) {
core.setFailed(error.message);
}
};
main();

View file

@ -12,18 +12,14 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
- name: 'Get Previous tag'
id: previoustag
uses: "WyriHaximus/github-action-get-previous-tag@master"
- name: 'Install git core' - name: 'Install git core'
run: npm install @actions/core run: |
npm install @actions/core
npm install @octokit/core
- name: 'Generate next build number' - name: 'Generate next build number'
id: build id: build
uses: "./.github/actions/build-number" uses: "./.github/actions/build-number"
with:
previous-build: ${{ steps.previoustag.outputs.tag }}
- name: Create Release - name: Create Release
id: create_release id: create_release