Skip to content

Commit

Permalink
depend on multiple git repos for each build
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal committed Sep 23, 2010
1 parent 2cfd394 commit 580ea2e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 29 deletions.
6 changes: 0 additions & 6 deletions add_builds.rb

This file was deleted.

6 changes: 4 additions & 2 deletions database.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DROP TABLE IF EXISTS builds;
DROP TABLE IF EXISTS runs;
DROP TABLE IF EXISTS git_repos;

CREATE TABLE "builds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "github_user" varchar(500), "github_repository" varchar(500), "git_branch" varchar(100), "run_script" varchar(5000), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "runs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "build_id" INTEGER, "git_hash" varchar(40), "success" boolean, "in_progress" boolean, "output" varchar(50000), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "builds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(500), "run_script" varchar(5000), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "runs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "build_id" INTEGER, "git_hash" varchar(40), "success" boolean, "in_progress" boolean, "output" varchar(50000), "created_at" datetime, "updated_at" datetime);
CREATE TABLE "git_repos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "build_id" INTEGER, "github_user" varchar(500), "github_repository" varchar(500), "git_branch" varchar(100), "created_at" datetime, "updated_at" datetime);
33 changes: 18 additions & 15 deletions models.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
class GitRepo < ActiveRecord::Base
belongs_to :build
def self.curl(url)
`curl -s #{url}`
end

def latest_hash
url = "http://github.com/api/v2/yaml/repos/show/#{github_user}/#{github_repository}/branches"
response = YAML::load(self.class.curl(url))
response["branches"][git_branch]
end
end

class Run < ActiveRecord::Base
belongs_to :build
end

class Build < ActiveRecord::Base
has_many :runs

def self.curl(url)
`curl -s #{url}`
end

has_many :git_repos

def self.next_to_run
all.detect { |build| build.should_run? }
end

def latest_github_hash
url = "http://github.com/api/v2/yaml/repos/show/#{github_user}/#{github_repository}/branches"
response = YAML::load(self.class.curl(url))
response["branches"][git_branch]
def latest_hashes
git_repos.order("id").map{|repo| repo.latest_hash }.join("_")
end

def should_run?
runs.find_by_git_hash(latest_github_hash) == nil
runs.find_by_git_hash(latest_hashes) == nil
end

def building?
last_run.in_progress?
end

def execute
run = Run.new(:git_hash => latest_github_hash, :build => self)
run = Run.new(:git_hash => latest_hashes, :build => self)
run.in_progress = true
run.save!
command = "unset #{env_vars_to_unset.join(" ")} && PATH=#{pre_bundler_path} #{run_script} 2>&1"
Expand All @@ -40,10 +47,6 @@ def execute
run
end

def name
"#{github_user}/#{github_repository}"
end

def last_run
runs.last
end
Expand Down
15 changes: 15 additions & 0 deletions reload_builds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
require 'environment'

Build.delete_all

build = Build.new(:name => "All Recipes")
build.git_repos << GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master")
build.run_script = "/Volumes/Persistent/build_all_recipes.command;"
build.save!

build = Build.new(:name => "Chef CI")
build.git_repos << GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master")
build.git_repos << GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "ci_cookbook", :git_branch => "master")
build.run_script = "/Volumes/Persistent/build_ci_cookbook.command;"
build.save!
35 changes: 29 additions & 6 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ def clear_db
describe Build do
before do
clear_db
@build = Build.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master", :run_script => "")
Build.stub(:curl).and_return "--- \nbranches: \n master: DEADBEEF\n broken_vim: df38014730975b9d18ee7fb969a247dac0b09ace\n"
@repo = GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master")
@build = Build.new(:run_script => "", :git_repos => [@repo], :name => "Basic Build")
@build.save!
GitRepo.stub(:curl).and_return "--- \nbranches: \n master: DEADBEEF\n broken_vim: df38014730975b9d18ee7fb969a247dac0b09ace\n"
end

it "exists" do
@build.should be_valid
end

it "should be able to get the latest hash from github" do
@build.latest_github_hash.should == "DEADBEEF"
it "should be able to get the latest hashes with one git repo" do
@build.latest_hashes.should == "DEADBEEF"
end

it "should be able to get the latest hashes with two git repos" do
@repo = GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "ci_cookbook", :git_branch => "master")
@build.git_repos << @repo
@build.save
@build.latest_hashes.should == "DEADBEEF_DEADBEEF"
end

it "knows if it should run or not" do
Expand All @@ -37,7 +46,7 @@ def clear_db
end

it "has a name method" do
@build.name.should == "pivotalexperimental/wschef"
@build.name.should == "Basic Build"
end

it "can find the last run" do
Expand Down Expand Up @@ -81,7 +90,7 @@ def clear_db
describe Run do
before do
clear_db
@build = Build.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master")
@build = Build.new
@run = Run.new(:git_hash => "DEADBEEF", :success => true)
Build.stub(:curl).and_return "--- \nbranches: \n master: DEADBEEF\n broken_vim: df38014730975b9d18ee7fb969a247dac0b09ace\n"
end
Expand All @@ -90,4 +99,18 @@ def clear_db
@run.build = @build
@run.should be_valid
end
end

describe GitRepo do
before do
clear_db
# @build = Build.new
# @run = Run.new(:git_hash => "DEADBEEF", :success => true)
GitRepo.stub(:curl).and_return "--- \nbranches: \n master: DEADBEEF\n broken_vim: df38014730975b9d18ee7fb969a247dac0b09ace\n"
end

it "can get its latest hash" do
@repo = GitRepo.new(:github_user => "pivotalexperimental", :github_repository => "wschef", :git_branch => "master")
@repo.latest_hash.should == "DEADBEEF"
end
end

0 comments on commit 580ea2e

Please sign in to comment.