Skip to content

Commit

Permalink
get latest patch versions from repo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
psavidis committed Oct 11, 2024
1 parent 80826f5 commit e22148c
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 16 deletions.
87 changes: 79 additions & 8 deletions common/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163774,13 +163774,36 @@ 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);
console.log(`${potentialLabel} => hsa Latest Patch Version: ${latestPatchVersion}`);

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) {
Expand Down Expand Up @@ -163824,6 +163847,52 @@ 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);

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 <version>...</version>
const patchVersionRegex = /<artifactId>camunda-root<\/artifactId>\s*<version>(\d+\.\d+\.\d+)(?:-\w+)?<\/version>/;
const match = xml.match(patchVersionRegex);

// 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 = /<artifactId>camunda-root<\/artifactId>\s*<version>(\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 {
Expand Down Expand Up @@ -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);
Expand Down
87 changes: 79 additions & 8 deletions common/src/set-version-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ 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);
console.log(`${potentialLabel} => hsa Latest Patch Version: ${latestPatchVersion}`);

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) {
Expand Down Expand Up @@ -52,6 +75,52 @@ 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);

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 <version>...</version>
const patchVersionRegex = /<artifactId>camunda-root<\/artifactId>\s*<version>(\d+\.\d+\.\d+)(?:-\w+)?<\/version>/;
const match = xml.match(patchVersionRegex);

// 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 = /<artifactId>camunda-root<\/artifactId>\s*<version>(\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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e22148c

Please sign in to comment.