oliyoung-integrity 0.1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/README.markdown +73 -0
  2. data/Rakefile +118 -0
  3. data/bin/integrity +4 -0
  4. data/config/config.sample.ru +21 -0
  5. data/config/config.sample.yml +41 -0
  6. data/config/thin.sample.yml +13 -0
  7. data/lib/integrity.rb +72 -0
  8. data/lib/integrity/app.rb +138 -0
  9. data/lib/integrity/author.rb +39 -0
  10. data/lib/integrity/build.rb +84 -0
  11. data/lib/integrity/commit.rb +71 -0
  12. data/lib/integrity/core_ext/object.rb +6 -0
  13. data/lib/integrity/helpers.rb +16 -0
  14. data/lib/integrity/helpers/authorization.rb +33 -0
  15. data/lib/integrity/helpers/breadcrumbs.rb +20 -0
  16. data/lib/integrity/helpers/forms.rb +28 -0
  17. data/lib/integrity/helpers/pretty_output.rb +45 -0
  18. data/lib/integrity/helpers/rendering.rb +19 -0
  19. data/lib/integrity/helpers/resources.rb +19 -0
  20. data/lib/integrity/helpers/urls.rb +49 -0
  21. data/lib/integrity/installer.rb +131 -0
  22. data/lib/integrity/migrations.rb +140 -0
  23. data/lib/integrity/notifier.rb +48 -0
  24. data/lib/integrity/notifier/base.rb +65 -0
  25. data/lib/integrity/project.rb +153 -0
  26. data/lib/integrity/project_builder.rb +56 -0
  27. data/lib/integrity/scm.rb +19 -0
  28. data/lib/integrity/scm/git.rb +84 -0
  29. data/lib/integrity/scm/git/uri.rb +57 -0
  30. data/public/buttons.css +82 -0
  31. data/public/reset.css +7 -0
  32. data/public/spinner.gif +0 -0
  33. data/test/acceptance/api_test.rb +97 -0
  34. data/test/acceptance/browse_project_builds_test.rb +65 -0
  35. data/test/acceptance/browse_project_test.rb +95 -0
  36. data/test/acceptance/build_notifications_test.rb +42 -0
  37. data/test/acceptance/create_project_test.rb +97 -0
  38. data/test/acceptance/delete_project_test.rb +53 -0
  39. data/test/acceptance/edit_project_test.rb +117 -0
  40. data/test/acceptance/error_page_test.rb +18 -0
  41. data/test/acceptance/helpers.rb +2 -0
  42. data/test/acceptance/installer_test.rb +82 -0
  43. data/test/acceptance/manual_build_project_test.rb +82 -0
  44. data/test/acceptance/notifier_test.rb +109 -0
  45. data/test/acceptance/project_syndication_test.rb +30 -0
  46. data/test/acceptance/stylesheet_test.rb +18 -0
  47. data/test/helpers.rb +80 -0
  48. data/test/helpers/acceptance.rb +78 -0
  49. data/test/helpers/acceptance/email_notifier.rb +55 -0
  50. data/test/helpers/acceptance/git_helper.rb +99 -0
  51. data/test/helpers/acceptance/textfile_notifier.rb +26 -0
  52. data/test/helpers/expectations.rb +4 -0
  53. data/test/helpers/expectations/be_a.rb +23 -0
  54. data/test/helpers/expectations/change.rb +90 -0
  55. data/test/helpers/expectations/have.rb +105 -0
  56. data/test/helpers/expectations/predicates.rb +37 -0
  57. data/test/helpers/fixtures.rb +87 -0
  58. data/test/helpers/initial_migration_fixture.sql +44 -0
  59. data/test/unit/build_test.rb +51 -0
  60. data/test/unit/commit_test.rb +83 -0
  61. data/test/unit/helpers_test.rb +56 -0
  62. data/test/unit/integrity_test.rb +18 -0
  63. data/test/unit/migrations_test.rb +56 -0
  64. data/test/unit/notifier_test.rb +123 -0
  65. data/test/unit/project_builder_test.rb +111 -0
  66. data/test/unit/project_test.rb +282 -0
  67. data/test/unit/scm_test.rb +54 -0
  68. data/views/_commit_info.haml +24 -0
  69. data/views/build.haml +2 -0
  70. data/views/error.haml +37 -0
  71. data/views/home.haml +21 -0
  72. data/views/integrity.sass +400 -0
  73. data/views/layout.haml +28 -0
  74. data/views/new.haml +51 -0
  75. data/views/not_found.haml +31 -0
  76. data/views/notifier.haml +7 -0
  77. data/views/project.builder +21 -0
  78. data/views/project.haml +30 -0
  79. data/views/unauthorized.haml +38 -0
  80. metadata +225 -0
@@ -0,0 +1,131 @@
1
+ require "thor"
2
+ require File.dirname(__FILE__) + "/../integrity"
3
+
4
+ module Integrity
5
+ class Installer < Thor
6
+ include FileUtils
7
+
8
+ desc "install [PATH]",
9
+ "Copy template files to PATH for desired deployement strategy
10
+ (either Thin, Passenger or Heroku). Next, go there and edit them."
11
+ method_options :passenger => :boolean,
12
+ :thin => :boolean,
13
+ :heroku => :boolean
14
+ def install(path)
15
+ @root = Pathname(path).expand_path
16
+
17
+ if options[:heroku]
18
+ cp_r Pathname(__FILE__).join("../../../config/heroku"), root
19
+ puts <<EOF
20
+ Your Integrity install is ready to be deployed onto Heroku. Next steps:
21
+
22
+ 1. git init && git add . && git commit -am "Initial import"
23
+ 2. heroku create
24
+ 3. git push heroku master
25
+ 4. heroku rake db:migrate
26
+ EOF
27
+ else
28
+ create_dir_structure
29
+ copy_template_files
30
+ edit_template_files
31
+ migrate_db(root.join("config.yml"))
32
+ after_setup_message
33
+ end
34
+ end
35
+
36
+ desc "migrate_db [CONFIG]",
37
+ "Checks the `database_uri` in CONFIG and migrates the
38
+ database up to the lastest version."
39
+ def migrate_db(config)
40
+ Integrity.new(config)
41
+
42
+ require "integrity/migrations"
43
+ Integrity.migrate_db
44
+ end
45
+
46
+ desc "launch [CONFIG]",
47
+ "Launch Integrity real quick."
48
+ method_options :config => :optional, :port => 4567
49
+ def launch
50
+ require "thin"
51
+ require "do_sqlite3"
52
+
53
+ if File.file?(options[:config].to_s)
54
+ Integrity.new(options[:config])
55
+ else
56
+ DataMapper.setup(:default, "sqlite3::memory:")
57
+ end
58
+
59
+ DataMapper.auto_migrate!
60
+
61
+ Thin::Server.start("0.0.0.0", options[:port], Integrity::App)
62
+ rescue LoadError => boom
63
+ missing_dependency = boom.message.split("--").last.lstrip
64
+ puts "Please install #{missing_dependency} to launch Integrity"
65
+ end
66
+
67
+ private
68
+ attr_reader :root
69
+
70
+ def create_dir_structure
71
+ mkdir_p root
72
+
73
+ mkdir_p root / "builds"
74
+ mkdir_p root / "log"
75
+
76
+ if options[:passenger]
77
+ mkdir_p root / "public"
78
+ mkdir_p root / "tmp"
79
+ end
80
+ end
81
+
82
+ def copy_template_files
83
+ copy "config.sample.ru"
84
+ copy "config.sample.yml"
85
+ copy "thin.sample.yml" if options[:thin]
86
+ end
87
+
88
+ def edit_template_files
89
+ edit_integrity_configuration
90
+ edit_thin_configuration if options[:thin]
91
+ end
92
+
93
+ def edit_integrity_configuration
94
+ config = File.read(root / "config.yml")
95
+ config.gsub! %r(sqlite3:///var/integrity.db), "sqlite3://#{root}/integrity.db"
96
+ config.gsub! %r(/path/to/scm/exports), "#{root}/builds"
97
+ config.gsub! %r(/var/log), "#{root}/log"
98
+ File.open(root / "config.yml", "w") { |f| f.puts config }
99
+ end
100
+
101
+ def edit_thin_configuration
102
+ config = File.read(root / "thin.yml")
103
+ config.gsub! %r(/apps/integrity), root
104
+ File.open(root / "thin.yml", 'w') { |f| f.puts config }
105
+ end
106
+
107
+ def after_setup_message
108
+ puts <<EOF
109
+ Awesome! Integrity was installed successfully!
110
+
111
+ If you want to enable notifiers, install the gems and then require them
112
+ in #{root}/config.ru
113
+
114
+ For example:
115
+
116
+ sudo gem install -s http://gems.github.com foca-integrity-email)
117
+
118
+ And then in #{root}/config.ru add:
119
+
120
+ require "notifier/email"
121
+
122
+ Don't forget to tweak #{root / "config.yml"} to your needs.
123
+ EOF
124
+ end
125
+
126
+ def copy(source)
127
+ cp(Pathname(__FILE__).dirname.join("../../config", source),
128
+ root.join(File.basename(source).gsub(/\.sample/, "")))
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,140 @@
1
+ require "dm-migrations"
2
+ require "migration_runner"
3
+
4
+ module Integrity
5
+ def self.migrate_db
6
+ setup_initial_migration if pre_migrations?
7
+ Integrity::Migrations.migrate_up!
8
+ end
9
+
10
+ def self.setup_initial_migration
11
+ database_adapter.execute %q(CREATE TABLE "migration_info" ("migration_name" VARCHAR(255));)
12
+ database_adapter.execute %q(INSERT INTO "migration_info" ("migration_name") VALUES ("initial"))
13
+ end
14
+
15
+ def self.pre_migrations?
16
+ !table_exists?("migration_info") &&
17
+ ( table_exists?("integrity_projects") &&
18
+ table_exists?("integrity_builds") &&
19
+ table_exists?("integrity_notifiers") )
20
+ end
21
+
22
+ def self.table_exists?(table_name)
23
+ database_adapter.storage_exists?(table_name)
24
+ end
25
+
26
+ def self.database_adapter
27
+ DataMapper.repository(:default).adapter
28
+ end
29
+
30
+ module Migrations
31
+ # This is what is actually happening:
32
+ # include DataMapper::MigrationRunner
33
+
34
+ include DataMapper::Types
35
+
36
+ migration 1, :initial, :verbose => true do
37
+ up do
38
+ create_table :integrity_projects do
39
+ column :id, Integer, :serial => true
40
+ column :name, String, :nullable => false
41
+ column :permalink, String
42
+ column :uri, URI, :nullable => false
43
+ column :branch, String, :nullable => false, :default => "master"
44
+ column :command, String, :nullable => false, :default => "rake"
45
+ column :public, Boolean, :default => true
46
+ column :building, Boolean, :default => false
47
+ column :created_at, DateTime
48
+ column :updated_at, DateTime
49
+
50
+ column :build_id, Integer
51
+ column :notifier_id, Integer
52
+ end
53
+
54
+ create_table :integrity_builds do
55
+ column :id, Integer, :serial => true
56
+ column :output, Text, :nullable => false, :default => ""
57
+ column :successful, Boolean, :nullable => false, :default => false
58
+ column :commit_identifier, String, :nullable => false
59
+ column :commit_metadata, Yaml, :nullable => false
60
+ column :created_at, DateTime
61
+ column :updated_at, DateTime
62
+
63
+ column :project_id, Integer
64
+ end
65
+
66
+ create_table :integrity_notifiers do
67
+ column :id, Integer, :serial => true
68
+ column :name, String, :nullable => false
69
+ column :config, Yaml, :nullable => false
70
+
71
+ column :project_id, Integer
72
+ end
73
+ end
74
+ end
75
+
76
+ migration 2, :add_commits, :verbose => true do
77
+ up do
78
+ class ::Integrity::Build
79
+ property :commit_identifier, String
80
+ property :commit_metadata, Yaml, :lazy => false
81
+ property :project_id, Integer
82
+ end
83
+
84
+ create_table :integrity_commits do
85
+ column :id, Integer, :serial => true
86
+ column :identifier, String, :nullable => false
87
+ column :message, String, :nullable => false, :length => 255
88
+ column :author, String, :nullable => false, :length => 255
89
+ column :committed_at, DateTime, :nullable => false
90
+ column :created_at, DateTime
91
+ column :updated_at, DateTime
92
+
93
+ column :project_id, Integer
94
+ end
95
+
96
+ modify_table :integrity_builds do
97
+ add_column :commit_id, Integer
98
+ add_column :started_at, DateTime
99
+ add_column :completed_at, DateTime
100
+ end
101
+
102
+ # Die, orphans, die
103
+ Build.all(:project_id => nil).destroy!
104
+
105
+ # sqlite hodgepockery
106
+ all_builds = Build.all.each {|b| b.freeze }
107
+ drop_table :integrity_builds
108
+ create_table :integrity_builds do
109
+ column :id, Integer, :serial => true
110
+ column :started_at, DateTime
111
+ column :completed_at, DateTime
112
+ column :successful, Boolean
113
+ column :output, Text, :nullable => false, :default => ""
114
+ column :created_at, DateTime
115
+ column :updated_at, DateTime
116
+
117
+ column :commit_id, Integer
118
+ end
119
+
120
+ all_builds.each do |build|
121
+ commit = Commit.first(:identifier => build.commit_identifier)
122
+
123
+ if commit.nil?
124
+ commit = Commit.create(:identifier => build.commit_identifier,
125
+ :message => build.commit_metadata[:message],
126
+ :author => build.commit_metadata[:author],
127
+ :committed_at => build.commit_metadata[:date],
128
+ :project_id => build.project_id)
129
+ end
130
+
131
+ Build.create(:commit_id => commit.id,
132
+ :started_at => build.created_at,
133
+ :completed_at => build.updated_at,
134
+ :successful => build.successful,
135
+ :output => build.output)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + "/notifier/base"
2
+
3
+ module Integrity
4
+ class Notifier
5
+ include DataMapper::Resource
6
+
7
+ property :id, Integer, :serial => true
8
+ property :name, String, :nullable => false
9
+ property :config, Yaml, :nullable => false, :lazy => false
10
+
11
+ belongs_to :project, :class_name => "Integrity::Project"
12
+
13
+ validates_is_unique :name, :scope => :project_id
14
+ validates_present :project_id
15
+
16
+ def self.available
17
+ constants.map { |name| const_get(name) }.select { |notifier| valid_notifier?(notifier) }
18
+ end
19
+
20
+ def self.enable_notifiers(project, enabled, config={})
21
+ all(:project_id => project).destroy!
22
+ list_of_enabled_notifiers(enabled).each do |name|
23
+ create! :project_id => project, :name => name, :config => config[name]
24
+ end
25
+
26
+ end
27
+
28
+ def notify_of_build(build)
29
+ to_const.notify_of_build(build, config)
30
+ end
31
+
32
+ private
33
+
34
+ def to_const
35
+ self.class.module_eval(name)
36
+ end
37
+
38
+ def self.list_of_enabled_notifiers(names)
39
+ [*names].reject { |n| n.nil? }
40
+ end
41
+ private_class_method :list_of_enabled_notifiers
42
+
43
+ def self.valid_notifier?(notifier)
44
+ notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) && notifier != Notifier::Base
45
+ end
46
+ private_class_method :valid_notifier?
47
+ end
48
+ end
@@ -0,0 +1,65 @@
1
+ module Integrity
2
+ class Notifier
3
+ class Base
4
+ def self.notify_of_build(build, config)
5
+ Timeout.timeout(8) { new(build, config).deliver! }
6
+ end
7
+
8
+ def self.to_haml
9
+ raise NoMethodError, "you need to implement this method in your notifier"
10
+ end
11
+
12
+ attr_reader :commit
13
+
14
+ def initialize(commit, config)
15
+ @commit = commit
16
+ @config = config
17
+ end
18
+
19
+ def build
20
+ warn "Notifier::Base#build is deprecated, use Notifier::Base#commit instead"
21
+ commit
22
+ end
23
+
24
+ def deliver!
25
+ raise NoMethodError, "you need to implement this method in your notifier"
26
+ end
27
+
28
+ def short_message
29
+ commit.human_readable_status
30
+ end
31
+
32
+ def full_message
33
+ <<-EOM
34
+ "Build #{commit.identifier} #{commit.successful? ? "was successful" : "failed"}"
35
+
36
+ Commit Message: #{commit.message}
37
+ Commit Date: #{commit.committed_at}
38
+ Commit Author: #{commit.author.name}
39
+
40
+ Link: #{commit_url}
41
+
42
+ Build Output:
43
+
44
+ #{stripped_commit_output}
45
+ EOM
46
+ end
47
+
48
+ def commit_url
49
+ raise if Integrity.config[:base_uri].nil?
50
+ Integrity.config[:base_uri] / commit.project.permalink / "commits" / commit.identifier
51
+ end
52
+
53
+ private
54
+
55
+ def stripped_commit_output
56
+ commit.output.gsub("\e[0m", "").gsub(/\e\[3[1-7]m/, "")
57
+ end
58
+
59
+ def stripped_build_output
60
+ warn "Notifier::Base#stripped_build_output is deprecated, use Notifier::base#stripped_commit_output instead"
61
+ stripped_commit_output
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,153 @@
1
+ module Integrity
2
+ class Project
3
+ include DataMapper::Resource
4
+
5
+ property :id, Integer, :serial => true
6
+ property :name, String, :nullable => false
7
+ property :permalink, String
8
+ property :uri, URI, :nullable => false, :length => 255
9
+ property :branch, String, :nullable => false, :default => "master"
10
+ property :command, String, :nullable => false, :length => 255, :default => "rake"
11
+ property :public, Boolean, :default => true
12
+ property :building, Boolean, :default => false
13
+ property :created_at, DateTime
14
+ property :updated_at, DateTime
15
+
16
+ has n, :commits, :class_name => "Integrity::Commit"
17
+ has n, :notifiers, :class_name => "Integrity::Notifier"
18
+
19
+ before :save, :set_permalink
20
+ before :destroy, :delete_code
21
+
22
+ validates_is_unique :name
23
+
24
+ def self.only_public_unless(condition)
25
+ if condition
26
+ all
27
+ else
28
+ all(:public => true)
29
+ end
30
+ end
31
+
32
+ def build(commit_identifier="HEAD")
33
+ commit_identifier = head_of_remote_repo if commit_identifier == "HEAD"
34
+ commit = find_or_create_commit_with_identifier(commit_identifier)
35
+ commit.queue_build
36
+ end
37
+
38
+ def push(payload)
39
+ payload = parse_payload(payload)
40
+ raise ArgumentError unless valid_payload?(payload)
41
+
42
+ commits =
43
+ if Integrity.config[:build_all_commits]
44
+ payload["commits"]
45
+ else
46
+ [ payload["commits"].first ]
47
+ end
48
+
49
+ commits.each do |commit_data|
50
+ create_commit_from(commit_data)
51
+ build(commit_data["id"])
52
+ end
53
+ end
54
+
55
+ def last_commit
56
+ commits.first(:project_id => id, :order => [:committed_at.desc])
57
+ end
58
+
59
+ def last_build
60
+ warn "Project#last_build is deprecated, use Project#last_commit"
61
+ last_commit
62
+ end
63
+
64
+ def previous_commits
65
+ commits.all(:project_id => id, :order => [:committed_at.desc]).tap {|commits| commits.shift }
66
+ end
67
+
68
+ def previous_builds
69
+ warn "Project#previous_builds is deprecated, use Project#previous_commits"
70
+ previous_commits
71
+ end
72
+
73
+ def status
74
+ last_commit && last_commit.status
75
+ end
76
+
77
+ def human_readable_status
78
+ last_commit && last_commit.human_readable_status
79
+ end
80
+
81
+ def public=(flag)
82
+ attribute_set(:public, case flag
83
+ when "1", "0" then flag == "1"
84
+ else !!flag
85
+ end)
86
+ end
87
+
88
+ def config_for(notifier)
89
+ notifier = notifiers.first(:name => notifier.to_s.split(/::/).last, :project_id => id)
90
+ notifier.blank? ? {} : notifier.config
91
+ end
92
+
93
+ def notifies?(notifier)
94
+ !notifiers.first(:name => notifier.to_s.split(/::/).last, :project_id => id).blank?
95
+ end
96
+
97
+ def enable_notifiers(*args)
98
+ Notifier.enable_notifiers(id, *args)
99
+ end
100
+
101
+ private
102
+ def find_or_create_commit_with_identifier(commit_identifier)
103
+ # We abuse +committed_at+ here setting it to Time.now because we use it
104
+ # to sort (for last_commit and previous_commits). I don't like this
105
+ # very much, but for now it's the only solution I can find.
106
+ #
107
+ # This also creates a dependency, as now we *always* have to update the
108
+ # +committed_at+ field after building to ensure the date is correct :(
109
+ #
110
+ # This might also make your commit listings a little jumpy, if some
111
+ # commits change place every time a build finishes =\
112
+ commits.first_or_create({ :identifier => commit_identifier, :project_id => id }, :committed_at => Time.now)
113
+ end
114
+
115
+ def head_of_remote_repo
116
+ SCM.new(uri, branch).head
117
+ end
118
+
119
+ def create_commit_from(data)
120
+ commits.create(:identifier => data["id"],
121
+ :author => "#{data["author"]["name"]} <#{data["author"]["email"]}>",
122
+ :message => data["message"],
123
+ :committed_at => data["timestamp"])
124
+ end
125
+
126
+ def set_permalink
127
+ self.permalink = (name || "").downcase.
128
+ gsub(/'s/, "s").
129
+ gsub(/&/, "and").
130
+ gsub(/[^a-z0-9]+/, "-").
131
+ gsub(/-*$/, "")
132
+ end
133
+
134
+ def delete_code
135
+ commits.all(:project_id => id).destroy!
136
+ ProjectBuilder.new(self).delete_code
137
+ rescue SCM::SCMUnknownError => error
138
+ Integrity.log "Problem while trying to deleting code: #{error}"
139
+ end
140
+
141
+ def valid_payload?(payload)
142
+ payload && payload["ref"].to_s.include?(branch) &&
143
+ !payload["commits"].nil? &&
144
+ !payload["commits"].to_a.empty?
145
+ end
146
+
147
+ def parse_payload(payload)
148
+ JSON.parse(payload.to_s)
149
+ rescue JSON::ParserError
150
+ false
151
+ end
152
+ end
153
+ end