File: source_url_builder.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (57 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download
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
54
55
56
57
# frozen_string_literal: true

module BulkImports
  class SourceUrlBuilder
    ALLOWED_RELATIONS = %w[
      issues
      merge_requests
      epics
      milestones
    ].freeze

    attr_reader :context, :entity, :entry

    # @param [BulkImports::Pipeline::Context] context
    # @param [ApplicationRecord] entry
    def initialize(context, entry)
      @context = context
      @entity = context.entity
      @entry = entry
    end

    # Builds a source URL for the given entry if iid is present
    def url
      return unless entry.is_a?(ApplicationRecord)
      return unless iid
      return unless ALLOWED_RELATIONS.include?(relation)

      File.join(source_instance_url, group_prefix, source_full_path, '-', relation, iid.to_s)
    end

    private

    def iid
      @iid ||= entry.try(:iid)
    end

    def relation
      @relation ||= context.tracker.pipeline_class.relation
    end

    def source_instance_url
      @source_instance_url ||= context.bulk_import.configuration.url
    end

    def source_full_path
      @source_full_path ||= entity.source_full_path
    end

    # Group milestone (or epic) url is /groups/:group_path/-/milestones/:iid
    # Project milestone url is /:project_path/-/milestones/:iid
    def group_prefix
      return '' if entity.project?

      entity.pluralized_name
    end
  end
end