Skip to content

Instantly share code, notes, and snippets.

@wycats
Created October 21, 2008 01:02
Show Gist options
  • Save wycats/18226 to your computer and use it in GitHub Desktop.
Save wycats/18226 to your computer and use it in GitHub Desktop.
require "fileutils"
class Ci < Merb::Controller
def _template_location(action, type = nil, controller = controller_name)
controller == "layout" ? "layout.#{action}.#{type}" : "#{action}.#{type}"
end
def push(payload)
repo = payload[:repository]
if repo[:name] == "merb" && repo[:user] == "wycats"
FileUtils.mkdir_p(Dir.pwd / ".srcs")
if File.directory?(Dir.pwd / ".srcs" / repo[:name])
`cd #{Dir.pwd}/.srcs/#{repo[:name]} && git pull`
else
`cd #{Dir.pwd}/.srcs && git clone #{repo[:url]}`
end
run_later do
File.open(Merb.root / "log" / "build-results", "a") do |f|
result = run_specs
if result == 0
green_build(f, result)
else
red_build(f, result)
end
end
end
else
raise Forbidden
end
end
private
def run_specs
`cd #{Merb.root}/.srcs/merb && rake spec`
$?.exitstatus
end
def green_build(f, result)
f.puts("Success")
end
def red_build(f, result)
f.puts("Fail")
end
end
require File.join(File.dirname(__FILE__), "spec_helper")
FileUtils.rm_rf(Merb.root / ".srcs")
FileUtils.rm_r(Merb.root / "log" / "build-results", :noop => true)
ENV["RESULT"] = "0"
describe "a POST to /ci/push" do
def request_push(user, name)
request("/ci/push", :method => "POST",
"CONTENT_TYPE" => "application/json",
:input => {:payload => {:repository =>
{:name => name, :user => user,
:url => "file://#{File.expand_path(File.dirname(__FILE__))}/fixtures/merb"}}}.to_json)
end
it "raises a BadRequest if no payload is provided" do
request("/ci/push", :method => "POST").status.should == 400
end
it "is forbidden when the repository is incorrect" do
resp = request_push("wycats", "merbz")
resp.status.should == 403
end
it "is forbidden when the user is incorrect" do
resp = request_push("wycatz", "merb")
resp.status.should == 403
end
describe "when the repository is correct" do
before(:all) do
@resp = request_push("wycats", "merb")
end
it "is successful" do
@resp.should be_successful
end
it "creates a .srcs directory" do
File.should be_directory(Dir.pwd / ".srcs")
end
it "creates a directory for the repository" do
File.should be_directory(Dir.pwd / ".srcs" / "merb")
end
it "runs git pull on the directory if it already exists" do
mtime = File.mtime(Dir.pwd / ".srcs" / "merb")
request_push("wycats", "merb")
File.mtime(Dir.pwd / ".srcs" / "merb").should == mtime
end
describe "when the build is green" do
it "runs the green_build method" do
File.read(Merb.root / "log" / "build-results").split("\n").last.
should == "Success"
end
end
describe "when the build is red" do
it "runs the red_build method" do
ENV["RESULT"] = "1"
request_push("wycats", "merb")
File.read(Merb.root / "log" / "build-results").split("\n").last.
should == "Fail"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment