require "git" module Perkins class Repo < ActiveRecord::Base attr_accessor :git attr_accessor :new_commit, :runner has_many :build_reports, class_name: 'Perkins::BuildReport' serialize :github_data, ActiveSupport::HashWithIndifferentAccess scope :from_github, ->{where(cached: true)} scope :added, ->{where(cached: false)} DEFAULT_DIR = "/tmp/" def self.add_from_github(id) github_repo = synced_records.find_by(gb_id: id.to_i) store_from_github(github_repo) end def self.sync_github_repos(user) user.api.repos.each do |repo| self.sync_github_repo(repo) end end 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 def self.synced_records self.from_github end def self.store_from_github(repo) repo repo.working_dir = DEFAULT_DIR #this should be configurable from app repo.cached = false repo.save end 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 def load_git clone_or_load end def clone_or_load if exists? open else ssh_url = self.github_data["ssh_url"] g = Git.clone(ssh_url, name, :path => working_dir) open end end def open self.git = Git.open(local_path) # :log => Logger.new(STDOUT) build_runner_config(self.check_config_existence) if self.check_config_existence end def check_config_existence config = self.git.chdir{ if File.exists?(".travis.yml") config = Travis::Yaml.parse( File.open(".travis.yml").read ) else config = Travis::Yaml.new end config } config end def exists? File.exists?(local_path) end def local_path self.working_dir + self.name.to_s end def branches self.git.branches.map(&:name) end #http://docs.travis-ci.com/user/build-configuration/#The-Build-Matrix def build_runner_config(config) runner = Runner.new() runner.config = config runner.repo = self self.branch = runner_branch self.runner = runner end def runner_branch case self.branch when :all self.branches when nil ["master"] else self.branches.include?(self.branch) ? [self.branch] : ["master"] end end 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) else puts "skipping commit from branch #{branch}" end end def enqueue_commit(commit) $redis.publish("commits", {id: self.id.to_s, name: self.name , sha: commit.sha, branch: commit.branch}.to_json) end 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 end end