|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright Elasticsearch B.V. and other contributors where applicable. |
| 5 | + * Licensed under the BSD 2-Clause License; you may not use this file except in |
| 6 | + * compliance with the BSD 2-Clause License. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict' |
| 10 | + |
| 11 | +// Check that the ".ci/tav.json" file includes all the modules defined in |
| 12 | +// the various ".tav.yml" files. If not, then CI "TAV" runs are not testing |
| 13 | +// all modules. |
| 14 | +// |
| 15 | +// Also check that the product of "versions" and "modules" in tav.json does |
| 16 | +// not exceed the 256 GH Actions limit. |
| 17 | +// https://docs.github.com/en/actions/learn-github-actions/usage-limits-billing-and-administration#usage-limits |
| 18 | + |
| 19 | +const fs = require('fs') |
| 20 | +const path = require('path') |
| 21 | + |
| 22 | +const glob = require('glob') |
| 23 | +const yaml = require('js-yaml') |
| 24 | + |
| 25 | +const TOP = path.resolve(path.join(__dirname, '..')) |
| 26 | +const TAV_JSON_PATH = path.join('.ci', 'tav.json') |
| 27 | + |
| 28 | +// ---- mainline |
| 29 | + |
| 30 | +function main (argv) { |
| 31 | + let numErrors = 0 |
| 32 | + |
| 33 | + // Find all module names in ".tav.yml" files. |
| 34 | + const moduleNamesFromYaml = new Set() |
| 35 | + const tavYmlPaths = glob.sync( |
| 36 | + '**/.tav.yml', |
| 37 | + { ignore: ['**/node_modules/**'] } |
| 38 | + ) |
| 39 | + // console.log('tavYmlPaths:', tavYmlPaths) |
| 40 | + tavYmlPaths.forEach(p => { |
| 41 | + const tavCfg = yaml.load(fs.readFileSync(p, 'utf8')) |
| 42 | + Object.keys(tavCfg).forEach(k => { |
| 43 | + const v = tavCfg[k] |
| 44 | + moduleNamesFromYaml.add(v.name || k) |
| 45 | + }) |
| 46 | + }) |
| 47 | + // console.log('moduleNamesFromYaml: ', moduleNamesFromYaml) |
| 48 | + |
| 49 | + // Find module names in ".ci/tav.json". |
| 50 | + const moduleNamesFromJson = new Set() |
| 51 | + const tavJson = JSON.parse(fs.readFileSync(path.join(TOP, TAV_JSON_PATH))) |
| 52 | + tavJson.modules.forEach(m => { |
| 53 | + m.split(',').forEach(moduleName => { |
| 54 | + moduleNamesFromJson.add(moduleName) |
| 55 | + }) |
| 56 | + }) |
| 57 | + // console.log('moduleNamesFromJson: ', moduleNamesFromJson) |
| 58 | + |
| 59 | + // Matrix 256 limit. |
| 60 | + const matrixSize = tavJson.versions.length * tavJson.modules.length |
| 61 | + if (matrixSize > 256) { |
| 62 | + console.error('lint-tav-json: #versions * #modules from "%s" is >256, which exceeds the GH Actions workflow matrix limit: %d', |
| 63 | + TAV_JSON_PATH, matrixSize) |
| 64 | + numErrors += 1 |
| 65 | + } |
| 66 | + |
| 67 | + // Error out if any discrepancies. |
| 68 | + const missingFromYaml = new Set([...moduleNamesFromJson].filter((x) => !moduleNamesFromYaml.has(x))) |
| 69 | + if (missingFromYaml.size > 0) { |
| 70 | + console.error('lint-tav-json: the following %d module name(s) are in "%s" but not in the "**/.tav.yml" files: %s', |
| 71 | + missingFromYaml.size, TAV_JSON_PATH, missingFromYaml) |
| 72 | + numErrors += 1 |
| 73 | + } |
| 74 | + const missingFromJson = new Set([...moduleNamesFromYaml].filter((x) => !moduleNamesFromJson.has(x))) |
| 75 | + if (missingFromJson.size > 0) { |
| 76 | + console.error('lint-tav-json: the following %d module name(s) are in the "**/.tav.yml" files but not in "%s": %s', |
| 77 | + missingFromJson.size, TAV_JSON_PATH, missingFromJson) |
| 78 | + numErrors += 1 |
| 79 | + } |
| 80 | + |
| 81 | + if (numErrors > 0) { |
| 82 | + process.exitCode = 1 |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +if (require.main === module) { |
| 87 | + main(process.argv) |
| 88 | +} |
0 commit comments