Skip to content

Commit

Permalink
Refactor Build into Commit and Build [integrity#61 state:resolved]
Browse files Browse the repository at this point in the history
* Migrations are fixed, and should work out of the box. Just run
  `integrity create_db config.yml` and your database should be
  migrated (backup the database just in case before, and let us know if
  something goes wrong).

* Build manually is broken if you don't have commits in your project
  already. If there's a build already, it will just rebuild that commit
  This ties into [integrity#16] but breaks a few things. Mainly building for the
  first time to test the config is working (before we get sent a push).

* Posting to /push requires more information. All the information is
  already sent by Github, so no harm done if you're using that. If not,
  you need to provide, for each commit, a timestamp, the author name and
  email (in 'name <email>' format), and the commit message. We will
  publish a custom ruby script that you can run as your post-receive
  hook and that supplies all this information.

* Building is still done in the foreground, but all the "dirty" work is
  done and making it build in background should be easy now.

* Some styles and views need work. need work (at least, to handle
  commits that haven't been built yet), but maybe something else I'm
  missing.
  • Loading branch information
foca committed Jan 19, 2009
1 parent a982080 commit 7721421
Show file tree
Hide file tree
Showing 31 changed files with 677 additions and 421 deletions.
4 changes: 2 additions & 2 deletions app.rb
Expand Up @@ -123,9 +123,9 @@
redirect project_url(@project)
end

get "/:project/builds/:build" do
get "/:project/builds/:commit" do
login_required unless current_project.public?
show :build, :title => ["projects", current_project.permalink, current_build.short_commit_identifier]
show :build, :title => ["projects", current_project.permalink, current_commit.short_commit_identifier]
end

get "/integrity.css" do
Expand Down
3 changes: 2 additions & 1 deletion lib/integrity.rb
Expand Up @@ -20,6 +20,7 @@
require "core_ext/string"

require "project"
require "commit"
require "build"
require "project_builder"
require "scm"
Expand Down Expand Up @@ -78,4 +79,4 @@ def call(severity, time, progname, msg)
time.strftime("[%H:%M:%S] ") + msg2str(msg) + "\n"
end
end
end
end
87 changes: 51 additions & 36 deletions lib/integrity/build.rb
Expand Up @@ -2,60 +2,75 @@ module Integrity
class Build
include DataMapper::Resource

property :id, Serial
property :output, Text, :nullable => false, :default => ""
property :successful, Boolean, :nullable => false, :default => false
property :commit_identifier, String, :nullable => false
property :commit_metadata, Yaml, :nullable => false, :lazy => false
property :created_at, DateTime
property :updated_at, DateTime
property :id, Integer, :serial => true
property :output, Text, :default => "", :lazy => false
property :successful, Boolean, :default => false
property :commit_id, Integer, :nullable => false
property :created_at, DateTime
property :updated_at, DateTime
property :started_at, DateTime
property :completed_at, DateTime

belongs_to :project, :class_name => "Integrity::Project"
belongs_to :commit, :class_name => "Integrity::Commit"

def self.pending
all(:started_at => nil)
end

def pending?
started_at.nil?
end

def failed?
!successful?
end

def status
successful? ? :success : :failed
end

def human_readable_status
successful? ? "Build Successful" : "Build Failed"
case
when pending? then :pending
when successful? then :success
when failed? then :failed
end
end


#
# Deprecated methods
#
def short_commit_identifier
sha1?(commit_identifier) ? commit_identifier[0..6] : commit_identifier
warn "Build#short_commit_identifier is deprecated, use Commit#short_identifier"
commit.short_identifier
end

def commit_metadata
case data = attribute_get(:commit_metadata)
when String; YAML.load(data)
else data
end
def commit_identifier
warn "Build#commit_identifier is deprecated, use Commit#identifier"
commit.identifier
end

def commit_author
@author ||= begin
commit_metadata[:author] =~ /^(.*) <(.*)>$/
OpenStruct.new(:name => $1.strip, :email => $2.strip, :full => commit_metadata[:author])
end
warn "Build#commit_author is deprecated, use Commit#author"
commit.author
end

def commit_message
commit_metadata[:message]
warn "Build#commit_message is deprecated, use Commit#message"
commit.message
end

def commited_at
case commit_metadata[:date]
when String then Time.parse(commit_metadata[:date])
else commit_metadata[:date]
end
warn "Build#commited_at is deprecated, use Commit#committed_at"
commit.committed_at
end

def project_id
warn "Build#project_id is deprecated, use Commit#project_id"
commit.project_id
end

def commit_metadata
warn "Build#commit_metadata is deprecated, use the different methods in Commit instead"
{ :message => commit.message,
:author => commit.author,
:date => commit.committed_at }
end

private
def sha1?(string)
string =~ /^[a-f0-9]{40}$/
end
end
end
end
72 changes: 72 additions & 0 deletions lib/integrity/commit.rb
@@ -0,0 +1,72 @@
module Integrity
class Commit
include DataMapper::Resource

property :id, Integer, :serial => true
property :identifier, String, :nullable => false
property :message, String, :nullable => false, :length => 255
property :author, String, :nullable => false, :length => 255
property :committed_at, DateTime, :nullable => false
property :created_at, DateTime
property :updated_at, DateTime

belongs_to :project, :class_name => "Integrity::Project"
has 1, :build, :class_name => "Integrity::Build"

def short_identifier
identifier.to_s[0..6]
end

def author
@structured_author ||= attribute_get(:author).tap do |author_string|
author_string =~ /^(.*) <(.*)>$/
author_string.singleton_def(:name) { $1.strip }
author_string.singleton_def(:email) { $2.strip }
author_string.singleton_def(:full) { author_string }
end
end

def status
build.nil? ? :pending : build.status
end

def successful?
status == :success
end

def failed?
status == :failed
end

def pending?
status == :pending
end

def human_readable_status
case status
when :success; "Built #{short_identifier} successfully"
when :failed; "Built #{short_identifier} and failed"
when :pending; "#{short_identifier} hasn't been built yet"
end
end

def output
build && build.output
end

def queue_build
self.build = Build.create(:commit_id => id)
self.save

# Build on foreground (this will move away, I promise)
ProjectBuilder.new(project).build(self)
end

# Deprecation layer
alias :short_commit_identifier :short_identifier
alias :commit_identifier :identifier
alias :commit_author :author
alias :commit_message :message
alias :commited_at :committed_at
end
end
12 changes: 11 additions & 1 deletion lib/integrity/core_ext/object.rb
Expand Up @@ -3,4 +3,14 @@ def tap
yield self
self
end
end

def singleton_class
class << self; self; end
end

def singleton_def(name, &block)
singleton_class.instance_eval do
define_method(name, &block)
end
end
end
4 changes: 2 additions & 2 deletions lib/integrity/helpers.rb
Expand Up @@ -38,8 +38,8 @@ def current_project
@project ||= Project.first(:permalink => params[:project]) or raise Sinatra::NotFound
end

def current_build
@build ||= current_project.builds.first(:commit_identifier => params[:build]) or raise Sinatra::NotFound
def current_commit
@commit ||= current_project.commits.first(:identifier => params[:commit]) or raise Sinatra::NotFound
end

def show(view, options={})
Expand Down
2 changes: 1 addition & 1 deletion lib/integrity/installer.rb
Expand Up @@ -64,7 +64,7 @@ def edit_template_files
edit_integrity_configuration
edit_thin_configuration
end

def edit_integrity_configuration
config = File.read(root / "config.yml")
config.gsub! %r(sqlite3:///var/integrity.db), "sqlite3://#{root}/integrity.db"
Expand Down

0 comments on commit 7721421

Please sign in to comment.