A platform for community discussion. Free, open, simple.
DEV: Refactor deprecation workflow to support `notify-admin` handler (#36759)
Sérgio Saquetim4d05bf8a68c8
diff --git a/frontend/discourse/app/deprecation-workflow.js b/frontend/discourse/app/deprecation-workflow.jsindex 4b2737f35e7..ef1ebec2984 100644--- a/frontend/discourse/app/deprecation-workflow.js+++ b/frontend/discourse/app/deprecation-workflow.js@@ -25,7 +25,8 @@ const VALID_HANDLERS = [...VALID_EMBER_CLI_WORKFLOW_HANDLERS,"dont-throw","dont-count",- "counter",+ "count",+ "notify-admin",];/**@@ -45,57 +46,7 @@ export class DiscourseDeprecationWorkflow {* @param {(string|string[])} [workflows[].env] - Environment(s) where the workflow applies*/constructor(workflows) {- workflows.forEach((workflow) => {- // validate the deprecation handlers- workflow.handler ||= [];- workflow.handler = Array.isArray(workflow.handler)- ? workflow.handler- : [workflow.handler];-- // throw an error if handler contains an item that is not in VALID_HANDLERS- workflow.handler.forEach((handler) => {- if (!VALID_HANDLERS.includes(handler)) {- throw new Error(- `Deprecation Workflow: \`handler\` ${handler} must be one of ${VALID_HANDLERS.join(", ")}`- );- }- });-- // we also need to ensure that `log` and `silence` are not used together- if (- workflow.handler.includes("log") &&- workflow.handler.includes("silence")- ) {- throw new Error(- `Deprecation Workflow: \`handler\` ${workflow.handler} must not include both \`log\` and \`silence\``- );- }-- // validate the deprecation matchIds- if (- typeof workflow.matchId !== "string" &&- !(workflow.matchId instanceof RegExp)- ) {- throw new Error(- `Deprecation Workflow: \`matchId\` ${workflow.matchId} must be a string or a regex`- );- }-- // validate the deprecation envs- workflow.env ||= [];- workflow.env = Array.isArray(workflow.env)- ? workflow.env- : [workflow.env];-- // throw an error if env contains an item that is not in VALID_ENVS- workflow.env.forEach((env) => {- if (!VALID_ENVS.includes(env)) {- throw new Error(- `Deprecation Workflow: \`env\` ${env} must be one of ${VALID_ENVS.join(", ")}`- );- }- });- });+ workflows.forEach(this.#validateWorkflow);this.#workflows = workflows;this.#updateActiveWorkflows();@@ -175,7 +126,7 @@ export class DiscourseDeprecationWorkflow {}const silenced = workflow.handler?.includes("silence") ?? false;- const count = workflow.handler?.includes("counter") ?? false;+ const count = workflow.handler?.includes("count") ?? false;return !silenced || count;}@@ -203,6 +154,17 @@ export class DiscourseDeprecationWorkflow {return !!workflow?.handler?.includes("throw");}+ /**+ * Checks if a deprecation should notify admins.+ *+ * @param {string} deprecationId - ID of the deprecation+ * @return {boolean} True if deprecation should notify admins+ */+ shouldNotifyAdmin(deprecationId) {+ const workflow = this.#find(deprecationId);+ return !!workflow?.handler?.includes("notify-admin");+ }+/*** Finds the workflow matching a deprecation ID.* @param {string} deprecationId - ID of the deprecation@@ -252,6 +214,78 @@ export class DiscourseDeprecationWorkflow {return targetEnvs.includes("development");});}++ /**+ * Validates a workflow configuration object.+ * Ensures handler types, matchId patterns, and environments are valid.+ * Automatically converts single values to arrays for handler and env properties.+ *+ * @param {Object} workflow - The workflow configuration to validate+ * @param {(string|string[])} workflow.handler - Handler type(s) for the workflow+ * @param {(string|RegExp)} workflow.matchId - ID or pattern to match deprecations+ * @param {(string|string[])} [workflow.env] - Environment(s) where the workflow applies+ * @throws {Error} If handler is not in VALID_HANDLERS list+ * @throws {Error} If incompatible handler combinations are used (e.g., "log" with "silence")+ * @throws {Error} If matchId is not a string or RegExp+ * @throws {Error} If env is not in VALID_ENVS list+ * @private+ */+ #validateWorkflow(workflow) {+ // validate the deprecation handlers+ workflow.handler ||= [];+ workflow.handler = Array.isArray(workflow.handler)+ ? workflow.handler+ : [workflow.handler];++ // throw an error if handler contains an item that is not in VALID_HANDLERS+ workflow.handler.forEach((handler) => {+ if (!VALID_HANDLERS.includes(handler)) {+ throw new Error(+ `Deprecation Workflow: \`handler\` ${handler} must be one of ${VALID_HANDLERS.join(", ")}`+ );+ }+ });++ // validate incompatible handler combinations+ const incompatiblePairs = [+ ["log", "silence"],+ ["notify-admin", "silence"],+ ];++ for (const [handler1, handler2] of incompatiblePairs) {+ if (+ workflow.handler.includes(handler1) &&+ workflow.handler.includes(handler2)+ ) {+ throw new Error(+ `Deprecation Workflow: \`handler\` ${workflow.handler} must not include both \`${handler1}\` and \`${handler2}\``+ );+ }+ }++ // validate the deprecation matchIds+ if (+ typeof workflow.matchId !== "string" &&+ !(workflow.matchId instanceof RegExp)+ ) {+ throw new Error(+ `Deprecation Workflow: \`matchId\` ${workflow.matchId} must be a string or a regex`+ );+ }++ // validate the deprecation envs+ workflow.env ||= [];+ workflow.env = Array.isArray(workflow.env) ? workflow.env : [workflow.env];++ // throw an error if env contains an item that is not in VALID_ENVS+ workflow.env.forEach((env) => {+ if (!VALID_ENVS.includes(env)) {+ throw new Error(+ `Deprecation Workflow: \`env\` ${env} must be one of ${VALID_ENVS.join(", ")}`+ );+ }+ });+ }}/**@@ -260,11 +294,20 @@ export class DiscourseDeprecationWorkflow {* IMPORTANT: The first match wins, so the order of the workflows is relevant.** Each workflow config item should have:- * @property {(string|string[])} handler - Handler type(s): "silence", "log", "throw", "dont-throw", "dont-count", and/or "counter"+ * @property {(string|string[])} handler - Handler type(s): "silence", "log", "throw", "dont-throw", "dont-count", "count", and/or "notify-admin"* @property {(string|RegExp)} matchId - ID or pattern to match deprecations* @property {(string|string[])} [env] - Optional environment(s): "development", "qunit-test", "rails-test", "test", "production", "unset"+ *+ * Handler types:+ * - "silence": Suppress the deprecation warning from logging+ * - "log": Allow the deprecation to be logged (default behavior)+ * - "throw": Throw an error when this deprecation occurs+ * - "dont-throw": Prevent throwing even when RAISE_ON_DEPRECATION is enabled+ * - "dont-count": Prevent counting this deprecation in metrics+ * - "count": Count this deprecation even if silenced+ * - "notify-admin": Display admin notification for this deprecation (incompatible with "silence")*/-const DeprecationWorkflow = new DiscourseDeprecationWorkflow([+const workflows = [{ handler: "silence", matchId: "template-action" }, // will be removed in Ember 6.0{ handler: "silence", matchId: "discourse.select-kit" },{@@ -279,17 +322,65 @@ const DeprecationWorkflow = new DiscourseDeprecationWorkflow([handler: "log",matchId: /^discourse\.native-array-extensions\..+$/,},- {- handler: ["dont-count", "dont-throw"],- matchId: /fake.deprecation.*/,- env: "test",- },// widget-related code should fail on all CI tests, including plugins and custom themes{handler: "throw",matchId: "discourse.widgets-decommissioned",env: "test",},-]);++ // CRITICAL DEPRECATIONS that should trigger admin warnings,+ // To keep warnings meaningful and prevent overflowing users with them,+ // we should only add values here after fixing core and official plugins+ { handler: "notify-admin", matchId: "discourse.add-flag-property" },+ { handler: "notify-admin", matchId: "discourse.add-header-panel" },+ { handler: "notify-admin", matchId: "discourse.bootbox" },+ { handler: "notify-admin", matchId: "discourse.breadcrumbs.childCategories" },+ { handler: "notify-admin", matchId: "discourse.breadcrumbs.firstCategory" },+ {+ handler: "notify-admin",+ matchId: "discourse.breadcrumbs.parentCategories",+ },+ {+ handler: "notify-admin",+ matchId: "discourse.breadcrumbs.parentCategoriesSorted",+ },+ { handler: "notify-admin", matchId: "discourse.breadcrumbs.parentCategory" },+ { handler: "notify-admin", matchId: "discourse.breadcrumbs.secondCategory" },+ {+ handler: "notify-admin",+ matchId: "discourse.component-template-resolving",+ },+ { handler: "notify-admin", matchId: "discourse.decorate-plugin-outlet" },+ { handler: "notify-admin", matchId: "discourse.header-widget-overrides" },+ { handler: "notify-admin", matchId: "discourse.modal-controllers" },+ {+ handler: "notify-admin",+ matchId: "discourse.plugin-outlet-classic-args-clash",+ },+ {+ handler: "notify-admin",+ matchId: "discourse.post-stream-widget-overrides",+ },+ {+ handler: "notify-admin",+ matchId: "discourse.post-stream.trigger-new-post",+ },+ { handler: "notify-admin", matchId: "discourse.qunit.acceptance-function" },+ { handler: "notify-admin", matchId: "discourse.qunit.global-exists" },+ { handler: "notify-admin", matchId: "discourse.script-tag-discourse-plugin" },+ { handler: "notify-admin", matchId: "discourse.script-tag-hbs" },+ { handler: "notify-admin", matchId: "discourse.widgets-decommissioned" },+ { handler: "notify-admin", matchId: "discourse.widgets-end-of-life" },++ // used in system specs+ {+ handler: ["dont-count", "dont-throw", "notify-admin"],+ matchId: /fake.deprecation.*/,+ env: "test",+ },+];++const DeprecationWorkflow = new DiscourseDeprecationWorkflow(workflows);export default DeprecationWorkflow;diff --git a/frontend/discourse/app/services/deprecation-warning-handler.js b/frontend/discourse/app/services/deprecation-warning-handler.jsindex 3ad5db40eac..bb928a9ddc1 100644--- a/frontend/discourse/app/services/deprecation-warning-handler.js+++ b/frontend/discourse/app/services/deprecation-warning-handler.js@@ -1,4 +1,3 @@-import { DEBUG } from "@glimmer/env";import { registerDeprecationHandler } from "@ember/debug";import Service, { service } from "@ember/service";import { addGlobalNotice } from "discourse/components/global-notice";@@ -10,41 +9,8 @@ import identifySource from "discourse/lib/source-identifier";import { escapeExpression } from "discourse/lib/utilities";import { i18n } from "discourse-i18n";-// Deprecations matching patterns on this list will trigger warnings for admins.-// To avoid 'crying wolf', we should only add values here when we're sure they're-// not being triggered by core or official themes/plugins.-export const CRITICAL_DEPRECATIONS = [- "discourse.modal-controllers",- "discourse.bootbox",- "discourse.add-header-panel",- "discourse.header-widget-overrides",- "discourse.add-flag-property",- "discourse.breadcrumbs.childCategories",- "discourse.breadcrumbs.firstCategory",- "discourse.breadcrumbs.parentCategories",- "discourse.breadcrumbs.parentCategoriesSorted",- "discourse.breadcrumbs.parentCategory",- "discourse.breadcrumbs.secondCategory",- "discourse.qunit.acceptance-function",- "discourse.qunit.global-exists",- "discourse.post-stream.trigger-new-post",- "discourse.plugin-outlet-classic-args-clash",- "discourse.decorate-plugin-outlet",- "discourse.component-template-resolving",- "discourse.script-tag-hbs",- "discourse.script-tag-discourse-plugin",- "discourse.post-stream-widget-overrides",- "discourse.widgets-end-of-life",- "discourse.widgets-decommissioned",-];-const REPLACEMENT_URLS = {};-if (DEBUG) {- // used in system specs- CRITICAL_DEPRECATIONS.push(/fake.deprecation.*/);-}-// Deprecation handling APIs don't have any way to unregister handlers, so we set up permanent// handlers and link them up to the application lifecycle using module-local state.let handler;@@ -94,15 +60,7 @@ export default class DeprecationWarningHandler extends Service {return;}- if (- CRITICAL_DEPRECATIONS.some((pattern) => {- if (typeof pattern === "string") {- return pattern === opts.id;- } else {- return pattern.test(opts.id);- }- })- ) {+ if (DeprecationWorkflow.shouldNotifyAdmin(opts.id)) {this.notifyAdmin(opts, source);}}diff --git a/frontend/discourse/tests/unit/deprecation-workflow-test.js b/frontend/discourse/tests/unit/deprecation-workflow-test.jsindex fab5c5a41e3..48d5ab4fe24 100644--- a/frontend/discourse/tests/unit/deprecation-workflow-test.js+++ b/frontend/discourse/tests/unit/deprecation-workflow-test.js@@ -97,10 +97,20 @@ module("Unit | deprecation-workflow", function (hooks) {"throws if both log and silence are present");+ // both notify-admin and silence together throws+ assert.throws(+ () =>+ new DiscourseDeprecationWorkflow([+ { handler: ["notify-admin", "silence"], matchId: "four" },+ ]),+ /must not include both `notify-admin` and `silence`/,+ "throws if both notify-admin and silence are present"+ );+// empty handler array is allowed (defaults to []), should still match for counting/silence checks- const wf2 = new DiscourseDeprecationWorkflow([{ matchId: "four" }]);+ const wf2 = new DiscourseDeprecationWorkflow([{ matchId: "five" }]);assert.true(- wf2.shouldCount("four"),+ wf2.shouldCount("five"),"empty handler is accepted and should count");});@@ -261,8 +271,8 @@ module("Unit | deprecation-workflow", function (hooks) {const wf = new DiscourseDeprecationWorkflow([{ handler: "log", matchId: "a" },{ handler: "silence", matchId: "b" },- { handler: "counter", matchId: "c" }, // no log -> false- { handler: ["silence", "counter"], matchId: "d" }, // still no log -> false+ { handler: "count", matchId: "c" }, // no log -> false+ { handler: ["silence", "count"], matchId: "d" }, // still no log -> false]);assert.true(@@ -271,38 +281,38 @@ module("Unit | deprecation-workflow", function (hooks) {);assert.true(wf.shouldLog("a"), "handler with log should log");assert.false(wf.shouldLog("b"), "silence handler should not log");- assert.false(wf.shouldLog("c"), "counter-only should not log");- assert.false(wf.shouldLog("d"), "silence+counter should not log");+ assert.false(wf.shouldLog("c"), "count-only should not log");+ assert.false(wf.shouldLog("d"), "silence+count should not log");});test("shouldSilence reflects presence of silence handler", function (assert) {const wf = new DiscourseDeprecationWorkflow([{ handler: "silence", matchId: "s1" },- { handler: ["silence", "counter"], matchId: "s2" },+ { handler: ["silence", "count"], matchId: "s2" },{ handler: "log", matchId: "l" },- { handler: "counter", matchId: "c" },+ { handler: "count", matchId: "c" },]);assert.true(wf.shouldSilence("s1"), "silence -> true");- assert.true(wf.shouldSilence("s2"), "silence+counter -> true");+ assert.true(wf.shouldSilence("s2"), "silence+count -> true");assert.false(wf.shouldSilence("l"), "log -> false");- assert.false(wf.shouldSilence("c"), "counter -> false");+ assert.false(wf.shouldSilence("c"), "count -> false");assert.false(wf.shouldSilence("unhandled"), "unhandled -> false");});- test("shouldCount defaults to true and is true with counter even if silenced", function (assert) {+ test("shouldCount defaults to true and is true with count even if silenced", function (assert) {const wf = new DiscourseDeprecationWorkflow([{ handler: "silence", matchId: "s" },- { handler: "counter", matchId: "c" },- { handler: ["silence", "counter"], matchId: "sc" },+ { handler: "count", matchId: "c" },+ { handler: ["silence", "count"], matchId: "sc" },{ handler: "log", matchId: "l" },{ handler: [], matchId: "empty" },]);assert.true(wf.shouldCount("unhandled"), "unhandled -> count");assert.false(wf.shouldCount("s"), "silence -> do not count");- assert.true(wf.shouldCount("c"), "counter -> count");- assert.true(wf.shouldCount("sc"), "silence+counter -> count");+ assert.true(wf.shouldCount("c"), "count -> count");+ assert.true(wf.shouldCount("sc"), "silence+count -> count");assert.true(wf.shouldCount("l"), "log only -> count");assert.true(wf.shouldCount("empty"), "empty handler -> count");});@@ -311,7 +321,7 @@ module("Unit | deprecation-workflow", function (hooks) {const wf = new DiscourseDeprecationWorkflow([{ handler: "dont-count", matchId: "dc" },{ handler: ["log", "dont-count"], matchId: "ldc" },- { handler: ["counter", "dont-count"], matchId: "cdc" },+ { handler: ["count", "dont-count"], matchId: "cdc" },{ handler: "log", matchId: "l" },]);@@ -323,10 +333,7 @@ module("Unit | deprecation-workflow", function (hooks) {wf.shouldCount("ldc"),"dont-count with log still prevents counting");- assert.false(- wf.shouldCount("cdc"),- "dont-count overrides counter handler"- );+ assert.false(wf.shouldCount("cdc"), "dont-count overrides count handler");assert.true(wf.shouldCount("l"), "log only should count");assert.true(wf.shouldCount("unhandled"), "unhandled should count");});@@ -405,11 +412,26 @@ module("Unit | deprecation-workflow", function (hooks) {"dont-throw with log prevents throwing even with includeUnsilenced");});++ test("shouldNotifyAdmin reflects presence of notify-admin handler", function (assert) {+ const wf = new DiscourseDeprecationWorkflow([+ { handler: "notify-admin", matchId: "na1" },+ { handler: ["notify-admin", "count"], matchId: "na2" },+ { handler: "log", matchId: "l" },+ { handler: "silence", matchId: "s" },+ ]);++ assert.true(wf.shouldNotifyAdmin("na1"), "notify-admin handler -> true");+ assert.true(wf.shouldNotifyAdmin("na2"), "notify-admin + count -> true");+ assert.false(wf.shouldNotifyAdmin("l"), "log handler -> false");+ assert.false(wf.shouldNotifyAdmin("s"), "silence handler -> false");+ assert.false(wf.shouldNotifyAdmin("unhandled"), "unhandled -> false");+ });});test("emberWorkflowList flattens handlers and filters to Ember CLI-compatible ones", function (assert) {const wf = new DiscourseDeprecationWorkflow([- { handler: ["silence", "counter"], matchId: "x" },+ { handler: ["silence", "count"], matchId: "x" },{ handler: ["log", "throw"], matchId: "y" },{ handler: ["silence", "dont-throw", "dont-count"], matchId: "z" },]);@@ -418,11 +440,11 @@ module("Unit | deprecation-workflow", function (hooks) {.map((w) => `${w.matchId}:${w.handler}`).sort();- // "counter", "dont-throw", "dont-count" are not included in Ember CLI workflow output; only Ember CLI handlers are+ // "count", "dont-throw", "dont-count" are not included in Ember CLI workflow output; only Ember CLI handlers areassert.deepEqual(flattened,["x:silence", "y:log", "y:throw", "z:silence"].sort(),- "outputs one entry per allowed handler and excludes counter, dont-throw, and dont-count"+ "outputs one entry per allowed handler and excludes count, dont-throw, and dont-count");});});