annotate plugins/redmine_checkout/lib/checkout/protocol.rb @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents 522f2aff1f07
children
rev   line source
Chris@16 1 module Checkout
Chris@16 2 class <<self
Chris@16 3 def awesome?
Chris@16 4 # Yes, this plugin is awesome!
Chris@16 5 true
Chris@16 6 end
Chris@16 7 end
Chris@16 8
Chris@16 9 class Protocol
Chris@16 10 attr_accessor :protocol, :regex, :regex_replacement, :access, :repository
Chris@16 11 attr_writer :default, :command, :fixed_url, :append_path
Chris@16 12
Chris@16 13
Chris@16 14 def initialize(args={})
Chris@16 15 args = args.dup
Chris@16 16
Chris@16 17 @protocol = args.delete :protocol
Chris@16 18 @command = args.delete :command # optional, if not set the default from the repo is used
Chris@16 19
Chris@16 20 # either a fixed url
Chris@16 21 @fixed_url = args.delete :fixed_url
Chris@16 22
Chris@16 23 # or a regex
Chris@16 24 @regex = args.delete :regex
Chris@16 25 @regex_replacement = args.delete :regex_replacement
Chris@16 26
Chris@16 27 @access = args.delete :access
Chris@16 28 @append_path = args.delete :append_path
Chris@16 29 @default = args.delete :is_default
Chris@16 30
Chris@16 31 @repository = args.delete :repository
Chris@16 32 end
Chris@16 33
Chris@16 34 def full_command(path = "")
Chris@16 35 cmd = ""
Chris@16 36 if repository.checkout_display_command?
Chris@16 37 cmd = self.command.present? ? self.command.strip + " " : ""
Chris@16 38 end
Chris@16 39 cmd + URI.escape(self.url(path))
Chris@16 40 end
Chris@16 41
Chris@16 42 def default?
Chris@16 43 @default.to_i > 0
Chris@16 44 end
Chris@16 45
Chris@16 46 def command
Chris@16 47 @command || self.repository && self.repository.checkout_default_command || ""
Chris@16 48 end
Chris@16 49
Chris@16 50 def append_path?
Chris@16 51 @append_path.to_i > 0
Chris@16 52 end
Chris@16 53
Chris@16 54 def access_rw(user)
Chris@16 55 # reduces the three available access levels 'read+write', 'read-only' and 'permission'
Chris@16 56 # to 'read+write' and 'read-only' and nil (not allowed)
Chris@16 57
Chris@16 58 @access_rw ||= {}
Chris@16 59 return @access_rw[user] if @access_rw.key? user
Chris@16 60 @access_rw[user] = case access
Chris@16 61 when 'permission'
Chris@16 62 case
Chris@16 63 when user.allowed_to?(:commit_access, repository.project) && user.allowed_to?(:browse_repository, repository.project)
Chris@16 64 'read+write'
Chris@16 65 when user.allowed_to?(:browse_repository, repository.project)
Chris@16 66 'read-only'
Chris@16 67 else
Chris@16 68 nil
Chris@16 69 end
Chris@16 70 else
Chris@16 71 @access
Chris@16 72 end
Chris@16 73 end
Chris@16 74
Chris@16 75 def access_label(user)
Chris@16 76 case access_rw(user)
chris@1135 77 when 'read+write'
chris@1135 78 :label_access_read_write
chris@1135 79 when 'read-only'
chris@1135 80 :label_access_read_only
Chris@16 81 end
Chris@16 82 end
Chris@16 83
Chris@16 84 def fixed_url
Chris@16 85 @fixed_url.present? ? @fixed_url : begin
Chris@16 86 if (regex.blank? || regex_replacement.blank?)
Chris@16 87 repository.url
Chris@16 88 else
Chris@16 89 repository.url.gsub(Regexp.new(regex), regex_replacement)
Chris@16 90 end
Chris@16 91 end
Chris@16 92 rescue RegexpError
Chris@16 93 repository.url || ""
Chris@16 94 end
Chris@16 95
Chris@16 96 def url(path = "")
Chris@16 97 return "" unless repository
Chris@16 98
Chris@16 99 url = fixed_url.sub(/\/+$/, "")
Chris@16 100 if repository.allow_subtree_checkout? && self.append_path? && path.present?
Chris@16 101 url = "#{url}/#{path}"
Chris@16 102 end
Chris@16 103
Chris@16 104 if repository.checkout_display_login?
Chris@16 105 begin
Chris@16 106 uri = URI.parse url
Chris@16 107 (uri.user = repository.login) if repository.login
Chris@16 108 (uri.password = repository.password) if (repository.checkout_display_login == 'password' && repository.login && repository.password)
Chris@16 109 url = uri.to_s
Chris@16 110 rescue URI::InvalidURIError
Chris@16 111 end
Chris@16 112 end
Chris@16 113 url
Chris@16 114 end
Chris@16 115 end
chris@1135 116 end