#!/usr/bin/env node /* * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 */ /** * Re-generate a ".github/component-label-map.yml". */ import fs from 'fs'; import { join, resolve, relative, dirname, basename } from 'path'; import { globSync } from 'glob'; import yaml from 'js-yaml'; // Currently a cheat, because relying on transitive deps for this. const TOP = resolve(import.meta.dirname, '..'); const SCRIPT_FILE = relative(TOP, import.meta.filename); const CO_FILE = relative( process.cwd(), join(TOP, '.github', 'component_owners.yml') ); const CLM_FILE = relative( process.cwd(), join(TOP, '.github', 'component-label-map.yml') ); const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0; let numWarns = 0; function warn(...args) { numWarns += 1; if (USE_COLOR) { process.stdout.write('\x1b[31m'); } process.stdout.write('gen-component-label-map warning: '); if (USE_COLOR) { process.stdout.write('\x1b[39m'); } console.log(...args); } function getAllWorkspaceDirs() { const pj = JSON.parse(fs.readFileSync(join(TOP, 'package.json'), 'utf8')); return pj.workspaces .map(wsGlob => globSync(`${wsGlob}/package.json`, { posix: true })) .flat() .map(dirname); } // mainline const wsDirs = new Set(getAllWorkspaceDirs()); // Gather data from ".github/component_owners.yml". const co = yaml.load(fs.readFileSync(CO_FILE, 'utf8')); const coComponents = new Set(Object.keys(co.components)); const coMissing = wsDirs.difference(coComponents); // Requires Node.js >=22. if (coMissing.size) { warn( `"${CO_FILE}" is missing ${coMissing.size === 1 ? 'an entry' : 'entries'} for: ${Array.from(coMissing).join(', ')}` ); } const coExtraneous = coComponents.difference(wsDirs); if (coExtraneous.size) { warn( `"${CO_FILE}" has ${coExtraneous.size === 1 ? 'an extraneous entry' : 'extraneous entries'} for: ${Array.from(coExtraneous).join(', ')}` ); } // Get set of unmaintained components. const unmaintainedPkgDirs = new Set(); for (const [pkgDir, maintainers] of Object.entries(co.components)) { if (maintainers.length === 0) { unmaintainedPkgDirs.add(pkgDir); } } // Build data for `pkg:*` enties in component-label-map.yml. const pjFromPkgDir = {}; const pkgDirFromPkgName = {}; // map '@opentelemetry/foo' -> 'packages/foo' for (const pkgDir of wsDirs) { const pj = JSON.parse(fs.readFileSync(join(pkgDir, 'package.json'))); pjFromPkgDir[pkgDir] = pj; pkgDirFromPkgName[pj.name] = pkgDir; } const globsFromPkgBase = {}; for (const pkgDir of Array.from(wsDirs).sort()) { const pkgBase = basename(pkgDir); const pj = pjFromPkgDir[pkgDir]; // A `pkg:foo` component label depends on: // 1. changes in its package dir (`packages/foo/**`) // 2. changes in contrib-test-utils if in its devDependencies // 3. changes in its local dependencies (e.g. redis-common for instr-ioredis) // // with these exceptions: // - Other local devDependencies are currently *ignored*, e.g. // instr-aws-lambda has a devDep on `@opentelemetry/propagator-aws-xray` // that is NOT included here. // - The `auto-*` packages do *not* include their local deps. // I believe this is to avoid CI having to run tests for // `auto-instrumentations-node` for any change in almost *any* // instrumentation package. const globs = [`${pkgDir}/**`]; let depsToConsider = []; if (pj.devDependencies?.['@opentelemetry/contrib-test-utils']) { depsToConsider.push('@opentelemetry/contrib-test-utils'); } if (!pkgBase.startsWith('auto-')) { depsToConsider = depsToConsider.concat(Object.keys(pj.dependencies ?? {})); } for (const dep of depsToConsider) { if (pkgDirFromPkgName[dep]) { globs.push(`${pkgDirFromPkgName[dep]}/**`); } } globsFromPkgBase[pkgBase] = globs; } // Build component-label-map.yml content. const clmParts = []; clmParts.push(`# Generated by "${SCRIPT_FILE}".\n\n`); // - the `pkg:{package basename}` entries for (const [pkgBase, globs] of Object.entries(globsFromPkgBase)) { clmParts.push(`pkg:${pkgBase}: - changed-files: - any-glob-to-any-file: ${globs.map(g => ` - ${g}`).join('\n')} `); } // - the `pkg-status:unmaintained` entry clmParts.push(` pkg-status:unmaintained: - changed-files: - any-glob-to-any-file: ${Array.from(unmaintainedPkgDirs) .sort() .map(d => ` - ${d}/**`) .join('\n')} `); const clmContent = clmParts.join(''); const currClmContent = fs.readFileSync(CLM_FILE, 'utf8'); if (clmContent !== currClmContent) { fs.writeFileSync(CLM_FILE, clmContent, 'utf8'); console.log(`Wrote "${CLM_FILE}".`); } else { console.log(`"${CLM_FILE}" unchanged.`); } // genSemconvTs(wsDir); if (numWarns > 0) { process.exitCode = 1; }