Class: Solano::SCM

Inherits:
Object
  • Object
show all
Defined in:
lib/solano/scm.rb,
lib/solano/scm/scm.rb,
lib/solano/scm/url.rb,
lib/solano/scm/configure.rb

Direct Known Subclasses

Git, Hg, StubSCM

Constant Summary collapse

SCMS =
%w(git hg)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSCM



7
8
9
# File 'lib/solano/scm/scm.rb', line 7

def initialize
  @default_origin_url = nil
end

Instance Attribute Details

#default_origin_urlObject

Returns the value of attribute default_origin_url.



5
6
7
# File 'lib/solano/scm/scm.rb', line 5

def default_origin_url
  @default_origin_url
end

Class Method Details

.configureObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/solano/scm/configure.rb', line 5

def self.configure
  scm = nil
  ok = false
  scms = [::Solano::Git, ::Solano::Hg]

  # Select SCM based on command availability and current repo type
  scms.each do |scm_class|
    sniff_scm = scm_class.new
    if sniff_scm.repo? && scm_class.version_ok then
      ok = true
      scm = sniff_scm
      break
    end
  end

  # Fall back to first SCM type that is available
  if !ok then
    scms.each do |scm_class|
      sniff_scm = scm_class.new
      if scm_class.version_ok then
        ok = true
        scm = sniff_scm
        break
      end
    end
  end

  # Default to a null SCM implementation
  scm ||= ::Solano::StubSCM.new
  return [scm, ok]
end

.parse_scp_url(url, scp_path) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/solano/scm/url.rb', line 7

def self.parse_scp_url(url, scp_path)
  # remove the ssh scheme (if any)
  url_to_parse = without_scheme(url, "ssh")

  # This is a gross abuse of Adressable::URI
  # It will treat [email protected] in [email protected]:user/repo.git as a scheme
  uri = Addressable::URI.parse(url_to_parse)
  raise SolanoError.new("invalid repo url #{url}") if uri.scheme.nil?

  scheme_parts = uri.scheme.split("@")
  uri.path = "/#{uri.path}" unless uri.path.to_s[0] == "/"
  uri.scheme = "ssh"
  uri.host = scheme_parts.last
  uri.user = scheme_parts.first
  uri
end

.scp_url?(raw_url, attempted_parse) ⇒ Boolean

Returns scp path if it looks like this is an SCP url



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/solano/scm/url.rb', line 25

def self.scp_url?(raw_url, attempted_parse)
  if attempted_parse && attempted_parse.scheme == 'https' then
    return nil
  end
  raw_url_elements = without_scheme(raw_url).split(":")
  scp_path = raw_url_elements.last if raw_url_elements.size > 1
  if scp_path then
    path_elements = scp_path.split(/\//)
    return scp_path if path_elements[0] !~ /^\d+$/
  end
  return nil
end

.valid_repo_url?(url) ⇒ Boolean

Weak validator; server will also validate



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/solano/scm/url.rb', line 44

def self.valid_repo_url?(url)
  if url.nil? || url.strip.empty? then
    raise SolanoError.new("invalid empty scm url")
  end

  begin
    attempted_parse = Addressable::URI.parse(url)
  rescue Exception => e
    raise SolanoError.new(e.message)
  end

  if scp_path = scp_url?(url, attempted_parse) then
    uri = parse_scp_url(url, scp_path)
  else
    uri = attempted_parse
  end

  scheme_pattern = SCMS+SCMS.map { |scm| scm+"+ssh" }
  scheme_pattern = scheme_pattern.join("|")
  scheme_pattern = "https?|ssh|"+scheme_pattern

  ok =  uri.scheme =~ /^(#{scheme_pattern})$/
  ok &&= uri.host && uri.host.size > 0
  ok &&= uri.path && uri.path.size > 0

  if !ok then
    raise SolanoError.new("invalid repo url: '#{url}'")
  end
  return ok
end

.without_scheme(url, scheme = nil) ⇒ Object



38
39
40
41
# File 'lib/solano/scm/url.rb', line 38

def self.without_scheme(url, scheme = nil)
  scheme ||= "[a-z]+"
  url.gsub(/^#{scheme}\:\/\//, "")
end

Instance Method Details

#changes?(options = {}) ⇒ Boolean



64
65
66
# File 'lib/solano/scm/scm.rb', line 64

def changes?(options={})
  return false
end

#commitsObject



107
108
109
# File 'lib/solano/scm/scm.rb', line 107

def commits
  []
end

#create_patch(session_id, options = {}) ⇒ Object



80
81
82
# File 'lib/solano/scm/scm.rb', line 80

def create_patch(session_id, options={})
  raise Text::Error:PATCH_NOT_SUPPORTED
end

#create_snapshot(session_id, options = {}) ⇒ Object



76
77
78
# File 'lib/solano/scm/scm.rb', line 76

def create_snapshot(session_id, options={})
  raise Text::Error:SNAPSHOT_NOT_SUPPORTED
end

#current_branchObject



56
57
58
# File 'lib/solano/scm/scm.rb', line 56

def current_branch
  return nil
end

#current_commitObject



103
104
105
# File 'lib/solano/scm/scm.rb', line 103

def current_commit
  return nil
end

#default_branchObject



60
61
62
# File 'lib/solano/scm/scm.rb', line 60

def default_branch
  return nil
end

#get_snap_idObject



68
69
70
# File 'lib/solano/scm/scm.rb', line 68

def get_snap_id
  return @snap_id
end

#ignore_pathObject



52
53
54
# File 'lib/solano/scm/scm.rb', line 52

def ignore_path
  return nil
end

#latest_commitObject



115
116
117
# File 'lib/solano/scm/scm.rb', line 115

def latest_commit
  return nil
end

#number_of_commits(id_from, id_to) ⇒ Object



111
112
113
# File 'lib/solano/scm/scm.rb', line 111

def number_of_commits(id_from, id_to)
  return 0
end

#origin_urlObject



48
49
50
# File 'lib/solano/scm/scm.rb', line 48

def origin_url
  return @default_origin_url
end

#push_latest(session_data, suite_details, options = {}) ⇒ Object



72
73
74
# File 'lib/solano/scm/scm.rb', line 72

def push_latest(session_data, suite_details, options={})
  return false
end

#repo?Boolean



36
37
38
# File 'lib/solano/scm/scm.rb', line 36

def repo?
  return false
end

#repo_nameObject



44
45
46
# File 'lib/solano/scm/scm.rb', line 44

def repo_name
  return "unknown"
end

#rootObject



40
41
42
# File 'lib/solano/scm/scm.rb', line 40

def root
  return Dir.pwd
end

#scm_nameObject



33
34
# File 'lib/solano/scm/scm.rb', line 33

def scm_name
end

#support_dataObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/solano/scm/scm.rb', line 11

def support_data
  data = Hash.new
  data['scm_name'] = self.scm_name
  if !self.repo? then
    data['is_repo'] = false
    return data
  end

  %w(scm_name repo? root repo_name origin_url current_branch default_branch changes?).each do |method|
    key = method
    if method =~ /[?]\z/ then
      key = "is_#{method.sub(/[?]\z/, '')}"
    end

    value = self.send(method.to_sym) rescue nil

    data[key] = value
  end

  return data
end

#upload_file(auth_url, file_path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/solano/scm/scm.rb', line 84

def upload_file(auth_url, file_path)
  if (`which curl >/dev/null 2>&1 ; echo $?`).to_i == 0 then
    `curl -f --upload-file "#{file_path}" "#{auth_url}"`
    if(`echo $?`).to_i == 22  then
      raise "Failed to upload #{file_path} URL (#{out.code})"
    end
  else
    uri = URI(auth_url)
    body = File.read(file_path)
    out = Net::HTTP.start(uri.host, :use_ssl => true) do |http|
        http.send_request("PUT", uri.request_uri, body, {"content-type" => "",})
    end
    if out.code.to_i != 200
      raise "Failed to upload to #{file_path} (#{out.code})"
    end
  end
end