Welcome to mirror list, hosted at ThFree Co, Russian Federation.

url_builder.rb « pages « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d025af4ce5477d66b76f159b91ad2a2cce935ea (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

module Gitlab
  module Pages
    class UrlBuilder
      attr_reader :project_namespace

      ALLOWED_ARTIFACT_EXTENSIONS = %w[.html .htm .txt .json .xml .log].freeze
      ARTIFACT_URL = "%{host}/-/%{project_path}/-/jobs/%{job_id}/artifacts/%{artifact_path}"

      def initialize(project)
        @project = project
        @project_namespace, _, @project_path = project.full_path.partition('/')
      end

      def pages_url(with_unique_domain: false)
        find_url(with_unique_domain).downcase
      end

      def unique_host
        return unless unique_domain_enabled?
        return if config.namespace_in_path

        URI(unique_url).host
      end

      def namespace_pages?
        namespace_url == pages_url
      end

      def artifact_url(artifact, job)
        return unless artifact_url_available?(artifact, job)

        host_url = config.namespace_in_path ? "#{pages_base_url}/#{project_namespace}" : namespace_url

        format(
          ARTIFACT_URL,
          host: host_url,
          project_path: project_path,
          job_id: job.id,
          artifact_path: artifact.path)
      end

      def artifact_url_available?(artifact, job)
        config.enabled &&
          config.artifacts_server &&
          ALLOWED_ARTIFACT_EXTENSIONS.include?(File.extname(artifact.name)) &&
          (config.access_control || job.project.public?)
      end

      private

      attr_reader :project, :project_path

      def find_url(with_unique_domain)
        return namespace_in_path_url(with_unique_domain && unique_domain_enabled?) if config.namespace_in_path
        return unique_url if with_unique_domain && unique_domain_enabled?

        project_path_url = "#{config.protocol}://#{project_path}"

        # If the project path is the same as host, we serve it as group page
        # On development we ignore the URL port to make it work on GDK
        return namespace_url if Rails.env.development? && portless(namespace_url) == project_path_url
        # If the project path is the same as host, we serve it as group page
        return namespace_url if namespace_url == project_path_url

        "#{namespace_url}/#{project_path}"
      end

      def namespace_url
        @namespace_url ||= url_for(project_namespace)
      end

      def unique_url
        @unique_url ||= url_for(project.project_setting.pages_unique_domain)
      end

      def pages_base_url
        @pages_url ||= URI(config.url)
          .tap { |url| url.port = config.port }
          .to_s
      end

      def namespace_in_path_url(with_unique_domain)
        if with_unique_domain
          "#{pages_base_url}/#{project.project_setting.pages_unique_domain}"
        else
          "#{pages_base_url}/#{project_namespace}/#{project_path}"
        end
      end

      def url_for(subdomain)
        URI(config.url)
          .tap { |url| url.port = config.port }
          .tap { |url| url.host.prepend("#{subdomain}.") }
          .to_s
      end

      def portless(url)
        URI(url)
          .tap { |u| u.port = nil }
          .to_s
      end

      def unique_domain_enabled?
        project.project_setting.pages_unique_domain_enabled?
      end

      def config
        Gitlab.config.pages
      end
    end
  end
end