Skip to content

Commit 1d9eccf

Browse files
authored
chore: corrected currency update logic to prevent mixing major and minor update (#2464)
ref https://jsw.ibm.com/browse/INSTA-84235
1 parent 264d9d5 commit 1d9eccf

1 file changed

Lines changed: 61 additions & 50 deletions

File tree

bin/dependencies/currency/update-currencies.js

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const path = require('path');
99
const semver = require('semver');
1010
const { execSync } = require('child_process');
1111
const fs = require('fs');
12-
const currencies = require(path.join(__dirname, '..', '..', '..', 'currencies.json'));
12+
const currenciesPath = path.join(__dirname, '..', '..', '..', 'currencies.json');
1313
const utils = require('../utils');
1414
const MAJOR_UPDATES_MODE = process.env.MAJOR_UPDATES_MODE ? process.env.MAJOR_UPDATES_MODE === 'true' : false;
1515
const BRANCH = process.env.BRANCH;
@@ -18,94 +18,109 @@ const cwd = path.join(__dirname, '..', '..', '..');
1818
const DRY_RUN = process.env.DRY_RUN === 'true';
1919

2020
if (!BRANCH) throw new Error('Please set env variable "BRANCH".');
21-
let branchName = BRANCH;
21+
22+
const loadCurrencies = () => {
23+
delete require.cache[require.resolve(currenciesPath)];
24+
return require(currenciesPath);
25+
};
26+
27+
let currencies = loadCurrencies();
2228

2329
console.log(`MAJOR_UPDATES_MODE: ${MAJOR_UPDATES_MODE}`);
2430
console.log(`BRANCH: ${BRANCH}`);
2531
console.log(`SKIP_PUSH: ${SKIP_PUSH}`);
32+
console.log('==============\n');
2633

2734
if (!MAJOR_UPDATES_MODE) {
28-
console.log('Preparing patch/minor updates...');
29-
utils.prepareGitEnvironment(branchName, cwd, BRANCH === 'main', DRY_RUN);
35+
console.log('[INIT] Preparing batch (patch/minor) updates...');
36+
utils.prepareGitEnvironment(BRANCH, cwd, BRANCH === 'main', DRY_RUN);
3037
}
3138

32-
currencies.forEach(currency => {
33-
console.log(`Checking currency update for ${currency.name}`);
39+
currencies.forEach(originalCurrency => {
40+
let currency = originalCurrency;
41+
42+
if (MAJOR_UPDATES_MODE) {
43+
utils.prepareGitEnvironment('main', cwd, true, DRY_RUN);
44+
45+
currencies = loadCurrencies();
46+
currency = currencies.find(c => c.name === originalCurrency.name);
47+
}
48+
49+
console.log(`-----------${currency.name}-----------`);
3450

3551
if (currency.ignoreUpdates) {
36-
console.log(`Skipping ${currency.name}. ignoreUpdates is set.`);
52+
console.log(`[SKIP] ${currency.name}. ignoreUpdates is set.`);
3753
return;
3854
}
3955

4056
const { version: installedVersion, versionObj: installedVersionObj } = utils.getLatestInstalledVersion(currency);
4157

4258
if (!installedVersion) {
43-
console.log(`Skipping ${currency.name}. Seems to be a core dependency.`);
59+
console.log(`[SKIP] ${currency.name}. No installed version(core dependency).`);
4460
return;
4561
}
4662

4763
const latestVersion = utils.getLatestVersion({
4864
pkgName: currency.name,
49-
installedVersion: installedVersion,
65+
installedVersion,
5066
isBeta: currency.isBeta
5167
});
5268

5369
if (latestVersion === installedVersion) {
54-
console.log(
55-
`Skipping ${currency.name}. Installed version is ${installedVersion}. Latest version is ${latestVersion}`
56-
);
70+
console.log(`[UP-TO-DATE] ${currency.name} already up-to-date (${installedVersion})`);
5771
return;
5872
}
5973

60-
console.log(`Latest version: ${latestVersion}`);
61-
console.log(`Installed version: ${installedVersion}`);
74+
const isMajorUpdate = semver.major(latestVersion) !== semver.major(installedVersion);
75+
76+
console.log(`[UPDATE] ${currency.name}: ${installedVersion}${latestVersion}`);
6277

63-
const isMajorUpdate = semver.major(latestVersion) === semver.major(installedVersion);
78+
if (MAJOR_UPDATES_MODE && !isMajorUpdate) {
79+
console.log('[SKIP] Not a major update');
80+
return;
81+
}
6482

65-
if (!MAJOR_UPDATES_MODE && !isMajorUpdate) {
66-
console.log(`Skipping ${currency.name}. Major updates not allowed.`);
83+
if (!MAJOR_UPDATES_MODE && isMajorUpdate) {
84+
console.log(`[SKIP] ${currency.name}. Major updates not allowed.`);
6785
return;
6886
}
6987

88+
let branchName = BRANCH;
89+
7090
if (MAJOR_UPDATES_MODE) {
71-
if (semver.major(latestVersion) === semver.major(installedVersion)) {
72-
console.log(`Skipping ${currency.name}. No major update available.`);
91+
if (!isMajorUpdate) {
92+
console.log(`[SKIP] ${currency.name}. No major update available.`);
7393
return;
7494
}
7595

76-
console.log(`Major update available for ${currency.name}.`);
96+
console.log(`[UPDATE] Major update available for ${currency.name}.`);
7797
branchName = utils.createBranchName(BRANCH, currency.name, latestVersion);
7898

7999
if (utils.branchExists(branchName, cwd)) {
80-
console.log(`Skipping ${currency.name}. Branch exists.`);
100+
console.log(`[SKIP] Branch exists: ${branchName}`);
81101
return;
82102
}
83103

84-
utils.prepareGitEnvironment(branchName, cwd, BRANCH === 'main');
104+
utils.prepareGitEnvironment(branchName, cwd, BRANCH === 'main', DRY_RUN);
85105
}
86106

87107
// 1. update currencies.json versions array
88-
const installedVersionIndex = currency.versions.findIndex(vObj => {
108+
const installedIndex = currency.versions.findIndex(vObj => {
89109
const v = typeof vObj === 'string' ? vObj : vObj.v;
90110
return v === installedVersion;
91111
});
92112

93-
if (MAJOR_UPDATES_MODE && isMajorUpdate) {
94-
const newVersionObj =
95-
typeof installedVersionObj === 'string' ? latestVersion : { ...installedVersionObj, v: latestVersion };
96-
currency.versions.unshift(newVersionObj);
97-
} else {
98-
currency.versions = currency.versions.filter(vObj => {
99-
const v = typeof vObj === 'string' ? vObj : vObj.v;
100-
return v !== installedVersion;
101-
});
102-
const newVersionObj =
103-
typeof installedVersionObj === 'string' ? latestVersion : { ...installedVersionObj, v: latestVersion };
104-
currency.versions.splice(installedVersionIndex, 0, newVersionObj);
105-
}
113+
const newVersionObj =
114+
typeof installedVersionObj === 'string' ? latestVersion : { ...installedVersionObj, v: latestVersion };
115+
116+
currency.versions = currency.versions.filter(vObj => {
117+
const v = typeof vObj === 'string' ? vObj : vObj.v;
118+
return v !== installedVersion;
119+
});
120+
currency.versions.splice(installedIndex, 0, newVersionObj);
106121

107122
if (!DRY_RUN) {
108-
fs.writeFileSync(path.join(__dirname, '..', '..', '..', 'currencies.json'), JSON.stringify(currencies, null, 2));
123+
fs.writeFileSync(currenciesPath, JSON.stringify(currencies, null, 2));
109124
} else {
110125
console.log(`[DRY RUN] Updated currencies.json with ${currency.name} version ${latestVersion}`);
111126
}
@@ -125,31 +140,27 @@ currencies.forEach(currency => {
125140
try {
126141
execSync("git add 'currencies.json'", { cwd });
127142
execSync(`git commit -m "build: bumped ${currency.name} from ${installedVersion} to ${latestVersion}"`, { cwd });
128-
} catch (error) {
129-
console.error(`Failed to commit changes: ${error.message}`);
143+
} catch (err) {
144+
console.error(`[ERROR] Commit failed: ${err.message}`);
130145
}
131-
} else {
132-
// eslint-disable-next-line max-len
133-
console.log(
134-
`[DRY RUN] git commit -m "build: bumped ${currency.name} from ${installedVersion} to ${latestVersion}"`
135-
);
136146
}
137147
});
138148

139149
// For non-major updates, push all changes at once
140150
if (!MAJOR_UPDATES_MODE) {
141-
if (utils.hasCommits(branchName, cwd)) {
151+
if (utils.hasCommits(BRANCH, cwd)) {
142152
if (!SKIP_PUSH) {
143153
try {
144-
execSync(`git push origin ${branchName} --no-verify`, { cwd });
145-
// eslint-disable-next-line max-len
154+
execSync(`git push origin ${BRANCH} --no-verify`, { cwd });
146155
const prTitle = '[Currency Bot] Bumped patch/minor dependencies';
147-
execSync(`gh pr create --base main --head ${branchName} --title "${prTitle}" --body "Tada!"`, { cwd });
156+
execSync(`gh pr create --base main --head ${BRANCH} --title "${prTitle}" --body "Tada!"`, { cwd });
157+
158+
console.log('[DONE] Currency PR created');
148159
} catch (error) {
149-
console.error(`Failed to push changes: ${error.message}`);
160+
console.error(`[ERROR] Failed to push changes:: ${error.message}`);
150161
}
151162
}
152163
} else {
153-
console.log(`Branch ${branchName} has no commits.`);
164+
console.log(`Branch ${BRANCH} has no commits.`);
154165
}
155166
}

0 commit comments

Comments
 (0)