-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathupdate-prod-dependencies.js
More file actions
116 lines (94 loc) · 3.5 KB
/
update-prod-dependencies.js
File metadata and controls
116 lines (94 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* (c) Copyright IBM Corp. 2025
*/
'use strict';
const path = require('path');
const utils = require('../utils');
const BRANCH = process.env.BRANCH;
const SKIP_PUSH = process.env.SKIP_PUSH === 'true';
const PROD_DEPS_PR_LIMIT = process.env.PROD_DEPS_PR_LIMIT || 5;
const ORG_PR_LIMIT = process.env.ORG_PR_LIMIT || 2;
const SKIP_DEPS = process.env.SKIP_DEPS ? process.env.SKIP_DEPS.split(',').map(p => p.trim()) : [];
const cwd = path.join(__dirname, '..', '..', '..');
if (!BRANCH) throw new Error('Please set env variable "BRANCH".');
console.log(`BRANCH: ${BRANCH}`);
console.log(`SKIP_PUSH: ${SKIP_PUSH}`);
console.log(`PROD_DEPS_PR_LIMIT: ${PROD_DEPS_PR_LIMIT}`);
console.log(`ORG_PR_LIMIT: ${ORG_PR_LIMIT}`);
console.log(`SKIP_DEPS: ${SKIP_DEPS.length ? SKIP_DEPS.join(', ') : '(none)'}`);
const updatedProdDeps = [];
const orgPrCount = {};
const packagesDir = path.join(__dirname, '..', '..', '..', 'packages');
const pkgPaths = utils.getPackageJsonPathsUnderPackagesDir(packagesDir);
const dependencyMap = {};
pkgPaths.forEach(obj => {
const pkgJson = require(obj.pkgJsonAbsPath);
const deps = pkgJson.dependencies || {};
Object.entries(deps).forEach(([dep, version]) => {
// Exclude internal libraries
if (dep.startsWith('@instana')) return;
if (!dependencyMap[dep]) dependencyMap[dep] = [];
dependencyMap[dep].push({ pkgRelDir: obj.pkgRelDir, version });
});
});
Object.entries(dependencyMap).some(([dep, usageList]) => {
if (SKIP_DEPS.includes(dep)) {
console.log(`Skipping ${dep}. It is listed in SKIP_DEPS.`);
return false;
}
if (updatedProdDeps.length >= PROD_DEPS_PR_LIMIT) return true;
const orgName = dep.startsWith('@') ? dep.split('/')[0] : null;
if (orgName && (orgPrCount[orgName] || 0) >= ORG_PR_LIMIT) {
console.log(`Skipping ${dep}. ${orgName} has reached its PR limit (${ORG_PR_LIMIT}).`);
return false;
}
const currentVersion = utils.cleanVersionString(usageList[0].version);
const latestVersion = utils.getLatestVersion({
pkgName: dep,
installedVersion: currentVersion,
isBeta: false
});
if (!latestVersion || latestVersion === currentVersion) return false;
const branchName = utils.createBranchName(BRANCH, dep, latestVersion);
console.log(`Preparing PR for ${dep} (${currentVersion} -> ${latestVersion})`);
try {
if (utils.branchExists(branchName, cwd)) {
console.log(`Skipping ${dep}. Branch ${branchName} already exists.`);
return false;
}
utils.prepareGitEnvironment(branchName, cwd, BRANCH === 'main');
usageList.forEach(({ pkgRelDir }) => {
utils.installPackage({
packageName: dep,
version: latestVersion,
cwd,
saveFlag: '',
workspaceFlag: pkgRelDir
});
});
const prCreated = utils.commitAndCreatePR({
packageName: dep,
currentVersion: currentVersion,
newVersion: latestVersion,
branchName,
cwd,
skipPush: SKIP_PUSH,
prTitle: `[Prod Dependency Bot] Bumped ${dep} from ${currentVersion} to ${latestVersion}`
});
if (prCreated) {
updatedProdDeps.push(dep);
if (orgName) {
orgPrCount[orgName] = (orgPrCount[orgName] || 0) + 1;
}
}
} catch (err) {
console.error(`Failed updating ${dep}: ${err.message}`);
}
return updatedProdDeps.length >= PROD_DEPS_PR_LIMIT;
});
console.log('\nSummary:');
console.log(`Total PRs created: ${updatedProdDeps.length}`);
Object.entries(orgPrCount).forEach(([org, count]) => {
console.log(` ${org}: ${count} PR(s)`);
});
console.log('Done.');