2019-01-01 20:40:33 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
2019-03-28 09:17:42 -04:00
|
|
|
class Link < ApplicationRecord
|
2019-01-01 20:40:33 -05:00
|
|
|
self.table_name = 'release_links'
|
|
|
|
|
2021-05-04 11:10:36 -04:00
|
|
|
belongs_to :release, touch: true
|
2019-01-01 20:40:33 -05:00
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
# 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
|
2020-02-24 19:09:12 -05:00
|
|
|
|
2019-04-11 02:29:07 -04:00
|
|
|
validates :url, presence: true, addressable_url: { schemes: %w(http https ftp) }, uniqueness: { scope: :release }
|
2019-01-01 20:40:33 -05:00
|
|
|
validates :name, presence: true, uniqueness: { scope: :release }
|
2020-02-24 19:09:12 -05:00
|
|
|
validates :filepath, uniqueness: { scope: :release }, format: { with: FILEPATH_REGEX }, allow_blank: true, length: { maximum: 128 }
|
2019-01-01 20:40:33 -05:00
|
|
|
|
|
|
|
scope :sorted, -> { order(created_at: :desc) }
|
|
|
|
|
2020-06-01 11:08:16 -04:00
|
|
|
enum link_type: {
|
|
|
|
other: 0,
|
|
|
|
runbook: 1,
|
|
|
|
package: 2,
|
|
|
|
image: 3
|
|
|
|
}
|
|
|
|
|
2019-01-01 20:40:33 -05:00
|
|
|
def internal?
|
|
|
|
url.start_with?(release.project.web_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def external?
|
|
|
|
!internal?
|
|
|
|
end
|
2020-11-06 19:08:58 -05:00
|
|
|
|
|
|
|
def hook_attrs
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
external: external?,
|
|
|
|
link_type: link_type,
|
|
|
|
name: name,
|
|
|
|
url: url
|
|
|
|
}
|
|
|
|
end
|
2019-01-01 20:40:33 -05:00
|
|
|
end
|
|
|
|
end
|