debian-mirror-gitlab/app/models/releases/link.rb

55 lines
1.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Releases
2019-07-07 11:18:12 +05:30
class Link < ApplicationRecord
2019-02-15 15:39:39 +05:30
self.table_name = 'release_links'
2021-06-08 01:23:25 +05:30
belongs_to :release, touch: true
2019-02-15 15:39:39 +05:30
2020-10-04 03:57:07 +05:30
# See https://gitlab.com/gitlab-org/gitlab/-/issues/218753
# Regex modified to prevent catastrophic backtracking
FILEPATH_REGEX = %r{\A\/[^\/](?!.*\/\/.*)[\-\.\w\/]+[\da-zA-Z]+\z}.freeze
2022-04-01 21:47:47 +05:30
FILEPATH_MAX_LENGTH = 128
2020-04-08 14:13:33 +05:30
2019-07-31 22:56:46 +05:30
validates :url, presence: true, addressable_url: { schemes: %w(http https ftp) }, uniqueness: { scope: :release }
2019-02-15 15:39:39 +05:30
validates :name, presence: true, uniqueness: { scope: :release }
2022-04-01 21:47:47 +05:30
validates :filepath, uniqueness: { scope: :release }, allow_blank: true
validate :filepath_format_valid?
# we use a custom validator here to prevent running the regex if the string is too long
# see https://gitlab.com/gitlab-org/gitlab/-/issues/273771
def filepath_format_valid?
return if filepath.nil? # valid use case
return errors.add(:filepath, "is too long (maximum is #{FILEPATH_MAX_LENGTH} characters)") if filepath.length > FILEPATH_MAX_LENGTH
return errors.add(:filepath, 'is in an invalid format') unless FILEPATH_REGEX.match? filepath
end
2019-02-15 15:39:39 +05:30
scope :sorted, -> { order(created_at: :desc) }
2020-06-23 00:09:42 +05:30
enum link_type: {
other: 0,
runbook: 1,
package: 2,
image: 3
}
2019-02-15 15:39:39 +05:30
def internal?
url.start_with?(release.project.web_url)
end
def external?
!internal?
end
2021-01-29 00:20:46 +05:30
def hook_attrs
{
id: id,
external: external?,
link_type: link_type,
name: name,
url: url
}
end
2019-02-15 15:39:39 +05:30
end
end