debian-mirror-gitlab/app/models/integrations/harbor.rb

91 lines
2.7 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# frozen_string_literal: true
2022-08-27 11:52:29 +05:30
require 'uri'
2022-05-07 20:08:51 +05:30
module Integrations
class Harbor < Integration
2022-08-13 15:12:31 +05:30
validates :url, public_url: true, presence: true, addressable_url: { allow_localhost: false, allow_local_network: false }, if: :activated?
2022-05-07 20:08:51 +05:30
validates :project_name, presence: true, if: :activated?
validates :username, presence: true, if: :activated?
validates :password, format: { with: ::Ci::Maskable::REGEX }, if: :activated?
2022-11-25 23:54:43 +05:30
field :url,
title: -> { s_('HarborIntegration|Harbor URL') },
placeholder: 'https://demo.goharbor.io',
help: -> { s_('HarborIntegration|Base URL of the Harbor instance.') },
exposes_secrets: true,
required: true
field :project_name,
title: -> { s_('HarborIntegration|Harbor project name') },
2023-06-20 00:43:36 +05:30
help: -> { s_('HarborIntegration|The name of the project in Harbor.') },
required: true
2022-11-25 23:54:43 +05:30
field :username,
title: -> { s_('HarborIntegration|Harbor username') },
required: true
field :password,
type: 'password',
title: -> { s_('HarborIntegration|Harbor password') },
help: -> { s_('HarborIntegration|Password for your Harbor username.') },
non_empty_password_title: -> { s_('HarborIntegration|Enter new Harbor password') },
non_empty_password_help: -> { s_('HarborIntegration|Leave blank to use your current password.') },
required: true
2022-05-07 20:08:51 +05:30
def title
'Harbor'
end
def description
s_("HarborIntegration|Use Harbor as this project's container registry.")
end
def help
2022-11-25 23:54:43 +05:30
s_("HarborIntegration|After the Harbor integration is activated, global variables `$HARBOR_USERNAME`, `$HARBOR_HOST`, `$HARBOR_OCI`, `$HARBOR_PASSWORD`, `$HARBOR_URL` and `$HARBOR_PROJECT` will be created for CI/CD use.")
2022-05-07 20:08:51 +05:30
end
2022-10-11 01:57:18 +05:30
def hostname
Gitlab::Utils.parse_url(url).hostname
end
2022-05-07 20:08:51 +05:30
class << self
def to_param
name.demodulize.downcase
end
def supported_events
[]
end
def supported_event_actions
[]
end
end
def test(*_args)
2023-06-20 00:43:36 +05:30
client.check_project_availability
2022-05-07 20:08:51 +05:30
end
def ci_variables
return [] unless activated?
2022-08-27 11:52:29 +05:30
oci_uri = URI.parse(url)
oci_uri.scheme = 'oci'
2022-05-07 20:08:51 +05:30
[
{ key: 'HARBOR_URL', value: url },
2022-08-27 11:52:29 +05:30
{ key: 'HARBOR_HOST', value: oci_uri.host },
{ key: 'HARBOR_OCI', value: oci_uri.to_s },
2022-05-07 20:08:51 +05:30
{ key: 'HARBOR_PROJECT', value: project_name },
2022-07-23 23:45:48 +05:30
{ key: 'HARBOR_USERNAME', value: username.gsub(/^robot\$/, 'robot$$') },
2022-05-07 20:08:51 +05:30
{ key: 'HARBOR_PASSWORD', value: password, public: false, masked: true }
]
end
private
def client
@client ||= ::Gitlab::Harbor::Client.new(self)
end
end
end