summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/commit_reference_filter.rb
blob: f4e0c3111f55ddec3ce3be8d4ffb47e810204dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Banzai
  module Filter
    # HTML filter that replaces commit references with links.
    #
    # This filter supports cross-project references.
    class CommitReferenceFilter < AbstractReferenceFilter
      self.reference_type = :commit

      def self.object_class
        Commit
      end

      def self.references_in(text, pattern = Commit.reference_pattern)
        text.gsub(pattern) do |match|
          yield match, $~[:commit], $~[:project], $~[:namespace], $~
        end
      end

      def find_object(project, id)
        if project && project.valid_repo?
          project.commit(id)
        end
      end

      def url_for_object(commit, project)
        h = Gitlab::Routing.url_helpers
        noteable = context[:merge_request] || context[:noteable]

        if noteable.is_a?(MergeRequest) &&
           noteable.all_commit_shas.include?(commit.id)

          # the internal shas are in the context?
          # why not preload in the object?, just make sure we have the same ref
          # in all the rendering
          h.diffs_project_merge_request_url(project, noteable, commit_id: commit.id)
        else
          h.project_commit_url(project, commit, only_path: context[:only_path])
        end
      end

      def object_link_text_extras(object, matches)
        extras = super

        path = matches[:path] if matches.names.include?("path")
        if path == '/builds'
          extras.unshift "builds"
        end

        extras
      end
    end
  end
end