Files
nvke/hack/label-sync.js
Dan Budris 3ec540d72a ci(labels): keep the repository's labels in .github/labels.yml (#2313)
* ci(labels): keep the repository's labels in .github/labels.yml

.github/labels.yml lists every label with its colour and description.
label-sync.yml makes the repository match it: on main, and on a manual
run, it creates a listed label that is missing and updates one whose
colour or description differs. It never deletes or renames a label; a
label that exists only in the UI is logged and left alone.

On a pull request it validates the file (unique names, six-digit hex
colours, descriptions of at most 100 characters) and logs what a merge
would change, without writing.

The file matches today's labels, plus descriptions for the area/*
labels, which had none. area/security and nvidia-runners keep empty
descriptions until their purpose is written down.

Signed-off-by: dbudris <dbudris@nvidia.com>

* ci(labels): validate pull requests read-only, and apply main in order

The pull request run held issues: write, and only the script's own event
check stopped it writing. Split the workflow into two jobs: validate runs
on a pull request with read-only permissions, and apply runs on main or a
manual run with write access.

apply uses one concurrency group that is never cancelled, so two quick
merges cannot finish out of order: the newer pending run replaces the
older one, and each run applies the whole file.

The shared logic moves to hack/label-sync.js, which both jobs load.

Signed-off-by: dbudris <dbudris@nvidia.com>

* ci(labels): add area/management-engine and label its pull requests

management-engine/ has CODEOWNERS owners but no area label, so its pull
requests and issues land unrouted. Add area/management-engine to
labels.yml, and a labeler rule for management-engine/** and its
workflow files, matching the paths CODEOWNERS gives the team.

Signed-off-by: dbudris <dbudris@nvidia.com>

* ci(labels): apply the current main's labels.yml, not the triggering commit's

A re-run of an older push kept that push's GITHUB_SHA, so apply checked
out and restored the older labels.yml over a newer one. apply now checks
out the default branch, so every run applies the latest file.

Signed-off-by: dbudris <dbudris@nvidia.com>

* ci(labels): drop the removed nktp/ paths from label descriptions

Signed-off-by: dbudris <dbudris@nvidia.com>

---------

Signed-off-by: dbudris <dbudris@nvidia.com>
2026-11-19 10:13:03 -07:00

82 lines
3.4 KiB
JavaScript

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Makes the repository's labels match .github/labels.yml, converted to JSON
// at LABELS_JSON. Called from .github/workflows/label-sync.yml through
// actions/github-script. With apply false it validates and logs only.
module.exports = async ({ github, context, core, apply }) => {
const fs = require('fs');
const wanted = JSON.parse(fs.readFileSync(process.env.LABELS_JSON, 'utf8'));
// Validate the whole file before changing anything.
const errors = [];
const seen = new Set();
if (!Array.isArray(wanted)) {
errors.push('labels.yml must be a list of labels');
}
for (const [i, label] of (Array.isArray(wanted) ? wanted : []).entries()) {
const where = `entry ${i + 1}${label?.name ? ` (${label.name})` : ''}`;
if (typeof label?.name !== 'string' || label.name.trim() === '') {
errors.push(`${where}: name must be a non-empty string`);
continue;
}
const key = label.name.toLowerCase();
if (seen.has(key)) {
errors.push(`${where}: duplicate name; GitHub label names are case-insensitive`);
}
seen.add(key);
if (typeof label.color !== 'string' || !/^[0-9a-fA-F]{6}$/.test(label.color)) {
errors.push(`${where}: color must be six hex digits as a string, for example "ededed"`);
}
const description = label.description ?? '';
if (typeof description !== 'string') {
errors.push(`${where}: description must be a string`);
} else if (description.length > 100) {
errors.push(`${where}: description is ${description.length} characters; GitHub allows 100`);
}
}
if (errors.length > 0) {
errors.forEach((e) => core.error(e));
core.setFailed(`labels.yml has ${errors.length} error(s)`);
return;
}
const existing = await github.paginate(github.rest.issues.listLabelsForRepo, {
...context.repo,
per_page: 100,
});
const byName = new Map(existing.map((l) => [l.name.toLowerCase(), l]));
const tally = { created: 0, updated: 0, unchanged: 0 };
for (const label of wanted) {
const color = label.color.toLowerCase();
const description = label.description ?? '';
const current = byName.get(label.name.toLowerCase());
if (!current) {
core.info(`${apply ? 'create' : 'would create'}: ${label.name}`);
if (apply) {
await github.rest.issues.createLabel({ ...context.repo, name: label.name, color, description });
}
tally.created += 1;
} else if (current.color.toLowerCase() !== color || (current.description ?? '') !== description) {
core.info(`${apply ? 'update' : 'would update'}: ${label.name}`);
if (apply) {
// Address the label by its current name; the file's
// spelling is not applied, so case-only renames stay manual.
await github.rest.issues.updateLabel({ ...context.repo, name: current.name, color, description });
}
tally.updated += 1;
} else {
tally.unchanged += 1;
}
}
const listed = new Set(wanted.map((l) => l.name.toLowerCase()));
const unlisted = existing.filter((l) => !listed.has(l.name.toLowerCase())).map((l) => l.name);
if (unlisted.length > 0) {
core.notice(`Not in labels.yml, left alone: ${unlisted.join(', ')}`);
}
core.info(`${apply ? 'Applied' : 'Dry run'}: ${tally.created} created, ${tally.updated} updated, ${tally.unchanged} unchanged, ${unlisted.length} not listed`);
};