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.
This commit is contained in:
Lauri Ojansivu committed 2026-07-09 02:14:07 +03:00
1 parent 07800da9af
commit aad00cdba2
3 files changed
+90 -10

No files matched your search

+24
View File
@@ -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,
};
+12 -10
View File
@@ -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;
}
}
+54
View File
@@ -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`);