Class: Gitlab::Pages::UrlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/pages/url_builder.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, options = nil) ⇒ UrlBuilder

Returns a new instance of UrlBuilder.



11
12
13
14
15
16
# File 'lib/gitlab/pages/url_builder.rb', line 11

def initialize(project, options = nil)
  @project = project
  namespace, _, @project_path = project.full_path.partition('/')
  @project_namespace = namespace.downcase
  @options = options || {}
end

Instance Attribute Details

#project_namespaceObject (readonly)

Returns the value of attribute project_namespace.



6
7
8
# File 'lib/gitlab/pages/url_builder.rb', line 6

def project_namespace
  @project_namespace
end

Instance Method Details

#artifact_url(artifact, job) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/pages/url_builder.rb', line 40

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

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

#artifact_url_available?(artifact, job) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/gitlab/pages/url_builder.rb', line 51

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

#hostnameObject

Defines the full hostname, eg. group.gitlab.io



59
60
61
62
63
64
65
# File 'lib/gitlab/pages/url_builder.rb', line 59

def hostname
  return instance_pages_domain if namespace_in_path?
  return project_path if is_namespace_homepage?
  return instance_pages_domain.prepend("#{subdomain}.") if subdomain.present?

  instance_pages_domain
end

#is_namespace_homepage?Boolean

If the project path is the same as host, we serve it as group/user page.

e.g. For Pages external url example.io,

`acmecorp/acmecorp.example.io` project will publish to `http(s)://acmecorp.example.io`

See docs.gitlab.com/ee/user/project/pages/getting_started_part_one.html#user-and-group-website-examples.

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/gitlab/pages/url_builder.rb', line 35

def is_namespace_homepage? # rubocop:disable Naming/PredicateName -- namespace_homepage is not an
  # adjective, so adding "is_" improves understandability
  project_path.downcase == "#{project_namespace}.#{instance_pages_domain}"
end

#pages_urlObject



18
19
20
21
# File 'lib/gitlab/pages/url_builder.rb', line 18

def pages_url
  default_url
    .to_s
end

#path_prefixObject



67
68
69
70
71
# File 'lib/gitlab/pages/url_builder.rb', line 67

def path_prefix
  return unless @options[:path_prefix].present?

  ::Gitlab::Utils.slugify(@options[:path_prefix], allow_dots: true).presence
end

#unique_hostObject



23
24
25
26
27
28
# File 'lib/gitlab/pages/url_builder.rb', line 23

def unique_host
  return unless unique_domain_enabled?
  return if namespace_in_path?

  hostname
end