Module: Cloner

Defined in:
lib/cloner.rb

Class Method Summary collapse

Class Method Details

.checkout_branch(branch) ⇒ Object

todo: Checkout a branch or tag



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

def self.checkout_branch(branch)
  true
end

.clone_repo(repo, release_id, repo_id, reclone = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloner.rb', line 17

def self.clone_repo(repo, release_id, repo_id, reclone=false)
  path = self.make_path(repo)
  if reclone
    FileUtils::rm_rf(path)
  end
  $plog.debug("Cloning #{repo} into #{path}...")
  # begin
    # Git.configure do |config|
      # If you want to use a custom git binary
      # config.binary_path = '/git/bin/path'

      # If you need to use a custom SSH script
      # Config private SSH key on github.com
    #   config.git_ssh = "#{AUTO_ROOT}/git_ssh_wrapper.sh"
    # end
    if Dir.exists?(path)
      if not $debug
        g = Git.open(path, :log => $plog)
        local_branch = g.branches.local[0].full
        g.pull(remote='origin', branch=local_branch)
      end
    else
      opts = {
        # :recursive => true
        # Only last commit history
        :depth => 1
      }
      local_repo = Git.clone(repo, path, opts)
      path = local_repo.dir.path
    end
    $plog.debug("Cloned #{repo} into #{path}.")

    process_gitmodules(path, release_id, repo_id)

    return path
  # rescue Git::GitExecuteError => e
  #   $plog.error e
  #   return nil
    # return self.clone_repo(repo, reclone=true)
  # end
end

.find_gitmodules(clone_path) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cloner.rb', line 132

def self.find_gitmodules(clone_path)
  gitmodules = []
  filename = '.gitmodules'
  file = "#{clone_path}/#{filename}"
  submodules_exists = File.exists?(file)
  $plog.debug("submodules_exists: #{submodules_exists}")
  if submodules_exists
    contents = File.readlines(file)
    pattern = /url\s=\s(?<url>.+)(\.git)?$/
    contents.each {|line|
      matched = pattern.match(line)
      if matched
        $plog.debug("matched: #{matched}, submodule line: #{line}")
        gitmodules.push(matched[:url].gsub(/\.git$/, ''))
      end
    }

  end
  gitmodules
end

.make_path(repo) ⇒ Object



10
11
12
13
14
15
# File 'lib/cloner.rb', line 10

def self.make_path(repo)
  # todo: If repo is a [email protected]:xxx/yyy
  $plog.debug(repo)
  repo = repo.gsub(/(https:\/\/|http:\/\/|git@)/,'')
  path = "#{AUTO_ROOT}/#{repo}"
end

.mq_publish_repo(release_id, sub_repo_id) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/cloner.rb', line 122

def self.mq_publish_repo(release_id, sub_repo_id)
  message = {
    :release_id => release_id,
    :repo_id => sub_repo_id
  }
  queue_name = 'license_auto.repo'
  $plog.info("submodule is Repo, enqueue MQ.repo, release_id: #{release_id}, sub_repo_id: #{sub_repo_id}")
  $rmq.publish(queue_name, message.to_json)
end

.process_gitmodules(clone_path, release_id, parent_repo_id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cloner.rb', line 64

def self.process_gitmodules(clone_path, release_id, parent_repo_id)
  gitmodules = find_gitmodules(clone_path)
  $plog.info("gitmodules: #{gitmodules}")
  gitmodules.each {|url|
    homepage = nil
    # [email protected]:repo_owner/reop_name
    ssh_pattern = /^(?<username>.+)@/
    git_pattern = /^git:\/\//
    if url =~ ssh_pattern
      url = url.gsub(/:/, '/').gsub(ssh_pattern, 'https://')
    elsif url =~ git_pattern
      url = url.gsub(git_pattern, 'http://')
    end
    $plog.debug("gitmodules url: #{url}")

    remote = API::RemoteSourceVCS.new(url)
    homepage = remote.get_homepage
    g = remote.vcs

    sub_host, sub_repo_owner, sub_repo_name = g.host, g.owner, g.repo
    org_url = "#{sub_host}/#{sub_repo_owner}"

    if api_get_whitelist_orgs(org_url).ntuples > 0
      $plog.debug("whitelist_orgs: #{url}")

      new_added, sub_repo = add_repo(sub_repo_name, url, parent_repo_id=parent_repo_id)
      sub_repo_id = sub_repo['id'].to_i
      $plog.debug("sub_repo_id: #{sub_repo_id}, new_added: #{new_added}")
      case_items = api_query_product_repo(release_id, parent_repo_id)
      if case_items.ntuples > 0
        api_add_product_repo(release_id, parent_repo_id, sub_repo_id)
      end

      if new_added
        mq_publish_repo(release_id, sub_repo_id)
      end
    else
      $plog.debug("whitelist_orgs not: #{url}")
      pack_name = sub_repo_name
      last_commit = g.last_commits
      pack_version = last_commit ? last_commit['sha'] : nil
      lang = g.host
      source_url = g.repo_url
      license = cmt = nil
      status = 10
      add_pack_result = api_add_pack(pack_name, pack_version, lang, homepage, source_url, license, status, cmt)
      pack_id, is_newbie = add_pack_result['pack_id'].to_i, (add_pack_result['is_newbie'] == 't')
      r = api_add_product_repo_pack(parent_repo_id, pack_id, release_id)
      # $plog.debug("r: #{r}")
      if is_newbie
        queue_name = 'license_auto.pack'
        $rmq.publish(queue_name, {:pack_id => pack_id}.to_json, check_exist=true)
      end
      $plog.debug("pack_id: #{pack_id}, is_newbie: #{is_newbie}")
    end
  }
end