class Perkins::Repo

Constants

DEFAULT_DIR

Attributes

git[RW]
new_commit[RW]
runner[RW]
virtual_sha[RW]

Public Class Methods

add_from_github(id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_records() click to toggle source
# File lib/perkins/repo.rb, line 38
def self.synced_records
  self.from_github
end

Public Instance Methods

add_commit(sha, branch) click to toggle source
# 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) click to toggle source
# 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
branches() click to toggle source
# File lib/perkins/repo.rb, line 180
def branches
  self.git.branches.map(&:name)
end
build_runner_config() click to toggle source

docs.travis-ci.com/user/build-configuration/#The-Build-Matrix

# 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_existence() click to toggle source
# 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_load() click to toggle source
# File lib/perkins/repo.rb, line 145
def clone_or_load
  if exists?
    open
  else
    download!
  end
end
download!() click to toggle source

def downloading?

self.download_status == "downloading"

end

# 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_name() click to toggle source
# File lib/perkins/repo.rb, line 96
def download_name
  [name, self.virtual_sha].join
end
edit_hook(url) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/perkins/repo.rb, line 172
def exists?
  File.exist?(local_path)
end
get_hook() click to toggle source
# 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_url() click to toggle source
# 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_url() click to toggle source
# 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_id() click to toggle source
# File lib/perkins/repo.rb, line 232
def last_report_id
  build_reports.last.id if build_reports.any?
end
load_git() click to toggle source
# File lib/perkins/repo.rb, line 60
def load_git
  clone_or_load
end
local_path() click to toggle source
# File lib/perkins/repo.rb, line 176
def local_path
 [ self.working_dir , self.download_name].join
end
open() click to toggle source
# 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_branch() click to toggle source
# 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) click to toggle source
# 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