A platform for community discussion. Free, open, simple.
UX: Show plugin requirements on the workflow templates page
Régis Hanol775d7516e502
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/templates.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/templates.gjsindex af0fd3131be..33870068cba 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/templates.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/templates.gjs@@ -1,60 +1,40 @@import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";-import { fn } from "@ember/helper";-import { on } from "@ember/modifier";+import { fn, hash } from "@ember/helper";import { action } from "@ember/object";-import didInsert from "@ember/render-modifiers/modifiers/did-insert";+import { LinkTo } from "@ember/routing";import { service } from "@ember/service";-import { trustHTML } from "@ember/template";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";+import { eq, not, or } from "discourse/truth-helpers";+import DButton from "discourse/ui-kit/d-button";+import DFilterControls from "discourse/ui-kit/d-filter-controls";+import DInterpolatedTranslation from "discourse/ui-kit/d-interpolated-translation";import dIcon from "discourse/ui-kit/helpers/d-icon";-import { nodeTypeColor, nodeTypeIcon } from "../../lib/workflows/node-types";+import { i18n } from "discourse-i18n";import { workflowFromTemplate } from "../../lib/workflows/template-workflow";-const MAX_VISIBLE_ICONS = 3;+const SEARCHABLE_PROPS = ["name", "description"];-function visibleNodeTypes(nodeTypes) {- return (nodeTypes || []).slice(0, MAX_VISIBLE_ICONS);-}--function hasOverflow(nodeTypes) {- return (nodeTypes || []).length > MAX_VISIBLE_ICONS;-}--function overflowCount(nodeTypes) {- return (nodeTypes || []).length - MAX_VISIBLE_ICONS;-}--function iconFor(nodeType) {- return nodeTypeIcon(nodeType) || "cog";-}+function requirementReason(requirement) {+ if (requirement.reason_key) {+ return i18n(requirement.reason_key);+ }-function iconStyle(nodeType) {- return trustHTML(`color: ${nodeTypeColor(nodeType)}`);+ return i18n("discourse_workflows.templates.missing_node", {+ node_type: requirement.node_type,+ });}export default class WorkflowsTemplates extends Component {@service router;- @service workflowsNodeTypes;@tracked creatingTemplateId = null;- @tracked nodeTypesLoaded = false;-- resolveNodeType = (identifier) => {- return this.workflowsNodeTypes.findNodeType(identifier) || identifier;- };get workflowId() {return this.router.currentRoute?.queryParams?.workflow_id;}- @action- async ensureNodeTypes() {- await this.workflowsNodeTypes.load();- this.nodeTypesLoaded = true;- }-@actionasync useTemplate(template) {if (this.creatingTemplateId) {@@ -107,38 +87,82 @@ export default class WorkflowsTemplates extends Component {}<template>- <div class="workflows-templates" {{didInsert this.ensureNodeTypes}}>- {{#each @templates as |tmpl|}}- <button- class="workflows-templates__tile"- disabled={{this.creatingTemplateId}}- type="button"- {{on "click" (fn this.useTemplate tmpl)}}- >- <span class="workflows-templates__description">- {{tmpl.description}}- </span>- {{#if this.nodeTypesLoaded}}- <div class="workflows-templates__icons">- {{#each (visibleNodeTypes tmpl.node_types) as |nodeTypeId|}}- {{#let (this.resolveNodeType nodeTypeId) as |nodeType|}}- <span- class="workflows-templates__icon"- style={{iconStyle nodeType}}- >- {{dIcon (iconFor nodeType)}}- </span>- {{/let}}+ <div class="workflows-templates">+ <DFilterControls+ @array={{@templates}}+ @inputPlaceholder={{i18n "discourse_workflows.templates.filter"}}+ @noResultsMessage={{i18n "discourse_workflows.templates.no_results"}}+ @searchableProps={{SEARCHABLE_PROPS}}+ >+ <:content as |filteredTemplates|>+ <table class="d-table">+ <tbody>+ {{#each filteredTemplates as |tmpl|}}+ <tr class="d-table__row">+ <td class="d-table__cell --overview">+ <div class="workflows-templates__title">+ <strong+ class="d-table__overview-name"+ >{{tmpl.name}}</strong>+ {{#each tmpl.plugins as |plugin|}}+ <span class="d-table-badge">+ <span+ class="d-table-badge__content"+ >{{plugin.name}}</span>+ </span>+ {{/each}}+ </div>+ <div class="workflows-templates__description">+ {{tmpl.description}}+ </div>+ {{#each tmpl.plugins as |plugin|}}+ {{#unless plugin.enabled}}+ <div class="workflows-templates__requirement">+ {{dIcon "triangle-exclamation"}}+ <DInterpolatedTranslation+ @key="discourse_workflows.templates.requires_plugin"+ as |Placeholder|+ >+ <Placeholder @name="plugin">+ <LinkTo+ @query={{hash filter=plugin.name}}+ @route="adminPlugins.index"+ >+ {{~i18n+ "discourse_workflows.templates.plugin_link"+ plugin=plugin.name+ ~}}+ </LinkTo>+ </Placeholder>+ </DInterpolatedTranslation>+ </div>+ {{/unless}}+ {{/each}}+ {{#each tmpl.missing_requirements as |requirement|}}+ <div class="workflows-templates__requirement">+ {{dIcon "triangle-exclamation"}}+ <span>{{requirementReason requirement}}</span>+ </div>+ {{/each}}+ </td>+ <td class="d-table__cell --controls">+ <DButton+ class="btn-default btn-small"+ @action={{fn this.useTemplate tmpl}}+ @disabled={{or+ this.creatingTemplateId+ (not tmpl.available)+ }}+ @isLoading={{eq this.creatingTemplateId tmpl.id}}+ @label="discourse_workflows.templates.use_template"+ />+ </td>+ </tr>{{/each}}- {{#if (hasOverflow tmpl.node_types)}}- <span class="workflows-templates__icon --overflow">- +{{overflowCount tmpl.node_types}}- </span>- {{/if}}- </div>- {{/if}}- </button>- {{/each}}+ </tbody>+ </table>+ </:content>+ </DFilterControls></div></template>}diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/template/list.rb b/plugins/discourse-workflows/app/services/discourse_workflows/template/list.rbindex 35effebf6ec..546bb16349d 100644--- a/plugins/discourse-workflows/app/services/discourse_workflows/template/list.rb+++ b/plugins/discourse-workflows/app/services/discourse_workflows/template/list.rb@@ -10,7 +10,38 @@ module DiscourseWorkflowsprivatedef fetch_templates- DiscourseWorkflows::TemplateStore.summaries+ nodes = Hash.new { |cache, identifier| cache[identifier] = node_info(identifier) }++ DiscourseWorkflows::TemplateStore.summaries.each do |summary|+ infos = summary[:node_types].map { |identifier| nodes[identifier] }++ summary[:plugins] = infos.filter_map { |info| info[:plugin] }.uniq+ summary[:missing_requirements] = infos.filter_map { |info| info[:requirement] }.uniq+ summary[:available] = summary[:plugins].all? { |plugin| plugin[:enabled] } &&+ summary[:missing_requirements].empty?+ end+ end++ def node_info(identifier)+ klass = Registry.latest_node_type(identifier, include_disabled_plugins: true)+ plugin = contributing_plugin(klass)++ {+ plugin: plugin && { name: plugin.humanized_name, enabled: plugin.enabled? },+ requirement: requirement_for(identifier, klass, plugin),+ }+ end++ def contributing_plugin(klass)+ plugin = klass && Registry.plugin_for(klass)+ plugin unless plugin&.name == DiscourseWorkflows::PLUGIN_NAME+ end++ def requirement_for(identifier, klass, plugin)+ return if klass&.available?+ return if plugin && !plugin.enabled?++ { node_type: identifier, reason_key: klass&.unavailable_reason_key }endendenddiff --git a/plugins/discourse-workflows/assets/stylesheets/common/templates.scss b/plugins/discourse-workflows/assets/stylesheets/common/templates.scssindex 76d957e883e..cc194719110 100644--- a/plugins/discourse-workflows/assets/stylesheets/common/templates.scss+++ b/plugins/discourse-workflows/assets/stylesheets/common/templates.scss@@ -1,55 +1,30 @@.workflows-templates {- display: grid;- grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));- gap: var(--space-4);-- &__tile {+ &__title {display: flex;- flex-direction: column;- gap: var(--space-3);- padding: var(--space-4);- background: var(--primary-very-low);- border: 1px solid var(--primary-low);- border-radius: var(--d-border-radius);- cursor: pointer;- text-align: left;-- &:hover {- border-color: var(--primary-medium);- }+ align-items: center;+ gap: var(--space-2);- &[disabled] {- opacity: 0.6;- cursor: wait;+ .d-table-badge {+ line-height: 1;}}&__description {- font-size: var(--font-0);- color: var(--primary);- line-height: var(--line-height-large);+ color: var(--primary-medium);+ font-size: var(--font-down-1);+ margin-top: var(--space-1);}- &__icons {+ &__requirement {display: flex;- gap: var(--space-2);- }-- &__icon {- display: inline-flex;align-items: center;- justify-content: center;- width: 32px;- height: 32px;- border-radius: var(--d-border-radius);- background: var(--primary-low);- color: var(--primary-high);+ gap: var(--space-1);+ margin-top: var(--space-1);font-size: var(--font-down-1);+ color: var(--primary-high);- &.--overflow {- font-size: var(--font-down-2);- font-weight: 700;- color: var(--primary-medium);+ .d-icon {+ color: var(--danger);}}}diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex a65f50815c2..7490471fe97 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -102,7 +102,12 @@ en:title: "Workflows"templates:title: "Templates"- description: "Start with a pre-built workflow template"+ requires_plugin: "Requires the %{plugin} to be enabled"+ plugin_link: "%{plugin} plugin"+ missing_node: "Requires the %{node_type} node, which is not installed"+ use_template: "Use template"+ filter: "Filter by name or description"+ no_results: "No templates match your filter"new_workflow: "New workflow"default_workflow_name: "My workflow"delete: "Delete"diff --git a/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb b/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rbindex b0c4e572eab..224031db357 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb@@ -47,9 +47,7 @@ module DiscourseWorkflowsprivatedef node_registered?(node_class)- DiscoursePluginRegistry._raw_discourse_workflows_nodes.any? do |entry|- entry[:value] == (node_class.name || node_class)- end+ Registry.plugin_for(node_class).present?end# `Plugin::Instance#on` gates the handler on the owning plugin being enabled,diff --git a/plugins/discourse-workflows/lib/discourse_workflows/registry.rb b/plugins/discourse-workflows/lib/discourse_workflows/registry.rbindex 34b8277b1a9..d16e192e8c6 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/registry.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/registry.rb@@ -39,6 +39,14 @@ module DiscourseWorkflowsendend+ def plugin_for(klass)+ reference = klass.name || klass+ DiscoursePluginRegistry+ ._raw_discourse_workflows_nodes+ .find { |entry| entry[:value] == reference }+ &.fetch(:plugin)+ end+def find_credential_type(identifier)resolve_class(credential_type_index[identifier])end@@ -48,6 +56,11 @@ module DiscourseWorkflowsnode_type_index(include_disabled_plugins: include_disabled_plugins)[[identifier, version]]end+ def latest_node_type(identifier, include_disabled_plugins: false)+ version = latest_version(identifier, include_disabled_plugins:)+ find_node_type(identifier, version:, include_disabled_plugins:) if version+ end+def latest_version(identifier, include_disabled_plugins: false)available_versions(identifier, include_disabled_plugins: include_disabled_plugins).lastenddiff --git a/plugins/discourse-workflows/spec/models/discourse_workflows/template_store_spec.rb b/plugins/discourse-workflows/spec/models/discourse_workflows/template_store_spec.rbindex 2bd8ecb08dd..39fabb46370 100644--- a/plugins/discourse-workflows/spec/models/discourse_workflows/template_store_spec.rb+++ b/plugins/discourse-workflows/spec/models/discourse_workflows/template_store_spec.rb@@ -1,13 +1,11 @@# frozen_string_literal: trueRSpec.describe DiscourseWorkflows::TemplateStore do- before { described_class.reset_cache! }- after { described_class.reset_cache! }-let(:template_path) { File.join(DiscourseWorkflows::TEMPLATES_PATH, "cached-template.json") }let(:broken_template_path) doFile.join(DiscourseWorkflows::TEMPLATES_PATH, "broken-template.json")end+ let(:template_paths) { [template_path] }let(:template_json) do{name: "Cached template",@@ -16,13 +14,18 @@ RSpec.describe DiscourseWorkflows::TemplateStore do}.to_jsonend- it "returns template summaries" do+ before do+ described_class.reset_cache!Dir.stubs(:glob).with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path])+ .returns(template_paths)File.stubs(:read).with(template_path).returns(template_json)+ end+ after { described_class.reset_cache! }++ it "returns template summaries" doexpect(described_class.summaries).to contain_exactly({id: "cached-template",@@ -33,69 +36,30 @@ RSpec.describe DiscourseWorkflows::TemplateStore do)end- it "returns a duplicated template" do- Dir- .stubs(:glob)- .with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path])- File.stubs(:read).with(template_path).returns(template_json)-- template = described_class.find("cached-template")- template["name"] = "Mutated"+ it "returns copies so callers cannot mutate the cache" do+ described_class.find("cached-template")["name"] = "Mutated"+ described_class.summaries.first[:node_types].first.upcase!expect(described_class.find("cached-template")["name"]).to eq("Cached template")+ expect(described_class.summaries.first[:node_types].first).to eq("trigger:topic_created")end- it "returns duplicated summaries" do- Dir- .stubs(:glob)- .with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path])- File.stubs(:read).with(template_path).returns(template_json)-- summary = described_class.summaries.first- summary[:name].upcase!- summary[:node_types].first.upcase!-- expect(described_class.summaries.first).to include(- name: "Cached template",- node_types: %w[trigger:topic_created action:topic],- )- end-- it "reuses parsed templates until the cache is reset" do- Dir- .stubs(:glob)- .with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path])- File.expects(:read).with(template_path).once.returns(template_json)+ it "reads each template file once until the cache is reset" do+ File.expects(:read).with(template_path).twice.returns(template_json)described_class.summariesdescribed_class.find("cached-template")+ described_class.reset_cache!+ described_class.find("cached-template")end- it "skips malformed template JSON" do- Dir- .stubs(:glob)- .with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path, broken_template_path])- File.stubs(:read).with(template_path).returns(template_json)- File.stubs(:read).with(broken_template_path).returns("not valid json{{{")+ context "with a malformed template file" do+ let(:template_paths) { [template_path, broken_template_path] }- ids = described_class.summaries.map { |template| template[:id] }+ before { File.stubs(:read).with(broken_template_path).returns("not valid json{{{") }- expect(ids).to contain_exactly("cached-template")- end-- it "reloads templates after reset" do- Dir- .stubs(:glob)- .with(File.join(DiscourseWorkflows::TEMPLATES_PATH, "*.json"))- .returns([template_path])- File.expects(:read).with(template_path).twice.returns(template_json)-- described_class.find("cached-template")- described_class.reset_cache!- described_class.find("cached-template")+ it "skips it" do+ expect(described_class.summaries.map { |t| t[:id] }).to contain_exactly("cached-template")+ endendenddiff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/templates_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/templates_controller_spec.rbindex bcbaad487d4..8d5fa0894bd 100644--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/templates_controller_spec.rb+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/templates_controller_spec.rb@@ -22,14 +22,11 @@ RSpec.describe DiscourseWorkflows::TemplatesController doenddescribe "GET /admin/plugins/discourse-workflows/templates" do- it "returns a list of templates with the expected attributes" do+ it "returns the templates" doget "/admin/plugins/discourse-workflows/templates.json"expect(response).to have_http_status(:ok)- templates = response.parsed_body["templates"]- expect(templates).to be_an(Array).and(be_present)- expect(templates).to all(include("id", "name", "description", "node_types"))- expect(templates.first["node_types"]).to be_an(Array)+ expect(response.parsed_body["templates"].map { |t| t["id"] }).to include("auto-tag-topics")endenddiff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/template/list_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/template/list_spec.rbindex d03465fa8b7..41c7bd4dd5e 100644--- a/plugins/discourse-workflows/spec/services/discourse_workflows/template/list_spec.rb+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/template/list_spec.rb@@ -17,46 +17,45 @@ RSpec.describe DiscourseWorkflows::Template::List doit { is_expected.to fail_a_policy(:can_manage_workflows) }end- context "when templates exist" do+ context "when there are no templates" do+ before { DiscourseWorkflows::TemplateStore.stubs(:summaries).returns([]) }+it { is_expected.to run_successfully }+ end- it "returns templates with the expected attributes" do- template = result[:templates].first- expect(template).to include(:id, :name, :description, :node_types)- end+ context "when every node type is available" do+ it { is_expected.to run_successfully }- it "includes known templates from config/templates" do- ids = result[:templates].map { |t| t[:id] }- expect(ids).to include("auto-tag-topics")+ it "returns available template summaries without plugins or requirements" do+ template = result[:templates].find { |t| t[:id] == "auto-tag-topics" }+ expect(template).to include(plugins: [], missing_requirements: [], available: true)endend- context "when a template file has invalid JSON" do+ context "when a template relies on plugin nodes" dobefore do- FileUtils.mkdir_p(DiscourseWorkflows::TEMPLATES_PATH)- File.write(- File.join(DiscourseWorkflows::TEMPLATES_PATH, "broken.json"),- "not valid json{{{",+ SiteSetting.chat_enabled = false+ DiscourseWorkflows::TemplateStore.stubs(:summaries).returns(+ [+ {+ id: "needs-plugins",+ node_types: %w[+ trigger:topic_created+ action:send_chat_message+ action:chat_approval+ action:not_installed+ ],+ },+ ],)end- after { FileUtils.rm_f(File.join(DiscourseWorkflows::TEMPLATES_PATH, "broken.json")) }-- it { is_expected.to run_successfully }-- it "skips the malformed template" do- ids = result[:templates].map { |t| t[:id] }- expect(ids).not_to include("broken")- end- end-- context "when no template files exist" do- before { DiscourseWorkflows::TemplateStore.stubs(:summaries).returns([]) }-- it { is_expected.to run_successfully }-- it "returns an empty array" do- expect(result[:templates]).to eq([])+ it "lists each plugin once and unexplained nodes as requirements" do+ expect(result[:templates].first).to include(+ plugins: [{ name: "Chat", enabled: false }],+ missing_requirements: [{ node_type: "action:not_installed", reason_key: nil }],+ available: false,+ )endendend