From aad00cdba2bb896068f4a896cf67df2a7bd79a73 Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Thu, 9 Jul 2026 01:34:25 +0300 Subject: [PATCH] Fix #5442: outgoing webhooks now include the label name on add/remove label Labels are embedded in the board document, not a separate collection, but the Activities label() helper looked the label id up in the Cards collection and always returned undefined. So the addedLabel/removedLabel webhook and notification params never got a label value and the __label__ token in the message text rendered as a bare, generic "label" with no name. Resolve the label from the already-loaded board via getLabelById, and add a pure labelDisplayName() helper (name -> color -> id fallback) so a nameless label is shown by its color, matching the UI. Add a plain-Node unit test. --- models/lib/labelDisplayName.js | 24 +++++++++++++++ server/models/activities.js | 22 ++++++++------ tests/labelDisplayName.test.cjs | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 models/lib/labelDisplayName.js create mode 100644 tests/labelDisplayName.test.cjs diff --git a/models/lib/labelDisplayName.js b/models/lib/labelDisplayName.js new file mode 100644 index 0000000000..58eff70ffa --- /dev/null +++ b/models/lib/labelDisplayName.js @@ -0,0 +1,24 @@ +'use strict'; + +// Pure helper: resolve a board label's human-readable display name for +// notification / outgoing-webhook text. Extracted so the fallback logic is +// unit-testable in plain Node without Meteor (mirrors +// models/lib/boardSortReorder.js and models/lib/cardSearch.js). +// +// #5442 (outgoing webhook on add/remove label did not include the label name): +// a WeKan label has an optional `name` and a `color`. In the UI a label with an +// empty name is shown by its color, so the webhook must do the same instead of +// emitting a bare, generic "label". Preference order: name, then color, then the +// label id, so the resolved text is never empty. +function labelDisplayName(label) { + if (!label) return ''; + const name = typeof label.name === 'string' ? label.name.trim() : ''; + if (name) return label.name; + const color = typeof label.color === 'string' ? label.color.trim() : ''; + if (color) return label.color; + return typeof label._id === 'string' ? label._id : ''; +} + +module.exports = { + labelDisplayName, +}; diff --git a/server/models/activities.js b/server/models/activities.js index f38a243c79..f45468da48 100644 --- a/server/models/activities.js +++ b/server/models/activities.js @@ -9,6 +9,7 @@ import { RulesHelper } from '/server/rulesHelper'; import { Notifications } from '/server/notifications/notifications'; import { ensureIndex } from '/server/lib/mongoStartup'; import { safeDeliver } from '/server/lib/webhookGuard'; +import { labelDisplayName } from '/models/lib/labelDisplayName'; function normalizeActivityText(value, fallback = '') { return typeof value === 'string' ? value : fallback; @@ -288,17 +289,18 @@ Activities.after.insert(async (userId, doc) => { } } - if (activity.labelId) { - const label = await activity.label(); + if (activity.labelId && board) { + // #5442: labels are embedded in the board document (not a separate + // collection), so resolve the label from the already-loaded board by id. + // The old activity.label() helper looked the id up in Cards and always + // returned undefined, so params.label was never set and the outgoing + // webhook / notification text showed a bare, generic "label" with no name. + // Fall back to the color (how the UI shows a nameless label), then the id, + // so the label token is never empty. + const label = board.getLabelById(activity.labelId); if (label) { - if (label.name) { - params.label = normalizeActivityText(label.name); - } else if (label.color) { - params.label = normalizeActivityText(label.color); - } - if (label._id) { - params.labelId = label._id; - } + params.label = normalizeActivityText(labelDisplayName(label), label._id); + params.labelId = label._id; } } diff --git a/tests/labelDisplayName.test.cjs b/tests/labelDisplayName.test.cjs new file mode 100644 index 0000000000..12e7af5a0f --- /dev/null +++ b/tests/labelDisplayName.test.cjs @@ -0,0 +1,54 @@ +'use strict'; + +// Plain-Node unit test (no Meteor) for the outgoing-webhook label name helper. +// Run: node tests/labelDisplayName.test.cjs +// +// Regression guard for #5442 ("outgoing webhook on add/remove label did not +// include the label name"): a label has an optional `name` and a `color`, and a +// nameless label is shown by its color in the UI. The webhook text must resolve +// a real display value (name -> color -> id) instead of a bare, generic "label". + +const assert = require('assert'); +const { labelDisplayName } = require('../models/lib/labelDisplayName'); + +let passed = 0; +function test(name, fn) { + fn(); + passed += 1; + console.log(' ok -', name); +} + +test('name present -> the name is used', () => { + assert.strictEqual( + labelDisplayName({ _id: 'l1', name: 'Bug', color: 'red' }), + 'Bug', + ); +}); + +test('empty / whitespace name -> falls back to the color', () => { + assert.strictEqual( + labelDisplayName({ _id: 'l1', name: '', color: 'green' }), + 'green', + ); + assert.strictEqual( + labelDisplayName({ _id: 'l1', name: ' ', color: 'blue' }), + 'blue', + ); + assert.strictEqual( + labelDisplayName({ _id: 'l1', color: 'yellow' }), + 'yellow', + ); +}); + +test('no name and no color -> falls back to the label id', () => { + assert.strictEqual(labelDisplayName({ _id: 'l42' }), 'l42'); + assert.strictEqual(labelDisplayName({ _id: 'l42', name: '', color: '' }), 'l42'); +}); + +test('missing / null label -> empty string, never throws', () => { + assert.strictEqual(labelDisplayName(null), ''); + assert.strictEqual(labelDisplayName(undefined), ''); + assert.strictEqual(labelDisplayName({}), ''); +}); + +console.log(`\n${passed} passed`);