sr (owner)

Revisions

  • 610de1 sr Thu Sep 17 08:51:58 -0700 2009
  • f4a0fa sr Wed Sep 16 10:15:50 -0700 2009
  • b694e9 sr Wed Sep 16 08:58:56 -0700 2009
  • b371e0 sr Tue Sep 15 18:04:21 -0700 2009
  • 8a88d0 sr Tue Sep 15 17:03:08 -0700 2009
gist: 188123 Download_button fork
public
Description:
integrity + dj (hack)
Public Clone URL: git://gist.github.com/188123.git
Embed All Files: show embed
config.ru #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require "integrity-dj"
 
Integrity.new(:database_uri => "sqlite3:integrity.db")
Integrity.setup_dj(:adapter => "sqlite3", :db => "dj.db", :log => "dj.log")
 
class Bobette::JSON
  def initialize(app, &block)
    @app = app
  end
 
  def call(env)
    payload = Rack::Request.new(env).POST["payload"] || ""
    payload = JSON.parse(payload)
    @app.call(env.update("bobette.payload" => payload))
  rescue JSON::JSONError
    Rack::Response.new("Unparsable payload", 400).finish
  end
end
 
map "/github" do
  use Bobette::JSON
  run Bobette.new(BuildableJob)
end
 
map "/" do
  use Sinatra::ShowExceptions
  run Integrity::App
end
 
deps.rip #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ripenv: integrity-dj
 
activerecord (2.3.2)
data_objects (0.9.12)
dm-validations (0.9.11)
uuidtools (2.0.0)
haml (2.2.2)
sinatra-url-for (1df3392)
addressable (2.1.0)
dm-types (0.9.11)
thor (0.9.9)
dm-aggregates (0.9.11)
delayed_job (unversioned)
dm-core (0.9.11)
dm-timestamps (0.9.11)
rack (1.0.0)
json (1.1.7)
sinatra (0.9.4)
bcrypt-ruby (2.0.5)
extlib (0.9.12)
activesupport (2.3.4)
sinatra-captcha (fbcefa9)
do_sqlite3 (0.9.12)
sinatra-authorization (1.0.0)
bobette (c47b2d1)
 
init.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$LOAD_PATH.unshift(File.dirname(__FILE__))
 
module Haml
  module Version
    @@version = "2.2.2"
  end
end
 
require "activerecord"
require "delayed_job"
require "bobette"
require "bobette/github"
require "integrity-dj"
 
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
  :adapter => "sqlite3",
  :database => "dj.db"
)
 
ActiveRecord::Schema.define do
  create_table :delayed_jobs, :force => true do |table|
    table.integer :priority, :default => 0
    table.integer :attempts, :default => 0
    table.text :handler
    table.string :last_error
    table.datetime :run_at
    table.datetime :locked_at
    table.datetime :failed_at
    table.string :locked_by
    table.timestamps
  end
end unless Delayed::Job.table_exists?
 
DataMapper.setup(:default, "sqlite3:integrity.db")
DataMapper.auto_migrate!
 
integrity-dj.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Haml
  module Version
    @@version = "2.2.2"
  end
end if ENV["RIPDIR"]
 
require "integrity"
require "delayed_job"
require "bobette"
require "bobette/github"
require "beacon"
require "ostruct"
 
module Integrity
  def self.setup_dj(options={})
    ActiveRecord::Base.logger = Logger.new(options[:log])
    ActiveRecord::Base.establish_connection(
      :adapter => options[:adapter],
      :database => options[:db]
    )
 
    ActiveRecord::Schema.define do
      create_table :delayed_jobs, :force => true do |table|
        table.integer :priority, :default => 0
        table.integer :attempts, :default => 0
        table.text :handler
        table.text :last_error
        table.datetime :run_at
        table.datetime :locked_at
        table.datetime :failed_at
        table.string :locked_by
        table.timestamps
      end
    end unless Delayed::Job.table_exists?
  end
 
  class BuildableJob
    def self.call(payload)
      project = Integrity::Project.first(
        :scm => payload["scm"],
        :uri => payload["uri"],
        :branch => payload["branch"]
      )
 
      return unless project
 
      payload["commits"].map { |c|
        Integrity::BuildableJob.new(
          OpenStruct.new(project.attributes.update(:commit => c["id"]))
        )
      }
    end
 
    attr_reader :buildable
 
    def initialize(buildable)
      @buildable = buildable
    end
 
    def build
      Delayed::Job.enqueue(self)
    end
 
    def perform
      scm.with_commit(buildable.commit) { |commit|
        Beacon.fire(:done, buildable, commit, *run)
      }
    end
 
    private
      def run
        output = nil
        IO.popen(script, "r") { |io| output = io.read }
        [$?.success?, output]
      end
 
      def script
        "(cd #{scm.dir_for(buildable.commit)} && #{buildable.command} 2>&1)"
      end
 
      def scm
        @scm ||= Bob::SCM.new(buildable.scm, buildable.uri, buildable.branch)
      end
  end
end
 
Beacon.watch(:done) { |buildable, commit, state, output|
  project = Integrity::Project.first(
    :scm => buildable.scm,
    :uri => buildable.uri,
    :branch => buildable.branch
  )
 
  next unless project
 
  build = Integrity::Build.new(
    :output => output,
    :successful => state,
    :started_at => Time.now, # TODO
    :completed_at => Time.now
  )
 
  Integrity::Commit.first_or_create({:identifier => commit["id"]},
    commit.merge(:build => build, :project_id => project.id))
}