GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

external_issue_reference_filter.rb 2.59 KB
Newer Older
1
require 'banzai'
2

3 4
module Banzai
  module Filter
Robert Speicher's avatar
Robert Speicher committed
5
    # HTML filter that replaces external issue tracker references with links.
6 7
    # References are ignored if the project doesn't use an external issue
    # tracker.
8
    class ExternalIssueReferenceFilter < ReferenceFilter
Robert Speicher's avatar
Robert Speicher committed
9 10 11 12 13 14 15 16 17 18 19 20
      # Public: Find `JIRA-123` issue references in text
      #
      #   ExternalIssueReferenceFilter.references_in(text) do |match, issue|
      #     "<a href=...>##{issue}</a>"
      #   end
      #
      # text - String text to search.
      #
      # Yields the String match and the String issue reference.
      #
      # Returns a String replaced with the return of the block.
      def self.references_in(text)
21
        text.gsub(ExternalIssue.reference_pattern) do |match|
Robert Speicher's avatar
Robert Speicher committed
22 23 24 25
          yield match, $~[:issue]
        end
      end

Drew Blessing's avatar
Drew Blessing committed
26 27 28 29 30 31 32 33 34 35 36 37
      def self.referenced_by(node)
        project = Project.find(node.attr("data-project")) rescue nil
        return unless project

        id = node.attr("data-external-issue")
        external_issue = ExternalIssue.new(id, project)

        return unless external_issue

        { external_issue: external_issue }
      end

Robert Speicher's avatar
Robert Speicher committed
38
      def call
39 40
        # Early return if the project isn't using an external tracker
        return doc if project.nil? || project.default_issues_tracker?
41

42
        replace_text_nodes_matching(ExternalIssue.reference_pattern) do |content|
43
          issue_link_filter(content)
Robert Speicher's avatar
Robert Speicher committed
44
        end
45 46 47 48

        replace_link_nodes_with_href(ExternalIssue.reference_pattern) do |link, text|
          issue_link_filter(link, link_text: text)
        end
Robert Speicher's avatar
Robert Speicher committed
49 50 51 52 53 54 55 56 57
      end

      # Replace `JIRA-123` issue references in text with links to the referenced
      # issue's details page.
      #
      # text - String text to replace references in.
      #
      # Returns a String with `JIRA-123` references replaced with links. All
      # links have `gfm` and `gfm-issue` class names attached for styling.
58
      def issue_link_filter(text, link_text: nil)
Robert Speicher's avatar
Robert Speicher committed
59 60
        project = context[:project]

Drew Blessing's avatar
Drew Blessing committed
61 62 63 64
        self.class.references_in(text) do |match, id|
          ExternalIssue.new(id, project)

          url = url_for_issue(id, project, only_path: context[:only_path])
Robert Speicher's avatar
Robert Speicher committed
65

66
          title = escape_once("Issue in #{project.external_issue_tracker.title}")
67
          klass = reference_class(:issue)
Drew Blessing's avatar
Drew Blessing committed
68
          data  = data_attribute(project: project.id, external_issue: id)
Robert Speicher's avatar
Robert Speicher committed
69

70 71
          text = link_text || match

72
          %(<a href="#{url}" #{data}
Robert Speicher's avatar
Robert Speicher committed
73
               title="#{title}"
74
               class="#{klass}">#{text}</a>)
Robert Speicher's avatar
Robert Speicher committed
75 76 77 78 79 80 81 82 83
        end
      end

      def url_for_issue(*args)
        IssuesHelper.url_for_issue(*args)
      end
    end
  end
end