Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from Rabatta-ApS/2-newStructure
Browse files Browse the repository at this point in the history
updated to spec
  • Loading branch information
Fatdet authored Feb 24, 2022
2 parents ede8122 + ae7095a commit c242ea7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
23 changes: 14 additions & 9 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
name: 'Chrome extension version bump'
description: 'Update version number in manifest file '
description: 'Update version number in version file '
inputs:
path-to-manifest:
description: 'The path to the manifest file'
default: 'src/manifestShared.json'
path-to-version-file:
description: 'The path to the version file'
default: 'version.txt'
required: true
new-verison:
description: 'New manifest version'
description: 'Manually decide new version'
required: false
mode:
description: 'Can be get, set or ignored if you want to both get the file and set the new value'
required: false
description: 'Can be GET_VERSION, BUMP_VERSION, SET_VERSION'
required: true
default: 'BUMP_VERSION'
outputs:
version:
description: The bumped version
cur_ver:
description: The current version
new_ver:
description: The new version
was_bumped:
description: Whether the version was bumped or not
runs:
using: 'node16'
main: 'index.js'
64 changes: 39 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,66 @@ const { Octokit } = require("@octokit/action");
const fs = require("fs");
const path = require("path");

let cur_ver;
let new_ver;
let was_bumped;

async function main(){
try {
const pathToManifest = core.getInput('path-to-manifest', { required: true });
const pathToVersionFile = core.getInput('path-to-version-file', { required: true });
const mode = core.getInput('mode');
let newManifestVersion = core.getInput('new-verison');
const newVersion= core.getInput('new-verison');

const jsonData = getJsonFromManifest(pathToManifest);
cur_ver = "";
new_ver = "";
was_bumped = false;

if(mode != 'set'){
const oldManifestVersion = jsonData.version;
newManifestVersion = await updatedManifestVersion(oldManifestVersion);
if(!process.env.GITHUB_TOKEN){
throw new Error("GITHUB_TOKEN env not set");
}

core.setOutput('version', newManifestVersion);

if(mode != 'get'){
jsonData.version = newManifestVersion;
updateManifest(pathToManifest, jsonData);

if(mode == "GET_VERSION"){
cur_ver = getVersion(pathToVersionFile);
new_ver = updateVersion(cur_ver);
}
else if(mode == "BUMP_VERSION"){
cur_ver = getVersion(pathToVersionFile);
new_ver = updateVersion(cur_ver);
updateVersionFile(pathToVersionFile, new_ver);
was_bumped = cur_ver != new_ver;
}
else if(mode == "SET_VERSION"){
cur_ver = getVersion(pathToVersionFile);
new_ver = newVersion;
updateVersionFile(pathToVersionFile, new_ver);
was_bumped = cur_ver != new_ver;
}
else{
throw new Error("Mode not recognised");
}

} catch (error) {
core.error(error.message);
}
finally{
core.setOutput("cur_version", cur_ver);
core.setOutput("new_version", new_ver);
core.setOutput("was_bumped", was_bumped);
}
}

function getJsonFromManifest(pathToManifest){
const rawData = fs.readFileSync(path.resolve(pathToManifest));
return JSON.parse(rawData);
function getVersion(pathToFile){
return fs.readFileSync(path.resolve(pathToFile), {encoding: 'utf8'});
}

async function updatedManifestVersion(version){
async function updateVersion(version){
const labels = await getLabels();

const versionNumbers = version.match(/\d/g);
for(let i = 0; i < versionNumbers.length; i++){
versionNumbers[i] = Number.parseInt(versionNumbers[i]);
}

let noRelease = false;
for(const label of labels){
core.debug(label);
if(label == "release:major"){
Expand All @@ -59,13 +79,8 @@ async function updatedManifestVersion(version){
else if(label == "release:patch"){
versionNumbers[2]++;
}
else if(label == "release:none"){
noRelease = true;
}
}

core.setOutput('no-release', noRelease);

const versionString = `${versionNumbers[0]}.${versionNumbers[1]}.${versionNumbers[2]}`;

return versionString;
Expand All @@ -84,9 +99,8 @@ async function getLabels(){
return labels ? labels.map(label => label.name) : [];
}

function updateManifest(pathToManifest, jsonData){
const data = JSON.stringify(jsonData, null, 2);
fs.writeFileSync(path.resolve(pathToManifest), data);
function updateVersionFile(pathToFile, newVersion){
fs.writeFileSync(path.resolve(pathToFile), newVersion);
}

main();
Expand Down

0 comments on commit c242ea7

Please sign in to comment.