Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- only potential labels with non zero patch are matched
- getVersionLabels returns map with potential label and its version label (null values possible)
  • Loading branch information
psavidis committed Oct 15, 2024
1 parent 8dd3872 commit e1eec29
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
48 changes: 27 additions & 21 deletions common/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163776,26 +163776,22 @@ const https = __nccwpck_require__(95687);

module.exports = async function () {

// Get Version Labels

// Returns Map with [potentialLabel - versionLabel entries]
const getVersionLabels = async function (potentialLabels) {
const promises = potentialLabels.map(async potentialLabel => {

if (potentialLabel.split('.')[2] === '0') { // For potential versions that are not patch
console.log(`label ${potentialLabel} is not a patch version, filtering it out.`);
return null;
}

const results = potentialLabels.map(potentialLabel => {
// For maintenance versions, find the latest patch from repo
const latestPatchVersion = await getLatestPatchVersion(potentialLabel);
const latestPatchVersion = getLatestPatchVersion(potentialLabel);
console.log(`${potentialLabel} => has Latest Patch Version: ${latestPatchVersion}`);

return getNextPatchVersion(latestPatchVersion);
if (latestPatchVersion == null) {
console.log(`No patch version was found in downloads page for potentialLabel: ${potentialLabel}. Returning entry with null version.`);
return [ potentialLabel, null ];
}

return [ potentialLabel, getNextPatchVersion(latestPatchVersion) ];
});

// Wait for all promises to settle and filter out null values
const results = await Promise.all(promises);
return results.filter(label => label !== null);
return new Map(results);
};

const removeLabels = async function (owner, repo, issueNumber, labels) {
Expand Down Expand Up @@ -163852,7 +163848,7 @@ module.exports = async function () {
return versionParts.join('.');
}

const getLatestPatchVersion = async function (potentialLabel) {
const getLatestPatchVersion = function (potentialLabel) {
console.log(`Get latest patch version for issue: #${issueNumber} for potential label:`, potentialLabel);
try {
const minorVersion = getMinorFromPotentialLabel(potentialLabel);
Expand Down Expand Up @@ -163952,8 +163948,8 @@ module.exports = async function () {

console.log(`read repo information repoName: ${repoName} - : owner: ${owner}`);

const expression = `potential:\\d+\\.\\d+\\.(?!0)\\d+`
const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, expression);
const potentialLabelsWithNonZeroPatchVersionRegex = `potential:\\d+\\.\\d+\\.(?!0)\\d+`;
const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, potentialLabelsWithNonZeroPatchVersionRegex);

if (!potentialLabels.length) {
console.log("no `potential:` label found, exiting.");
Expand All @@ -163965,11 +163961,21 @@ module.exports = async function () {
const latestVersion = await getLatestMinorVersion();
console.log(`Latest minor version: ${latestVersion}`);

const versionLabels = await getVersionLabels(potentialLabels);
const uniqueVersionLabels = [...new Set(versionLabels)];
const potentialToVersionLabelsMap = await getVersionLabels(potentialLabels);

// only potential labels that have a version label will be removed
const potentialLabelsToRemove = potentialToVersionLabelsMap
.filter(([_, versionLabel]) => versionLabel !== null)
.map(([potentialLabel, _]) => potentialLabel);

const versionLabelsToAssign = potentialToVersionLabelsMap
.filter(([_, versionLabel]) => versionLabel !== null)
.map(([_, versionLabel]) => versionLabel);

const uniqueVersionLabelsToAssign = [...new Set(versionLabelsToAssign)];

await setLabels(owner, repoName, issueNumber, uniqueVersionLabels);
await removeLabels(owner, repoName, issueNumber, potentialLabels);
await setLabels(owner, repoName, issueNumber, uniqueVersionLabelsToAssign);
await removeLabels(owner, repoName, issueNumber, potentialLabelsToRemove);
}

/***/ }),
Expand Down
48 changes: 27 additions & 21 deletions common/src/set-version-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ const https = require('https');

module.exports = async function () {

// Get Version Labels

// Returns Map with [potentialLabel - versionLabel entries]
const getVersionLabels = async function (potentialLabels) {
const promises = potentialLabels.map(async potentialLabel => {

if (potentialLabel.split('.')[2] === '0') { // For potential versions that are not patch
console.log(`label ${potentialLabel} is not a patch version, filtering it out.`);
return null;
}

const results = potentialLabels.map(potentialLabel => {
// For maintenance versions, find the latest patch from repo
const latestPatchVersion = await getLatestPatchVersion(potentialLabel);
const latestPatchVersion = getLatestPatchVersion(potentialLabel);
console.log(`${potentialLabel} => has Latest Patch Version: ${latestPatchVersion}`);

return getNextPatchVersion(latestPatchVersion);
if (latestPatchVersion == null) {
console.log(`No patch version was found in downloads page for potentialLabel: ${potentialLabel}. Returning entry with null version.`);
return [ potentialLabel, null ];
}

return [ potentialLabel, getNextPatchVersion(latestPatchVersion) ];
});

// Wait for all promises to settle and filter out null values
const results = await Promise.all(promises);
return results.filter(label => label !== null);
return new Map(results);
};

const removeLabels = async function (owner, repo, issueNumber, labels) {
Expand Down Expand Up @@ -80,7 +76,7 @@ module.exports = async function () {
return versionParts.join('.');
}

const getLatestPatchVersion = async function (potentialLabel) {
const getLatestPatchVersion = function (potentialLabel) {
console.log(`Get latest patch version for issue: #${issueNumber} for potential label:`, potentialLabel);
try {
const minorVersion = getMinorFromPotentialLabel(potentialLabel);
Expand Down Expand Up @@ -180,8 +176,8 @@ module.exports = async function () {

console.log(`read repo information repoName: ${repoName} - : owner: ${owner}`);

const expression = `potential:\\d+\\.\\d+\\.(?!0)\\d+`
const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, expression);
const potentialLabelsWithNonZeroPatchVersionRegex = `potential:\\d+\\.\\d+\\.(?!0)\\d+`;
const potentialLabels = await getLabelsMatchingRegexp(owner, repoName, issueNumber, potentialLabelsWithNonZeroPatchVersionRegex);

if (!potentialLabels.length) {
console.log("no `potential:` label found, exiting.");
Expand All @@ -193,9 +189,19 @@ module.exports = async function () {
const latestVersion = await getLatestMinorVersion();
console.log(`Latest minor version: ${latestVersion}`);

const versionLabels = await getVersionLabels(potentialLabels);
const uniqueVersionLabels = [...new Set(versionLabels)];
const potentialToVersionLabelsMap = await getVersionLabels(potentialLabels);

// only potential labels that have a version label will be removed
const potentialLabelsToRemove = potentialToVersionLabelsMap
.filter(([_, versionLabel]) => versionLabel !== null)
.map(([potentialLabel, _]) => potentialLabel);

const versionLabelsToAssign = potentialToVersionLabelsMap
.filter(([_, versionLabel]) => versionLabel !== null)
.map(([_, versionLabel]) => versionLabel);

const uniqueVersionLabelsToAssign = [...new Set(versionLabelsToAssign)];

await setLabels(owner, repoName, issueNumber, uniqueVersionLabels);
await removeLabels(owner, repoName, issueNumber, potentialLabels);
await setLabels(owner, repoName, issueNumber, uniqueVersionLabelsToAssign);
await removeLabels(owner, repoName, issueNumber, potentialLabelsToRemove);
}

0 comments on commit e1eec29

Please sign in to comment.