|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * Copyright The OpenTelemetry Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * A script to check if some things (e.g. config files) have out of date data. |
| 20 | + * This can be useful for checking things where fully automated updating isn't |
| 21 | + * worth it. |
| 22 | + * |
| 23 | + * See "BITROT:" comments for things that are checked. |
| 24 | + * |
| 25 | + * Usage: |
| 26 | + * node scripts/bitrot.mjs |
| 27 | + */ |
| 28 | + |
| 29 | +import * as assert from 'assert'; |
| 30 | +import { execSync } from 'child_process'; |
| 31 | +import * as fs from 'fs'; |
| 32 | +import * as path from 'path'; |
| 33 | +import { fileURLToPath } from 'url'; |
| 34 | +import { globSync } from 'glob'; |
| 35 | + |
| 36 | +const TOP = path.resolve(fileURLToPath(new URL('.', import.meta.url)), '..'); |
| 37 | +const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0; |
| 38 | +const BUILD_DIR = path.join(TOP, 'build', 'bitrot'); |
| 39 | + |
| 40 | +let numProbs = 0; |
| 41 | +function problem(...args) { |
| 42 | + numProbs += 1; |
| 43 | + if (USE_COLOR) { |
| 44 | + process.stdout.write('\x1b[31m'); |
| 45 | + } |
| 46 | + args.unshift('bitrot error:'); |
| 47 | + console.log(...args); |
| 48 | + if (USE_COLOR) { |
| 49 | + process.stdout.write('\x1b[39m'); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function gitCloneSync(repo, dir) { |
| 54 | + execSync(`git clone ${repo} "${dir}"`) |
| 55 | +} |
| 56 | +function gitPullSync(cwd) { |
| 57 | + execSync('git pull', {cwd}) |
| 58 | +} |
| 59 | + |
| 60 | +function isPublicPackage(pj) { |
| 61 | + if (pj.private === true) { |
| 62 | + return false; |
| 63 | + } else if (pj.publishConfig?.access) { |
| 64 | + return (pj.publishConfig.access === 'public'); |
| 65 | + } else { |
| 66 | + // Default is *false* for scoped packages. |
| 67 | + return (!pj.name.startsWith('@')); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * BITROT: Check that the `"groupName": "OTel Core experimental",` |
| 73 | + * group in renovate.json matches the actual current set of experimental |
| 74 | + * packages from the core repo. |
| 75 | + */ |
| 76 | +function bitrotRenovateCoreExperimental() { |
| 77 | + const renovateJson = path.join(TOP, 'renovate.json'); |
| 78 | + const renovate = JSON.parse(fs.readFileSync(renovateJson)); |
| 79 | + const group = renovate.packageRules.filter(r => r.groupName === 'OTel Core experimental')[0]; |
| 80 | + assert.ok(group, `found "OTel Core experimental" group in ${renovateJson}`); |
| 81 | + |
| 82 | + const ojDir = path.join(BUILD_DIR, 'opentelemetry-js'); |
| 83 | + if (fs.existsSync(ojDir)) { |
| 84 | + gitPullSync(ojDir); |
| 85 | + } else { |
| 86 | + gitCloneSync('https://github.com/open-telemetry/opentelemetry-js.git', ojDir); |
| 87 | + } |
| 88 | + |
| 89 | + const pkgNames = globSync(path.join(ojDir, 'experimental/packages/*/package.json')) |
| 90 | + .map(packageJson => JSON.parse(fs.readFileSync(packageJson))) |
| 91 | + .filter(pj => isPublicPackage(pj)) |
| 92 | + .map(pj => pj.name); |
| 93 | + |
| 94 | + const pkgNamesSet = new Set(pkgNames); |
| 95 | + const renovateSet = new Set(group.matchPackageNames); |
| 96 | + const missing = pkgNamesSet.difference(renovateSet); // requires Node.js >=22 |
| 97 | + const extraneous = renovateSet.difference(pkgNamesSet); |
| 98 | + |
| 99 | + const issues = []; |
| 100 | + if (missing.size) { |
| 101 | + issues.push(`missing entries: ${JSON.stringify(Array.from(missing))}`); |
| 102 | + } |
| 103 | + if (extraneous.size) { |
| 104 | + issues.push(`extraneous entries: ${JSON.stringify(Array.from(extraneous))}`); |
| 105 | + } |
| 106 | + if (issues.length) { |
| 107 | + problem(`${renovateJson}: "matchPackageNames" in the "OTel Core experimental" group does not match the current set experimental packages from the opentelemetry-js.git repo:\n - ${issues.join('\n - ')}\nThe "matchPackageNames" should be:\n${JSON.stringify(pkgNames.sort(), null, 2)}`); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function bitrot() { |
| 112 | + bitrotRenovateCoreExperimental(); |
| 113 | +} |
| 114 | + |
| 115 | +// ---- mainline |
| 116 | + |
| 117 | +await bitrot(); |
| 118 | +if (numProbs > 0) { |
| 119 | + process.exitCode = 1; |
| 120 | +} |
| 121 | + |
0 commit comments