ci: add node engine check (#7574)

* add issue bot for prs

* Update CHANGELOG.md

* Update issue-bot.yml

* replace node 15 with node 16

* Update CHANGELOG.md

* use node 16 as default node version

* ignore node 15 in ci self-check

* bumped madge for node deprecation DEP0148

* ci: add node engine check

* lint

* bump node engine

* Update ci.yml

* revert unnecessary changes

* Update CHANGELOG.md

* Update ci.yml
This commit is contained in:
Manuel
2021-09-14 16:29:56 +02:00
committed by GitHub
parent 3e4d1ecbf3
commit e9e3be1df8
7 changed files with 281 additions and 79 deletions

290
ci/CiVersionCheck.js Normal file
View File

@@ -0,0 +1,290 @@
const core = require('@actions/core');
const semver = require('semver');
const yaml = require('yaml');
const fs = require('fs').promises;
/**
* This checks the CI version of an environment variable in a YAML file
* against a list of released versions of a package.
*/
class CiVersionCheck {
/**
* The constructor.
* @param {Object} config The config.
* @param {String} config.packageName The package name to check.
* @param {String} config.packageSupportUrl The URL to the package website
* that shows the End-of-Life support dates.
* @param {String} config.yamlFilePath The path to the GitHub workflow YAML
* file that contains the tests.
* @param {String} config.ciEnvironmentsKeyPath The key path in the CI YAML
* file to the environment specifications.
* @param {String} config.ciVersionKey The key in the CI YAML file to
* determine the package version.
* @param {Array<String>} config.releasedVersions The released versions of
* the package to check against.
* @param {Array<String>} config.ignoreReleasedVersions The versions to
* ignore when checking whether the CI tests against the latest versions.
* This can be used in case there is a package release for which Parse
* Server compatibility is not required.
* @param {String} [config.latestComponent='patch'] The version component
* (`major`, `minor`, `patch`) that must be the latest released version.
* Default is `patch`.
*
* For example:
* - Released versions: 1.0.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, 2.0.0
* - Tested version: 1.2.0
*
* If the latest version component is `patch`, then the check would
* fail and recommend an upgrade to version 1.2.1 and to add additional
* tests against 1.3.1 and 2.0.0.
* If the latest version component is `minor` then the check would
* fail and recommend an upgrade to version 1.3.0 and to add an additional
* test against 2.0.0.
* If the latest version component is `major` then the check would
* fail and recommend an upgrade to version 2.0.0.
*/
constructor(config) {
const {
packageName,
packageSupportUrl,
yamlFilePath,
ciEnvironmentsKeyPath,
ciVersionKey,
releasedVersions,
ignoreReleasedVersions = [],
latestComponent = CiVersionCheck.versionComponents.patch,
} = config;
// Ensure required params are set
if ([
packageName,
packageSupportUrl,
yamlFilePath,
ciEnvironmentsKeyPath,
ciVersionKey,
releasedVersions,
].includes(undefined)) {
throw 'invalid configuration';
}
if (!Object.keys(CiVersionCheck.versionComponents).includes(latestComponent)) {
throw 'invalid configuration for latestComponent';
}
this.packageName = packageName;
this.packageSupportUrl = packageSupportUrl;
this.yamlFilePath = yamlFilePath;
this.ciEnvironmentsKeyPath = ciEnvironmentsKeyPath;
this.ciVersionKey = ciVersionKey;
this.releasedVersions = releasedVersions;
this.ignoreReleasedVersions = ignoreReleasedVersions;
this.latestComponent = latestComponent;
}
/**
* The definition of version components.
*/
static get versionComponents() {
return Object.freeze({
major: 'major',
minor: 'minor',
patch: 'patch',
});
}
/**
* Returns the test environments as specified in the YAML file.
*/
async getTests() {
try {
// Get CI workflow
const ciYaml = await fs.readFile(this.yamlFilePath, 'utf-8');
const ci = yaml.parse(ciYaml);
// Extract package versions
let versions = this.ciEnvironmentsKeyPath.split('.').reduce((o,k) => o !== undefined ? o[k] : undefined, ci);
versions = Object.entries(versions)
.map(entry => entry[1])
.filter(entry => entry[this.ciVersionKey]);
return versions;
} catch (e) {
throw `Failed to determine ${this.packageName} versions from CI YAML file with error: ${e}`;
}
}
/**
* Returns the package versions which are missing in the CI environment.
* @param {Array<String>} releasedVersions The released versions; need to
* be sorted descending.
* @param {Array<String>} testedVersions The tested versions.
* @param {String} versionComponent The latest version component.
* @returns {Array<String>} The untested versions.
*/
getUntestedVersions(releasedVersions, testedVersions, versionComponent) {
// Use these example values for debugging the version range logic below
// versionComponent = CiVersionCheck.versionComponents.patch;
// this.ignoreReleasedVersions = ['<4.4.0', '~4.7.0'];
// testedVersions = ['4.4.3'];
// releasedVersions = [
// '5.0.0-rc0',
// '5.0.0',
// '4.9.1',
// '4.9.0',
// '4.8.1',
// '4.8.0',
// '4.7.1',
// '4.7.0',
// '4.4.3',
// '4.4.2',
// '4.4.0',
// '4.1.0',
// '3.5.0',
// ];
// Determine operator for range comparison
const operator = versionComponent == CiVersionCheck.versionComponents.major
? '>='
: versionComponent == CiVersionCheck.versionComponents.minor
? '^'
: '~'
// Get all untested versions
const untestedVersions = releasedVersions.reduce((m, v) => {
// If the version should be ignored, skip it
if (this.ignoreReleasedVersions.length > 0 && semver.satisfies(v, this.ignoreReleasedVersions.join(' || '))) {
return m;
}
// If the version is a pre-release, skip it
if ((semver.prerelease(v) || []).length > 0) {
return m;
}
// If a satisfying version has already been added to untested, skip it
if (semver.maxSatisfying(m, `${operator}${v}`)) {
return m;
}
// If a satisfying version is already tested, skip it
if (semver.maxSatisfying(testedVersions, `${operator}${v}`)) {
return m;
}
// Add version
m.push(v);
return m;
}, []);
return untestedVersions;
}
/**
* Returns the latest version for a given version and component.
* @param {Array<String>} versions The versions in which to search.
* @param {String} version The version for which a newer version
* should be searched.
* @param {String} versionComponent The version component up to
* which the latest version should be checked.
* @returns {String|undefined} The newer version.
*/
getNewerVersion(versions, version, versionComponent) {
// Determine operator for range comparison
const operator = versionComponent == CiVersionCheck.versionComponents.major
? '>='
: versionComponent == CiVersionCheck.versionComponents.minor
? '^'
: '~'
const latest = semver.maxSatisfying(versions, `${operator}${version}`);
// If the version should be ignored, skip it
if (this.ignoreReleasedVersions.length > 0 && semver.satisfies(latest, this.ignoreReleasedVersions.join(' || '))) {
return undefined;
}
// Return the latest version if it is newer than any currently used version
return semver.gt(latest, version) ? latest : undefined;
}
/**
* This validates that the given versions strictly follow semver
* syntax.
* @param {Array<String>} versions The versions to check.
*/
_validateVersionSyntax(versions) {
for (const version of versions) {
if (!semver.valid(version)) {
throw version;
}
}
}
/**
* Runs the check.
*/
async check() {
try {
console.log(`\nChecking ${this.packageName} versions in CI environments...`);
// Validate released versions syntax
try {
this._validateVersionSyntax(this.releasedVersions);
} catch (e) {
core.setFailed(`Failed to check ${this.packageName} versions because released version '${e}' does not follow semver syntax (x.y.z).`);
return;
}
// Sort versions descending
semver.sort(this.releasedVersions).reverse()
// Get tested package versions from CI
const tests = await this.getTests();
// Is true if any of the checks failed
let failed = false;
// Check whether each tested version is the latest patch
for (const test of tests) {
const version = test[this.ciVersionKey];
// Validate version syntax
try {
this._validateVersionSyntax([version]);
} catch (e) {
core.setFailed(`Failed to check ${this.packageName} versions because environment version '${e}' does not follow semver syntax (x.y.z).`);
return;
}
const newer = this.getNewerVersion(this.releasedVersions, version, this.latestComponent);
if (newer) {
console.log(`❌ CI environment '${test.name}' uses an old ${this.packageName} ${this.latestComponent} version ${version} instead of ${newer}.`);
failed = true;
} else {
console.log(`✅ CI environment '${test.name}' uses the latest ${this.packageName} ${this.latestComponent} version ${version}.`);
}
}
// Check whether there is a newer component version available that is not tested
const testedVersions = tests.map(test => test[this.ciVersionKey]);
const untested = this.getUntestedVersions(this.releasedVersions, testedVersions, this.latestComponent);
if (untested.length > 0) {
console.log(`❌ CI does not have environments using the following versions of ${this.packageName}: ${untested.join(', ')}.`);
failed = true;
} else {
console.log(`✅ CI has environments using all recent versions of ${this.packageName}.`);
}
if (failed) {
core.setFailed(
`CI environments are not up-to-date with the latest ${this.packageName} versions.` +
`\n\nCheck the error messages above and update the ${this.packageName} versions in the CI YAML ` +
`file.\n\n Additionally, there may be versions of ${this.packageName} that have reached their official end-of-life ` +
`support date and should be removed from the CI, see ${this.packageSupportUrl}.`
);
}
} catch (e) {
const msg = `Failed to check ${this.packageName} versions with error: ${e}`;
core.setFailed(msg);
}
}
}
module.exports = CiVersionCheck;

70
ci/ciCheck.js Normal file
View File

@@ -0,0 +1,70 @@
'use strict'
const CiVersionCheck = require('./CiVersionCheck');
const mongoVersionList = require('mongodb-version-list');
const allNodeVersions = require('all-node-versions');
async function check() {
// Run checks
await checkMongoDbVersions();
await checkNodeVersions();
}
/**
* Check the MongoDB versions used in test environments.
*/
async function checkMongoDbVersions() {
const releasedVersions = await new Promise((resolve, reject) => {
mongoVersionList(function(error, versions) {
if (error) {
reject(error);
}
resolve(versions);
});
});
await new CiVersionCheck({
packageName: 'MongoDB',
packageSupportUrl: 'https://www.mongodb.com/support-policy',
yamlFilePath: './.github/workflows/ci.yml',
ciEnvironmentsKeyPath: 'jobs.check-mongo.strategy.matrix.include',
ciVersionKey: 'MONGODB_VERSION',
releasedVersions,
latestComponent: CiVersionCheck.versionComponents.path,
ignoreReleasedVersions: [
'<4.0.0', // Versions reached their MongoDB end-of-life support date
'~4.1.0', // Development release according to MongoDB support
'~4.3.0', // Development release according to MongoDB support
'~4.7.0', // Development release according to MongoDB support
'4.0.26', // Temporarily disabled because not yet available for download via mongodb-runner
],
}).check();
}
/**
* Check the Nodejs versions used in test environments.
*/
async function checkNodeVersions() {
const allVersions = await allNodeVersions();
const releasedVersions = allVersions.versions;
await new CiVersionCheck({
packageName: 'Node.js',
packageSupportUrl: 'https://github.com/nodejs/node/blob/master/CHANGELOG.md',
yamlFilePath: './.github/workflows/ci.yml',
ciEnvironmentsKeyPath: 'jobs.check-mongo.strategy.matrix.include',
ciVersionKey: 'NODE_VERSION',
releasedVersions,
latestComponent: CiVersionCheck.versionComponents.minor,
ignoreReleasedVersions: [
'<12.0.0', // These versions have reached their end-of-life support date
'>=13.0.0 <14.0.0', // These versions have reached their end-of-life support date
'>=16.0.0', // This version has not been officially released yet
],
}).check();
}
check();

190
ci/nodeEngineCheck.js Normal file
View File

@@ -0,0 +1,190 @@
const core = require('@actions/core');
const semver = require('semver');
const fs = require('fs').promises;
const path = require('path');
/**
* This checks whether any package dependency requires a minimum node engine
* version higher than the host package.
*/
class NodeEngineCheck {
/**
* The constructor.
* @param {Object} config The config.
* @param {String} config.nodeModulesPath The path to the node_modules directory.
* @param {String} config.packageJsonPath The path to the parent package.json file.
*/
constructor(config) {
const {
nodeModulesPath,
packageJsonPath,
} = config;
// Ensure required params are set
if ([
nodeModulesPath,
packageJsonPath,
].includes(undefined)) {
throw 'invalid configuration';
}
this.nodeModulesPath = nodeModulesPath;
this.packageJsonPath = packageJsonPath;
}
/**
* Returns an array of `package.json` files under the given path and subdirectories.
* @param {String} [basePath] The base path for recursive directory search.
*/
async getPackageFiles(basePath = this.nodeModulesPath) {
try {
// Declare file list
const files = []
// Get files
const dirents = await fs.readdir(basePath, { withFileTypes: true });
const validFiles = dirents.filter(d => d.name.toLowerCase() == 'package.json').map(d => path.join(basePath, d.name));
files.push(...validFiles);
// For each directory entry
for (const dirent of dirents) {
if (dirent.isDirectory()) {
const subFiles = await this.getPackageFiles(path.join(basePath, dirent.name));
files.push(...subFiles);
}
}
return files;
} catch (e) {
throw `Failed to get package.json files in ${this.nodeModulesPath} with error: ${e}`;
}
}
/**
* Extracts and returns the node engine versions of the given package.json
* files.
* @param {String[]} files The package.json files.
* @param {Boolean} clean Is true if packages with undefined node versions
* should be removed from the results.
* @returns {Object[]} A list of results.
*/
async getNodeVersion({ files, clean = false }) {
// Declare response
let response = [];
// For each file
for (const file of files) {
// Get node version
const contentString = await fs.readFile(file, 'utf-8');
const contentJson = JSON.parse(contentString);
const version = ((contentJson || {}).engines || {}).node;
// Add response
response.push({
file: file,
nodeVersion: version
});
}
// If results should be cleaned by removing undefined node versions
if (clean) {
response = response.filter(r => r.nodeVersion !== undefined);
}
return response;
}
/**
* Returns the highest semver definition that satisfies all versions
* in the given list.
* @param {String[]} versions The list of semver version ranges.
* @param {String} baseVersion The base version of which higher versions should be
* determined; as a version (1.2.3), not a range (>=1.2.3).
* @returns {String} The highest semver version.
*/
getHigherVersions({ versions, baseVersion }) {
// Add min satisfying node versions
const minVersions = versions.map(v => {
v.nodeMinVersion = semver.minVersion(v.nodeVersion)
return v;
});
// Sort by min version
const sortedMinVersions = minVersions.sort((v1, v2) => semver.compare(v1.nodeMinVersion, v2.nodeMinVersion));
// Filter by higher versions
const higherVersions = sortedMinVersions.filter(v => semver.gt(v.nodeMinVersion, baseVersion));
// console.log(`getHigherVersions: ${JSON.stringify(higherVersions)}`);
return higherVersions;
}
/**
* Returns the node version of the parent package.
* @return {Object} The parent package info.
*/
async getParentVersion() {
// Get parent package.json version
const version = await this.getNodeVersion({ files: [ this.packageJsonPath ], clean: true });
// console.log(`getParentVersion: ${JSON.stringify(version)}`);
return version[0];
}
}
async function check() {
// Define paths
const nodeModulesPath = path.join(__dirname, '../node_modules');
const packageJsonPath = path.join(__dirname, '../package.json');
// Create check
const check = new NodeEngineCheck({
nodeModulesPath,
packageJsonPath,
});
// Get package node version of parent package
const parentVersion = await check.getParentVersion();
// If parent node version could not be determined
if (parentVersion === undefined) {
core.setFailed(`Failed to determine node engine version of parent package at ${this.packageJsonPath}`);
return;
}
// Determine parent min version
const parentMinVersion = semver.minVersion(parentVersion.nodeVersion);
// Get package.json files
const files = await check.getPackageFiles();
core.info(`Checking the minimum node version requirement of ${files.length} dependencies`);
// Get node versions
const versions = await check.getNodeVersion({ files, clean: true });
// Get are dependencies that require a higher node version than the parent package
const higherVersions = check.getHigherVersions({ versions, baseVersion: parentMinVersion });
// Get highest version
const highestVersion = higherVersions.map(v => v.nodeMinVersion).pop();
// If there are higher versions
if (higherVersions.length > 0) {
console.log(`\nThere are ${higherVersions.length} dependencies that require a higher node engine version than the parent package (${parentVersion.nodeVersion}):`);
// For each dependency
for (const higherVersion of higherVersions) {
// Get package name
const _package = higherVersion.file.split('node_modules/').pop().replace('/package.json', '');
console.log(`- ${_package} requires at least node ${higherVersion.nodeMinVersion} (${higherVersion.nodeVersion})`);
}
console.log('');
core.setFailed(`❌ Upgrade the node engine version in package.json to at least '${highestVersion}' to satisfy the dependencies.`);
console.log('');
return;
}
console.log(`✅ All dependencies satisfy the node version requirement of the parent package (${parentVersion.nodeVersion}).`);
}
check();