Class: Perkins::Repo

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/perkins/repo.rb

Constant Summary collapse

DEFAULT_DIR =
"/tmp/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gitObject

Returns the value of attribute git.



4
5
6
# File 'lib/perkins/repo.rb', line 4

def git
  @git
end

#new_commitObject

Returns the value of attribute new_commit.



5
6
7
# File 'lib/perkins/repo.rb', line 5

def new_commit
  @new_commit
end

#runnerObject

Returns the value of attribute runner.



5
6
7
# File 'lib/perkins/repo.rb', line 5

def runner
  @runner
end

#virtual_shaObject

Returns the value of attribute virtual_sha.



5
6
7
# File 'lib/perkins/repo.rb', line 5

def virtual_sha
  @virtual_sha
end

Class Method Details

.add_from_github(id) ⇒ Object



15
16
17
18
# File 'lib/perkins/repo.rb', line 15

def self.add_from_github(id)
  github_repo  = synced_records.find_by(gb_id: id.to_i)
  store_from_github(github_repo)
end

.initialize_from_store(opts) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/perkins/repo.rb', line 50

def self.initialize_from_store(opts)
  repo = Repo.new
  repo.url = opts["github_data"]["ssh_url"]
  repo.name = opts["name"]
  repo.github_data = opts["github_data"]
  repo.gb_id = opts["github_data"]["id"]
  repo.working_dir ||= DEFAULT_DIR #this should be configurable from app
  repo
end

.store_from_github(repo) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/perkins/repo.rb', line 42

def self.store_from_github(repo)
  #repo
  repo.working_dir ||= DEFAULT_DIR #this should be configurable from app
  repo.cached = false
  repo.save
  repo
end

.sync_github_repo(r) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/perkins/repo.rb', line 26

def self.sync_github_repo(r)
  return if Repo.where(gb_id: r[:id]).any?
  repo = Repo.new
  repo.github_data = r.to_attrs.with_indifferent_access
  repo.cached = true
  repo.url    = r[:clone_url]
  repo.name   = r[:full_name]
  repo.gb_id  = r[:id]
  repo.working_dir ||= DEFAULT_DIR
  repo.save
end

.sync_github_repos(user) ⇒ Object



20
21
22
23
24
# File 'lib/perkins/repo.rb', line 20

def self.sync_github_repos(user)
  user.api.repos.each do |repo|
    self.sync_github_repo(repo)
  end
end

.synced_recordsObject



38
39
40
# File 'lib/perkins/repo.rb', line 38

def self.synced_records
  self.from_github
end

Instance Method Details

#add_commit(sha, branch) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/perkins/repo.rb', line 205

def add_commit(sha, branch)
  #if runner_branch.include?(branch)
    #@new_commit = Perkins::Commit.new(sha, self)
    #@new_commit.branch = branch
    #enqueue_commit(@new_commit)
    enqueue_commit(sha, branch)
  #else
  #  puts "skipping commit from branch #{branch}"
  #end
end

#add_hook(url = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/perkins/repo.rb', line 100

def add_hook(url=nil)
  url = hook_url if url.blank?

  if hook_id.blank?
    res = $github_client.create_hook(
      self.name,
      'web',
      { :url => url, :content_type => 'json'},
      { :events => ['push', 'pull_request'], 
        :active => true}
    )
    self.update_attribute(:hook_id, res[:id]) if res[:id].present?
  end
end

#branchesObject



180
181
182
# File 'lib/perkins/repo.rb', line 180

def branches
  self.git.branches.map(&:name)
end

#build_runner_configObject



185
186
187
188
189
190
191
192
# File 'lib/perkins/repo.rb', line 185

def build_runner_config
  config = self.check_config_existence
  runner = Runner.new()
  runner.config = config
  runner.repo = self
  self.branch = runner_branch
  self.runner = runner
end

#check_config_existenceObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/perkins/repo.rb', line 159

def check_config_existence
  #puts "CURRENT GIT DIR: #{self.git.dir.path} !!!!!"
  config = self.git.chdir{
    if File.exist?(".travis.yml")
      config = Travis::Yaml.parse( File.open(".travis.yml").read )
    else
      config = Travis::Yaml.new
    end
    config
  }
  config
end

#clone_or_loadObject



145
146
147
148
149
150
151
# File 'lib/perkins/repo.rb', line 145

def clone_or_load
  if exists?
    open
  else
    download!
  end
end

#download!Object

def downloading?

self.download_status == "downloading"

end



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/perkins/repo.rb', line 72

def download!
  # we need this only in the context of the first clone
  # in the context of builds we are not going to notice 
  # the user that we are cloning the repo
  if self.virtual_sha.present?
    send_sse( status: "downloading")
    #self.update_column(:download_status, "downloading")
  end

  #clone repo
  ssh_url = self.github_data["ssh_url"]
  Git.clone(ssh_url, download_name, :path => working_dir)
  open
  
  #TODO: fix this & handle with care
  begin
    add_hook #permissions issue
  rescue Exception => e
    puts e.message
  end

  send_sse(status: "downloaded") if self.virtual_sha.present?
end

#download_nameObject



96
97
98
# File 'lib/perkins/repo.rb', line 96

def download_name
  [name, self.virtual_sha].join
end

#edit_hook(url) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/perkins/repo.rb', line 115

def edit_hook(url)

  url = hook_url if url.blank?
 
  hook = get_hook
        
  if hook.present?
    res = $github_client.edit_hook(
      self.name,
      hook["id"],
      'web',
      {:url => url, :content_type => 'json'},
      {:active => true}
    )
    self.update_attribute(:hook_id, res[:id]) if res[:id].present?
  end
end

#enqueue_commit(sha, branch) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/perkins/repo.rb', line 216

def enqueue_commit(sha, branch)
  report = Perkins::BuildReport.new
  report.sha = sha 
  report.branch = branch

  self.build_reports << report
  self.save
end

#exists?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/perkins/repo.rb', line 172

def exists?
  File.exist?(local_path)
end

#get_hookObject



140
141
142
143
# File 'lib/perkins/repo.rb', line 140

def get_hook
  return {} if self.hook_id.blank?
  $github_client.hook(self.name, self.hook_id)
end

#hook_urlObject



133
134
135
136
137
138
# File 'lib/perkins/repo.rb', line 133

def hook_url
  u = Perkins::Application.instance.sse_endpoint
  p = Perkins::Application.instance.port == "80" ? nil : Perkins::Application.instance.port
  host = [u, p].compact.join(":")
  url = "#{host}/repos/receiver.json"
end

#http_urlObject



225
226
227
228
229
230
# File 'lib/perkins/repo.rb', line 225

def http_url
  new_url = self.url.include?("http") ? self.url : self.url.gsub(":", "/")
  new_url.gsub!("git@", "https://")
  new_url.gsub!(".git", "")
  new_url
end

#last_report_idObject



232
233
234
# File 'lib/perkins/repo.rb', line 232

def last_report_id
  build_reports.last.id if build_reports.any?
end

#load_gitObject



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

def load_git
  clone_or_load
end

#local_pathObject



176
177
178
# File 'lib/perkins/repo.rb', line 176

def local_path
 [ self.working_dir , self.download_name].join
end

#openObject



153
154
155
156
157
# File 'lib/perkins/repo.rb', line 153

def open
  self.git = Git.open(local_path)  # :log => Logger.new(STDOUT)
  build_runner_config
  #self.update_column(:download_status, "downloaded") #unless self.downloaded?
end

#runner_branchObject



194
195
196
197
198
199
200
201
202
203
# File 'lib/perkins/repo.rb', line 194

def runner_branch
  case self.branch
  when :all
    self.branches
  when nil
    ["master"]
  else
    self.branches.include?(self.branch) ? [self.branch] : ["master"]
  end
end

#send_sse(msg) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/perkins/repo.rb', line 236

def send_sse(msg)
  opts = {repo: {id: self.id, name: self.name }}
  opts[:repo].merge!(msg) if msg.is_a?(Hash)
  json_opts = opts.to_json
  puts "Notify #{json_opts}".yellow
  Redis.current.publish("message.", json_opts)
end