const core = require("@actions/core"); function parseVersion(str) { if (typeof str != "string") { throw new Error("When parsing a version number a string is expected."); } var arr = str.split("."); // parse int or default to 0 var maj = parseInt(arr[0]) || 0; var min = parseInt(arr[1]) || 0; var patch = parseInt(arr[2]) || 0; return { major: maj, minor: min, patch: patch, }; } async function increaseVersionPart(url_pattern, version, version_part) { let test_vor_existence = version; while (true) { const url_with_version = url_pattern.replace( /{{VERSION}}/g, test_vor_existence, ); if (url_with_version == url_pattern) { throw new Error("The url does not contain an {{VERSION}} marker!"); } let status = await fetch(url_with_version, { method: "GET", }).then((response) => response.status); if (status === 200) { version = test_vor_existence; const version_parsed = parseVersion(test_vor_existence); if (version_part == "minor") { test_vor_existence = `${version_parsed.major}.${ version_parsed.minor + 1 }.0`; } else if (version_part == "micro") { test_vor_existence = `${version_parsed.major}.${version_parsed.minor}.${ version_parsed.patch + 1 }`; } else { throw new Error("The verision part to be updated isn't reconised!"); } } else { return version; } } } async function increaseVersion(url_pattern, version) { let minor = await increaseVersionPart( url_pattern, version, (version_part = "minor"), ); return await increaseVersionPart( url_pattern, minor, (version_part = "micro"), ); } async function main() { try { const url_pattern = core.getInput("url-pattern"); const start_version = core.getInput("start-version"); const current_version = `${await increaseVersion( url_pattern, start_version, )}`; core.info(`The new version determend is ${current_version}`); core.setOutput("current_version", `${current_version}`); } catch (error) { core.setFailed(`Action failed with error: ${error}`); } } main();