summaryrefslogtreecommitdiff
path: root/lib/gitlab/dependency_linker/go_sum_linker.rb
blob: 20dc82ede9f45a761be15522dec5bfd362f3dd10 (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
# frozen_string_literal: true

module Gitlab
  module DependencyLinker
    class GoSumLinker < GoModLinker
      self.file_type = :go_sum

      private

      BASE64 = Gitlab::Regex.base64_regex
      REGEX = Regexp.new("^\\s*(?<name>#{NAME.source})\\s+(?<version>v#{SEMVER.source})(\/go.mod)?\\s+h1:(?<checksum>#{BASE64.source})\\s*$", NAME.options).freeze

      # rubocop: disable CodeReuse/ActiveRecord
      def link_dependencies
        highlighted_lines.map!.with_index do |rich_line, i|
          plain_line = plain_lines[i].chomp
          match = REGEX.match(plain_line)
          next rich_line unless match

          i0, j0 = match.offset(:name)
          i2, j2 = match.offset(:checksum)

          marker = StringRangeMarker.new(plain_line, rich_line.html_safe)
          marker.mark([i0..(j0 - 1), i2..(j2 - 1)]) do |text, left:, right:|
            if left
              url = package_url(text, match[:version])
              url ? link_tag(text, url) : text

            elsif right
              link_tag(text, "https://sum.golang.org/lookup/#{match[:name]}@#{match[:version]}")
            end
          end
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end