Skip to content

Commit 0040b87

Browse files
Add check versions CI step
1 parent b61584b commit 0040b87

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

ci/build-common-packages.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ jobs:
3131
displayName: Verify task source changes
3232
condition: and(succeeded(), ne(variables['Build.SourceBranch'], 'refs/heads/main'))
3333
34+
- script: node ./ci/check-versions.js
35+
displayName: Verify tasks' versions
36+
condition: and(succeeded(), ne(variables['Build.SourceBranch'], 'refs/heads/main'))
37+
3438
- script: |
3539
npm run test
3640
displayName: Test Common Npm packages

ci/check-versions.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const { execSync } = require('node:child_process');
2+
const { readFileSync } = require('node:fs');
3+
const { join } = require('node:path');
4+
const { styleText } = require('node:util');
5+
6+
const packages = [
7+
'artifacts-common',
8+
'az-blobstorage-provider',
9+
'azure-arm-rest',
10+
'azurermdeploycommon',
11+
'codeanalysis-common',
12+
'codecoverage-tools',
13+
'docker-common',
14+
'ios-signing-common',
15+
'java-common',
16+
'kubernetes-common',
17+
'msbuildhelpers',
18+
'packaging-common',
19+
'securefiles-common',
20+
'utility-common',
21+
'webdeployment-common',
22+
];
23+
24+
const gitDiffResult = execSync('git diff --name-only main', { encoding: 'utf8' }).split('\n');
25+
const changedFolders = new Set();
26+
27+
for (const filePath of gitDiffResult) {
28+
for (const pkg of packages) {
29+
// Check if the file path starts with the package folder name
30+
if (filePath.startsWith(`common-npm-packages/${pkg}/`)) {
31+
changedFolders.add(pkg);
32+
}
33+
}
34+
}
35+
36+
if (changedFolders.size > 0) {
37+
const errors = [];
38+
39+
for (const pkg of changedFolders) {
40+
const pkgJsonPath = join(__dirname, '../common-npm-packages', pkg, 'package.json');
41+
try {
42+
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
43+
const currentVersion = pkgJson.version;
44+
// Get package.json from main branch
45+
let mainVersion = null;
46+
try {
47+
const mainPkgJsonRaw = execSync(`git show main:common-npm-packages/${pkg}/package.json`, { encoding: 'utf8' });
48+
const mainPkgJson = JSON.parse(mainPkgJsonRaw);
49+
mainVersion = mainPkgJson.version;
50+
} catch (err) {
51+
// If package.json doesn't exist in main, treat as new package
52+
}
53+
54+
if (mainVersion && compareVersions(mainVersion, currentVersion) >= 0) {
55+
errors.push({
56+
pkg,
57+
mainVersion,
58+
currentVersion
59+
});
60+
}
61+
} catch (err) {
62+
console.warn(`Could not read version for ${pkg}:`, err.message);
63+
}
64+
}
65+
if (errors.length > 0) {
66+
console.error(styleText('red', 'Error: The following packages have not been updated correctly:'));
67+
errors.forEach(error => {
68+
console.error(`Local version ${styleText('green', error.currentVersion)} <= main version ${styleText('redBright', error.mainVersion)} for package ${styleText('blueBright', error.pkg)}.`);
69+
});
70+
process.exit(1);
71+
}
72+
} else {
73+
console.log('No changed package folders detected.');
74+
}
75+
76+
/**
77+
* Compare two semver strings. Returns 1 if a > b, 0 if a == b, -1 if a < b
78+
* @param {string} a - The first version string to compare
79+
* @param {string} b - The second version string to compare
80+
* @returns
81+
*/
82+
function compareVersions(a, b) {
83+
const pa = a.split('.').map(Number);
84+
const pb = b.split('.').map(Number);
85+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
86+
const na = pa[i] || 0;
87+
const nb = pb[i] || 0;
88+
if (na > nb) return 1;
89+
if (na < nb) return -1;
90+
}
91+
return 0;
92+
}

tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"module": "commonjs",
5+
"esModuleInterop": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"strict": true,
8+
"noEmit": true,
9+
"skipLibCheck": true,
10+
"allowJs": true,
11+
"checkJs": true,
12+
"useUnknownInCatchVariables": false
13+
},
14+
"include": [
15+
"ci"
16+
]
17+
}

0 commit comments

Comments
 (0)