From 03bf6b3882ae708f6c7504ea6da057ac049e575d Mon Sep 17 00:00:00 2001
From: Guillaume Grossetie <ggrossetie@gmail.com>
Date: Fri, 20 Nov 2020 14:13:16 +0100
Subject: [PATCH] Disable diagram formats that require a companion container

---
 Gemfile                                       |  2 +-
 .../application_setting_implementation.rb     |  4 +-
 lib/banzai/filter/kroki_filter.rb             | 17 ++-------
 lib/gitlab/asciidoc.rb                        |  7 +---
 lib/gitlab/kroki.rb                           | 38 +++++++++++++++++++
 locale/gitlab.pot                             |  8 ++--
 6 files changed, 50 insertions(+), 26 deletions(-)
 create mode 100644 lib/gitlab/kroki.rb

diff --git a/Gemfile b/Gemfile
index a7ab56f835c0..7aace004923b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -159,7 +159,7 @@ gem 'wikicloth', '0.8.1'
 gem 'asciidoctor', '~> 2.0.10'
 gem 'asciidoctor-include-ext', '~> 0.3.1', require: false
 gem 'asciidoctor-plantuml', '~> 0.0.12'
-gem 'asciidoctor-kroki', '~> 0.2.2'
+gem 'asciidoctor-kroki', '~> 0.2.2', require: false
 gem 'rouge', '~> 3.25.0'
 gem 'truncato', '~> 0.7.11'
 gem 'bootstrap_form', '~> 4.2.0'
diff --git a/app/models/application_setting_implementation.rb b/app/models/application_setting_implementation.rb
index 5c7abbccd638..016314370ece 100644
--- a/app/models/application_setting_implementation.rb
+++ b/app/models/application_setting_implementation.rb
@@ -168,7 +168,9 @@ def defaults
         user_show_add_ssh_key_message: true,
         wiki_page_max_content_bytes: 50.megabytes,
         container_registry_delete_tags_service_timeout: 250,
-        container_registry_expiration_policies_worker_capacity: 0
+        container_registry_expiration_policies_worker_capacity: 0,
+        kroki_enabled: false,
+        kroki_url: nil,
       }
     end
 
diff --git a/lib/banzai/filter/kroki_filter.rb b/lib/banzai/filter/kroki_filter.rb
index 881b390bd368..8936e4bd5c2a 100644
--- a/lib/banzai/filter/kroki_filter.rb
+++ b/lib/banzai/filter/kroki_filter.rb
@@ -8,21 +8,10 @@ module Filter
     # HTML that replaces all diagrams supported by Kroki with the corresponding img tags.
     #
     class KrokiFilter < HTML::Pipeline::Filter
-      DIAGRAM_SELECTORS = ::AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES
-                              .map { |diagram_type| %(pre[lang="#{diagram_type}"] > code) }
-                              .join(', ')
-      DIAGRAM_SELECTORS_WO_PLANTUML = ::AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES
-                                          .select { |diagram_type| diagram_type != 'plantuml' }
-                                          .map { |diagram_type| %(pre[lang="#{diagram_type}"] > code) }
-                                          .join(', ')
-
       def call
-        diagram_selectors = if settings.plantuml_enabled
-                              # if PlantUML is enabled, PlantUML diagrams will be processed by the PlantUML filter.
-                              DIAGRAM_SELECTORS_WO_PLANTUML
-                            else
-                              DIAGRAM_SELECTORS
-                            end
+        diagram_selectors = ::Gitlab::Kroki.formats(settings)
+                                .map { |diagram_type| %(pre[lang="#{diagram_type}"] > code) }
+                                .join(', ')
 
         return doc unless settings.kroki_enabled && doc.at(diagram_selectors)
 
diff --git a/lib/gitlab/asciidoc.rb b/lib/gitlab/asciidoc.rb
index a7ab8e00126f..a9c2dd001cbb 100644
--- a/lib/gitlab/asciidoc.rb
+++ b/lib/gitlab/asciidoc.rb
@@ -56,11 +56,8 @@ def self.render(input, context)
       extensions = proc do
         include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
         block ::Gitlab::Asciidoc::MermaidBlockProcessor
-
-        if Gitlab::CurrentSettings.kroki_enabled
-          ::AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES.each do |name|
-            block ::AsciidoctorExtensions::KrokiBlockProcessor, name
-          end
+        ::Gitlab::Kroki.formats(Gitlab::CurrentSettings).each do |name|
+          block ::AsciidoctorExtensions::KrokiBlockProcessor, name
         end
       end
 
diff --git a/lib/gitlab/kroki.rb b/lib/gitlab/kroki.rb
new file mode 100644
index 000000000000..8c5652fb766e
--- /dev/null
+++ b/lib/gitlab/kroki.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'asciidoctor/extensions/asciidoctor_kroki/extension'
+
+module Gitlab
+  # Helper methods for Kroki
+  module Kroki
+    BLOCKDIAG_FORMATS = %w[
+        blockdiag
+        seqdiag
+        actdiag
+        nwdiag
+        packetdiag
+        rackdiag
+      ].freeze
+    # Diagrams that require a companion container are disabled for now
+    DIAGRAMS_FORMATS = ::AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES
+                           .reject { |diagram_type| diagram_type == 'mermaid' || diagram_type == 'bpmn' || BLOCKDIAG_FORMATS.include?(diagram_type) }
+    DIAGRAMS_FORMATS_WO_PLANTUML = DIAGRAMS_FORMATS
+                                        .reject { |diagram_type| diagram_type == 'plantuml' }
+
+    # Get the list of diagram formats that are currently enabled
+    #
+    # Returns an Array of diagram formats.
+    # If Kroki is not enabled, returns an empty Array.
+    def self.formats(current_settings)
+      return [] unless current_settings.kroki_enabled
+
+      # If PlantUML is enabled, PlantUML diagrams will be processed by the PlantUML server.
+      # In other words, the PlantUML server has precedence over Kroki since both can process PlantUML diagrams.
+      if current_settings.plantuml_enabled
+        DIAGRAMS_FORMATS_WO_PLANTUML
+      else
+        DIAGRAMS_FORMATS
+      end
+    end
+  end
+end
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index 21254bc44cd9..1059ea4d789e 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -180,11 +180,6 @@ msgid_plural "%d errors"
 msgstr[0] ""
 msgstr[1] ""
 
-msgid "%d error found:"
-msgid_plural "%d errors found:"
-msgstr[0] ""
-msgstr[1] ""
-
 msgid "%d exporter"
 msgid_plural "%d exporters"
 msgstr[0] ""
@@ -20378,6 +20373,9 @@ msgstr ""
 msgid "Please note that this application is not provided by GitLab and you should verify its authenticity before allowing access."
 msgstr ""
 
+msgid "Please note that using additional formats requires to start companion containers. Make sure that all %{kroki_images} are running."
+msgstr ""
+
 msgid "Please only enable search after installing the plugin, enabling indexing and recreating the index"
 msgstr ""
 
-- 
GitLab