1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class Repository < ArvadosModel
8 include CommonApiTemplate
10 # Order is important here. We must validate the owner before we can
13 validate :name_format, :if => Proc.new { |r| r.errors[:owner_uuid].empty? }
14 validates(:name, uniqueness: true, allow_nil: false)
16 api_accessible :user, extend: :common do |t|
23 def self.attributes_required_columns
24 super.merge("clone_urls" => ["name"],
25 "fetch_url" => ["name"],
26 "push_url" => ["name"])
29 # Deprecated. Use clone_urls instead.
34 # Deprecated. Use clone_urls instead.
40 [ssh_clone_url, https_clone_url].compact
44 # Find where the repository is stored on the API server's filesystem,
45 # and return that path, or nil if not found.
46 # This method is only for the API server's internal use, and should not
47 # be exposed through the public API. Following our current gitolite
48 # setup, it searches for repositories stored by UUID, then name; and it
49 # prefers bare repositories over checkouts.
50 [["%s.git"], ["%s", ".git"]].each do |repo_base, *join_args|
51 [:uuid, :name].each do |path_attr|
52 git_dir = File.join(Rails.configuration.Git.Repositories,
53 repo_base % send(path_attr), *join_args)
54 return git_dir if File.exist?(git_dir)
62 def permission_to_update
65 elsif current_user.is_admin
68 current_user.uuid == owner_uuid
75 User.find_by_uuid(owner_uuid)
79 if owner.nil? or (owner.username.nil? and (owner.uuid != system_user_uuid))
80 errors.add(:owner_uuid, "must refer to a user with a username")
86 if owner.uuid == system_user_uuid
88 errmsg_start = "must be"
90 prefix_match = Regexp.escape(owner.username + "/")
91 errmsg_start = "must be the owner's username, then '/', then"
93 if not (/^#{prefix_match}[A-Za-z][A-Za-z0-9]*$/.match(name))
95 "#{errmsg_start} a letter followed by alphanumerics")
101 _clone_url Rails.configuration.Services.GitSSH.andand.ExternalURL, 'ssh://git@git.%s.arvadosapi.com'
105 _clone_url Rails.configuration.Services.GitHTTP.andand.ExternalURL, 'https://git.%s.arvadosapi.com/'
108 def _clone_url config_var, default_base_fmt
112 prefix = new_record? ? Rails.configuration.ClusterID : uuid[0,5]
113 if prefix == Rails.configuration.ClusterID and config_var != URI("")
116 base = URI(default_base_fmt % prefix)
121 if base.scheme == "ssh"
122 '%s@%s:%s.git' % [base.user, base.host, name]
124 '%s%s.git' % [base, name]