From e151fcb070ee1a31bde9f317a9ed8e4aa8981264 Mon Sep 17 00:00:00 2001 From: Petros Savvidis Date: Fri, 11 Oct 2024 13:40:10 +0300 Subject: [PATCH] get latest patch versions from repo This commit introduces the following changes in the logic of the script: - The scripts performs one call to fetch the latest minor version from the main repo - For each of the potential labels, the scripts - Estimates the version label for potential labels that use the latest version (by mistake) - Makes one call for each potential label to fetch the latest from the repo - For cases where parsing of the minor version of a potential label is wrong, the calculated version is used as fallback --- common/dist/index.js | 87 +++++++++++++++++++++++++++++--- common/src/set-version-labels.js | 87 +++++++++++++++++++++++++++++--- 2 files changed, 158 insertions(+), 16 deletions(-) diff --git a/common/dist/index.js b/common/dist/index.js index eff1bd9..7310717 100644 --- a/common/dist/index.js +++ b/common/dist/index.js @@ -163774,13 +163774,35 @@ const core = __nccwpck_require__(42186); const github = __nccwpck_require__(95438); module.exports = async function () { - const getListWithNewPrefix = function (strings, oldPrefix, newPrefix) { - return strings.map(str => { - if (str.startsWith(oldPrefix)) { - return newPrefix + str.slice(oldPrefix.length); + + // Get Version Labels + + const getVersionLabels = function (potentialLabels, latestVersion) { + return potentialLabels.map(potentialLabel => { + const minorVersion = getMinorFromPotentialLabel(potentialLabel); + + if (minorVersion === latestVersion) { + // For latest version, calculate the version label right away + const calculatedVersion = getVersionLabelFromPotential(potentialLabel); + console.log(`returning calculated version for latest potential version: ${calculatedVersion}`); + return calculatedVersion; } - return str; - }); + + // For maintenance versions, find the latest patch from repo + const latestPatchVersion = getLatestPatchVersion(potentialLabel); + + if (!latestPatchVersion) { + const calculatedVersionLabel = getVersionLabelFromPotential(potentialLabel); // fallback if github is down, pom version is wrong, error at parsing + console.log(`No latest patch version found for potential label: ${potentialLabel}, returning calculated version ${calculatedVersionLabel}`); + return calculatedVersionLabel; + } + + return latestPatchVersion; + }).filter(label => label !== null); + } + + const getVersionLabelFromPotential = function (potentialLabel) { + return `version:` + potentialLabel.slice(`potential:`.length); } const removeLabels = async function (owner, repo, issueNumber, labels) { @@ -163824,6 +163846,53 @@ module.exports = async function () { } } + const getMinorFromPotentialLabel = function (potentialLabel) { + const regex = /potential:(\d+\.\d+)\.\d+/; + const match = potentialLabel.match(regex); + + return match ? match[1] : null; + } + + const getLatestPatchVersion = async function (potentialLabel) { + console.log(`Get latest patch version for issue: #${issueNumber} for potential label:`, potentialLabel); + try { + const minorVersion = getMinorFromPotentialLabel(potentialLabel); + console.log(`Fetched the Minor Version:` + minorVersion); + + if (!minorVersion) { + console.log('No minor version found in the potential label'); + return null; + } + + const url = `https://github.com/camunda/camunda-bpm-platform-maintenance/raw/refs/heads/${minorVersion}/pom.xml`; + + const response = await fetch(url); + const xml = await response.text(); + + // Define regex to match ... + const regex = /(.*?)<\/version>/; + const match = xml.match(regex); + + // Return the version if found, otherwise return null + return match ? match[1] : null; + } catch (error) { + console.error("Error fetching the XML document:", error); + return null; + } + } + + const getLatestVersion = async function() { + const url = `https://github.com/camunda/camunda-bpm-platform/raw/refs/heads/master/pom.xml`; + const response = await fetch(url); + const pomXml = await response.text(); + + const versionTagRegex = /camunda-root<\/artifactId>\s*(\d+\.\d+)(?:\.\d+.*)?<\/version>/ + const match = pomXml.match(versionTagRegex); + + // Return the version if found, otherwise return null + return match ? match[1] : null; + } + const setLabels = async function (owner, repo, issueNumber, labels) { console.log(`Set labels for issue: #${issueNumber}:`, labels); try { @@ -163852,13 +163921,15 @@ module.exports = async function () { const expression = `potential:\\d+\\.\\d+\\.\\d+` const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, expression); - if (!potentialLabels.length) { console.log("no `potential:` label found, exiting.") return; } - const versionLabels = getListWithNewPrefix(potentialLabels, "potential:", "version:"); + const latestVersion = await getLatestVersion(); + console.log(`Latest version: ${latestVersion}`); + + const versionLabels = getVersionLabels(potentialLabels, latestVersion); await setLabels(owner, repoName, issueNumber, versionLabels); await removeLabels(owner, repoName, issueNumber, potentialLabels); diff --git a/common/src/set-version-labels.js b/common/src/set-version-labels.js index c535340..22debd2 100644 --- a/common/src/set-version-labels.js +++ b/common/src/set-version-labels.js @@ -2,13 +2,35 @@ const core = require('@actions/core'); const github = require('@actions/github'); module.exports = async function () { - const getListWithNewPrefix = function (strings, oldPrefix, newPrefix) { - return strings.map(str => { - if (str.startsWith(oldPrefix)) { - return newPrefix + str.slice(oldPrefix.length); + + // Get Version Labels + + const getVersionLabels = function (potentialLabels, latestVersion) { + return potentialLabels.map(potentialLabel => { + const minorVersion = getMinorFromPotentialLabel(potentialLabel); + + if (minorVersion === latestVersion) { + // For latest version, calculate the version label right away + const calculatedVersion = getVersionLabelFromPotential(potentialLabel); + console.log(`returning calculated version for latest potential version: ${calculatedVersion}`); + return calculatedVersion; + } + + // For maintenance versions, find the latest patch from repo + const latestPatchVersion = getLatestPatchVersion(potentialLabel); + + if (!latestPatchVersion) { + const calculatedVersionLabel = getVersionLabelFromPotential(potentialLabel); // fallback if github is down, pom version is wrong, error at parsing + console.log(`No latest patch version found for potential label: ${potentialLabel}, returning calculated version ${calculatedVersionLabel}`); + return calculatedVersionLabel; } - return str; - }); + + return latestPatchVersion; + }).filter(label => label !== null); + } + + const getVersionLabelFromPotential = function (potentialLabel) { + return `version:` + potentialLabel.slice(`potential:`.length); } const removeLabels = async function (owner, repo, issueNumber, labels) { @@ -52,6 +74,53 @@ module.exports = async function () { } } + const getMinorFromPotentialLabel = function (potentialLabel) { + const regex = /potential:(\d+\.\d+)\.\d+/; + const match = potentialLabel.match(regex); + + return match ? match[1] : null; + } + + const getLatestPatchVersion = async function (potentialLabel) { + console.log(`Get latest patch version for issue: #${issueNumber} for potential label:`, potentialLabel); + try { + const minorVersion = getMinorFromPotentialLabel(potentialLabel); + console.log(`Fetched the Minor Version:` + minorVersion); + + if (!minorVersion) { + console.log('No minor version found in the potential label'); + return null; + } + + const url = `https://github.com/camunda/camunda-bpm-platform-maintenance/raw/refs/heads/${minorVersion}/pom.xml`; + + const response = await fetch(url); + const xml = await response.text(); + + // Define regex to match ... + const regex = /(.*?)<\/version>/; + const match = xml.match(regex); + + // Return the version if found, otherwise return null + return match ? match[1] : null; + } catch (error) { + console.error("Error fetching the XML document:", error); + return null; + } + } + + const getLatestVersion = async function() { + const url = `https://github.com/camunda/camunda-bpm-platform/raw/refs/heads/master/pom.xml`; + const response = await fetch(url); + const pomXml = await response.text(); + + const versionTagRegex = /camunda-root<\/artifactId>\s*(\d+\.\d+)(?:\.\d+.*)?<\/version>/ + const match = pomXml.match(versionTagRegex); + + // Return the version if found, otherwise return null + return match ? match[1] : null; + } + const setLabels = async function (owner, repo, issueNumber, labels) { console.log(`Set labels for issue: #${issueNumber}:`, labels); try { @@ -80,13 +149,15 @@ module.exports = async function () { const expression = `potential:\\d+\\.\\d+\\.\\d+` const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, expression); - if (!potentialLabels.length) { console.log("no `potential:` label found, exiting.") return; } - const versionLabels = getListWithNewPrefix(potentialLabels, "potential:", "version:"); + const latestVersion = await getLatestVersion(); + console.log(`Latest version: ${latestVersion}`); + + const versionLabels = getVersionLabels(potentialLabels, latestVersion); await setLabels(owner, repoName, issueNumber, versionLabels); await removeLabels(owner, repoName, issueNumber, potentialLabels);