Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
several various bugfixes
  • Loading branch information
movitto committed Apr 14, 2010
1 parent 07d5197 commit 028f538
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 55 deletions.
4 changes: 3 additions & 1 deletion TODO
Expand Up @@ -2,4 +2,6 @@
- Syncronization locks needed! (on all shared artifacts, db objects)
- More event handlers, generic handler interface,
workflow integration to automatically chain handlers together
- multiple user support, RBAC and ACL mechanisms
- Multiple user support, RBAC and ACL mechanisms
- Project dependencies (w/ versions)
- Integrate mock into build events to build packages in multiple seperate environments
4 changes: 3 additions & 1 deletion db/models/event.rb
Expand Up @@ -35,6 +35,8 @@ def self.processes
validates_inclusion_of :version_qualifier, :in => VERSION_QUALIFIERS,
:if => Proc.new { |e| !e.version_qualifier.nil? }

# nil version and version_qualifier indicates event will be run for _all_ versons

validates_presence_of :version,
:if => Proc.new { |e| !e.version_qualifier.nil? }

Expand All @@ -43,7 +45,7 @@ def self.processes

# determine if event applies to specified version
def applies_to_version?(cversion)
raise ArgumentError, "valid event version #{version} and version #{cversion} required" unless version.class == String && cversion.class == String
raise ArgumentError, "valid event version #{version} and version #{cversion} required" unless (version.nil? || version.class == String) && cversion.class == String

# XXX remove any non-numeric chars from the version number (eg if a version has a '-beta' or whatnot, not pretty but works for now
cversion.gsub(/[a-zA-Z]*/, '')
Expand Down
1 change: 1 addition & 0 deletions db/models/project.rb
Expand Up @@ -51,6 +51,7 @@ def sources_for_version(version)
# Get the project primary source
def primary_source
ps = projects_sources.all.find { |ps| ps.primary_source }
# TODO special case if no sources are marked as primary, grab the first ? (also in primary_source_for_version below)
return ps.nil? ? nil : ps.source
end

Expand Down
10 changes: 9 additions & 1 deletion db/models/projects_source.rb
Expand Up @@ -10,16 +10,24 @@
# General Public License, along with Polisher. If not, see
# <http://www.gnu.org/licenses/>

# FIXME rename to ProjectSourceVersion

class ProjectsSource < ActiveRecord::Base
belongs_to :project
belongs_to :source

# TODO destroy source on deletion only if no other projects_sources sharing the source exist
# FIXME destroy source on deletion only if no other projects_sources sharing the source exist

validates_uniqueness_of :source_id, :scope => [:project_id, :project_version]

# validate only one primary_source set to 'true' in scope of (project_id, project_version)
validates_uniqueness_of :primary_source,
:scope => [:project_id, :project_version],
:if => Proc.new { |ps| ps.primary_source }

before_save :normalize_versions
def normalize_versions
self.project_version = nil if project_version == ""
self.source_version = nil if source_version == ""
end
end
66 changes: 45 additions & 21 deletions lib/dsl.rb
Expand Up @@ -43,13 +43,27 @@ def initialize(args = {})
end

# Add a method to project for each source type, eg add_archive, add_path, add_repo, etc.
# Simply dispatch to source handler dsl method
::Source::SOURCE_TYPES.each { |st|
define_method "add_#{st}".intern do |args|
src = source(args)
yield src if block_given?
# TODO maintain as source_types are added (or put this this in a seperate module)
# XXX ran into this when doing it metaprogramatically http://coderrr.wordpress.com/2008/10/29/using-define_method-with-blocks-in-ruby-18/
def add_archive args = {}, &block; add_source('archive', args, &block); end
def add_patch args = {}, &block; add_source('patch', args, &block); end
def add_gem args = {}, &block; add_source('gem', args, &block); end
def add_file args = {}, &block; add_source('file', args, &block); end

def add_source type, args = {}, &block
args[:source_type] = type # automatically set the source type
src = source(args)

# Dispatch source to caller blocker to register specific project/source versions.
if !block.nil?
block.call src

# If no block is given register default */* project/source version
# TODO should this execute regardless of whether there is a block or not, eg only if no project/source versions were created/exist (need to perform a new query to get that info)
else
version nil, :corresponds_to => src
end
}
end

# Create Project from xml and return
def self.from_xml(xml_str)
Expand Down Expand Up @@ -107,20 +121,24 @@ def on_version(*args)
# Associate specified project version w/ corresponding source version if specified,
# else if not just return self
def version(version, args = {})
unless args.has_key?(:corresponds_to)
@project_version = version
return self
end
version = nil if version == "*"
@project_version = version
return self unless args.has_key?(:corresponds_to)

# dispatch to source.version so we don't have to implement twice
source = args[:corresponds_to]
args = {:project_id => id, :project_version => version,
:source_id => source.id, :source_version => source.source_version, :source_uri_params => source.uri_args}
RestClient.post("#{$polisher_uri}/projects_sources/create", args) { |response| Polisher.handle_response('created project source', response) }
source.version source.source_version, :corresponds_to => self
end

# Test fire project released event for specified version
def released(version, params = {})
rparams = { :name => name, :version => version}.merge(params)
RestClient.post("#{$polisher_uri}/projects/released", rparams ) { |response| Polisher.handle_response('released project', response) }
resource = RestClient::Resource.new("#{$polisher_uri}/projects/released", :timeout => 1000) # give event handlers plenty of time to run

sparams = "name=#{name}&version=#{version}"
params.each { |k,v| sparams += "&#{k}=#{v}" }
resource.post sparams do |response|
Polisher.handle_response('released project', response)
end
end
end

Expand All @@ -132,10 +150,13 @@ class Source
# Means to store source version and optional uri params to be used when setting up project sources
attr_accessor :source_version, :uri_args

# Means to store primary_source value when creating projects sources
attr_accessor :primary_source

def initialize(args = {})
args = { :id => nil, :name => nil, :uri => nil, :source_type => nil}.merge(args)
@id = args[:id] ; @name = args[:name] ; @uri = args[:uri] ; @source_type = args[:source_type]
@uri_args = ''
args = { :id => nil, :name => nil, :uri => nil, :source_type => nil, :primary_source => false}.merge(args)
@id = args[:id] ; @name = args[:name] ; @uri = args[:uri] ; @source_type = args[:source_type] ; @primary_sources = args[:primary_source]
@uri_args = '' ; @primary_source = false
end

# Create Source from xml and return
Expand Down Expand Up @@ -172,23 +193,25 @@ def create
# Associate specified project source w/ corresponding source version if specified,
# else if not just return self
def version(version, args = {})
version = nil if version == "*"
project = args.delete(:corresponds_to)

@uri_args = args.keys.collect { |k| k.to_s + "=" + args[k].to_s }.join(";")
@uri_args = args.keys.collect { |k| k.to_s + "=" + args[k].to_s }.join(";") unless args.empty?

if project.nil?
@source_version = version
return self
end

args = {:project_id => project.id, :project_version => project.project_version,
:source_id => id, :source_version => version, :source_uri_params => @uri_args}
:source_id => id, :source_version => version, :source_uri_params => @uri_args,
:primary_source => @primary_source }
RestClient.post("#{$polisher_uri}/projects_sources/create", args) { |response| Polisher.handle_response('created project source', response) }
end

# Set source as primary in project/source associations
def is_the_primary_source
@uri_args[:primary_source] = true
@primary_source = true
end
end

Expand Down Expand Up @@ -246,6 +269,7 @@ def source(args = {})
src = Polisher::Source.new args
src.create
src = source(args)
src.primary_source = args[:primary_source] if args.has_key?(:primary_source) # XXX ugly hack
yield src if block_given?
return src
end
11 changes: 8 additions & 3 deletions lib/event_handlers.rb
Expand Up @@ -38,7 +38,8 @@ def create_rpm_package(event, version, args = {})
sfh = File.open(spec_file, "wb")

# d/l projects sources into artifacts/SOURCES dir
project.download_to :dir => ARTIFACTS_DIR + "/SOURCES", :version => version
args.merge!({ :dir => ARTIFACTS_DIR + "/SOURCES", :version => version })
project.download_to args

# read template if specified
template = (template_file == '' || template_file.nil?) ? nil : File.read_all(template_file)
Expand Down Expand Up @@ -75,6 +76,8 @@ def create_rpm_package(event, version, args = {})

# run rpmbuild on spec
system("rpmbuild --define '_topdir #{ARTIFACTS_DIR}' -ba #{spec_file}")

# XXX FIXME this need to record all the rpms actually created
end

# Update specified yum repository w/ latest project artifact for specified version
Expand All @@ -85,10 +88,12 @@ def update_yum_repo(event, version, args = {})
# create the repository dir if it doesn't exist
Dir.mkdir repository unless File.directory? repository

# get the latest built rpm that matches gem name
# XXX FIXME this need to copy all the rpms created, including all that don't match the project name

# get the latest built rpm that matches the project name
project_src_rpm = Dir[ARTIFACTS_DIR + "/RPMS/*/#{project.name}-#{version}*.rpm"].
collect { |fn| File.new(fn) }.
sort { |f1,f2| file1.mtime <=> file2.mtime }.last
sort { |f1,f2| f1.mtime <=> f2.mtime }.last
project_tgt_rpm = "#{project.name}.rpm"

# grab the architecture from the directory the src file resides in
Expand Down
9 changes: 7 additions & 2 deletions public/stylesheets/style.css
Expand Up @@ -46,17 +46,22 @@ form tr > td:first-child{
padding-left: 10px;
}

#main h2,
#main h3,
#main h4,
#main h5,
#main h6{
font-size: 0.8em;
display: inline;
}

#main h4{
#main h1{
margin-top: 25px;
margin-bottom: 0;
}

#main h6{
#main h3,
#main h5{
padding-left: 15px;
}

Expand Down
8 changes: 4 additions & 4 deletions spec/dsl_spec.rb
Expand Up @@ -55,6 +55,8 @@
## Test firing events
#project(:name => "ruby").released "1.8.6", :any => "other", :optional => "params"

# TODO test primary_source in Polisher::Source, projects_sources/create rest call, and source method

require File.dirname(__FILE__) + '/spec_helper'

require 'thin'
Expand All @@ -71,12 +73,10 @@
proj = Polisher::Project.new :id => 20, :name => 'foobar'
proj.id.should == 20
proj.name.should == "foobar"

Source::SOURCE_TYPES.each { |st|
proj.method("add_#{st}".intern).should_not be_nil
}
end

# TODO test add_archive, add_file, add_patch, add_gem

it "should be instantiatable from xml" do
# TODO versions, sources, events
xml = "<project><id>10</id><name>foo</name></project>"
Expand Down
17 changes: 14 additions & 3 deletions spec/models_spec.rb
Expand Up @@ -318,6 +318,15 @@
ps.primary_source.should be_false
end

it "should default versions to nil" do
project = Project.create! :name => 'project-source-valid-testproj10'
source = Source.create! :name => 'project-source-valid-testsource10',
:uri => 'http://presvts098', :source_type => 'file'
ps = ProjectsSource.create! :project => project, :source => source, :project_version => "", :source_version => ""
ps.project_version.should be_nil
ps.source_version.should be_nil
end

it "should not be valid if a project id/version is associated w/ a source multiple times" do
project = Project.create! :name => 'project-source-valid-testproj1'
source = Source.create! :name => 'project-source-valid-testsource1',
Expand Down Expand Up @@ -433,9 +442,11 @@

it "should raise error if trying to compare invalid versions" do
event = Event.new
lambda {
event.applies_to_version?('0.5.2')
}.should raise_error(ArgumentError)

# nil event version and version_qualifier is permitted
#lambda {
# event.applies_to_version?('0.5.2')
#}.should raise_error(ArgumentError)

event.version = '0.7'
lambda {
Expand Down
4 changes: 2 additions & 2 deletions views/layout.haml
Expand Up @@ -13,9 +13,9 @@

%ul{:id => "primary_nav"}
%li
%a{:href => url_for("/projects")}projects
%a{:href => url_for("/projects.html")}projects
%li
%a{:href => url_for("/sources")}sources
%a{:href => url_for("/sources.html")}sources

%div{:style => "clear: both;"}

Expand Down
20 changes: 11 additions & 9 deletions views/projects/index.html.haml
@@ -1,24 +1,25 @@
%h4
%h1
Projects

%ul
- @projects.sort { |x,y| x.name <=> y.name }.each do |project|
%li
%h5
%h2
= project.name
%a{:href => url_for("/projects/destroy/#{project.id}"), :class => 'delete_project_link'}
(delete)
%br/
%h6
%h3
versions
%ul
- project.versions.each do |version|
- pevents = project.events_for_version(version)
- psources = project.projects_sources_for_version(version)
%li
%h7
%h4
= version
%h8
%br/
%h5
sources
%ul
- psources.each do |ps|
Expand All @@ -27,7 +28,8 @@
= ps.source_version
%a{:href => url_for("/projects_sources/destroy/#{ps.id}"), :class => 'delete_project_source_link'}
(delete)
%h6
%br/
%h3
events
%ul
- project.events.each do |event|
Expand All @@ -38,7 +40,7 @@
%a{:href => url_for("/events/destroy/#{event.id}"), :class => 'delete_event_link'}
(delete)

%h4
%h1
New Project

%form{:id => 'create_project_form', :method => "post", :action => url_for("/projects/create")}
Expand All @@ -53,7 +55,7 @@
%td
%input{:type => "submit", :value => "submit"}

%h4
%h1
New Project Source

%form{:id => 'create_project_source_form', :method => "post", :action => url_for("/projects_sources/create")}
Expand All @@ -79,7 +81,7 @@
%td
%input{:type => "submit", :value => "submit"}

%h4 New Event
%h1 New Event

%form{:id => 'create_event_form', :method => "post", :action => url_for("/events/create")}
%table
Expand Down

0 comments on commit 028f538

Please sign in to comment.