Class: CIJoe
- Inherits:
-
Object
- Object
- CIJoe
- Defined in:
- lib/cijoe/gmail.rb,
lib/cijoe.rb,
lib/cijoe/build.rb,
lib/cijoe/commit.rb,
lib/cijoe/config.rb,
lib/cijoe/server.rb,
lib/cijoe/version.rb,
lib/cijoe/campfire.rb
Overview
The CI Joe Gmail notifier is a mashup of two pieces of code.
1) This gist:
2) The CI Joe Campfire notifier.
The CI Joe Campfire notifier uses a valid_config? method to check that the notifier will be able to work. If so, it loads the Campfire module right into CI Joe, so that when CI Joe calls #notify, it’s calling the #notify on the Campfire module. Obviously, even though Chris was very explicit about not supporting any kind of notifier except the Campfire notifier, this is a very, very extensible API which is very, very friendly to repurposing. So I made with the repurpose. All you need to support is an #activate method on the module itself and a #notify instance method. Copying the #valid_config? pattern is strongly advised but absolutely not required.
Defined Under Namespace
Modules: Campfire, Gmail Classes: Build, Commit, Config, Server
Constant Summary collapse
- Version =
"0.1.3"
Instance Attribute Summary collapse
-
#current_build ⇒ Object
readonly
Returns the value of attribute current_build.
-
#last_build ⇒ Object
readonly
Returns the value of attribute last_build.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#build ⇒ Object
run the build but make sure only one is running at a time.
-
#build! ⇒ Object
update git then run the build.
-
#build_failed(output, error) ⇒ Object
build callbacks.
- #build_worked(output) ⇒ Object
-
#building? ⇒ Boolean
is a build running?.
- #finish_build(status, output) ⇒ Object
- #git_branch ⇒ Object
- #git_sha ⇒ Object
- #git_update ⇒ Object
- #git_user_and_project ⇒ Object
-
#initialize(project_path) ⇒ CIJoe
constructor
A new instance of CIJoe.
-
#pid ⇒ Object
the pid of the running child process.
-
#run_hook(hook) ⇒ Object
massage our repo.
-
#runner_command ⇒ Object
shellin’ out.
-
#stop ⇒ Object
kill the child and exit.
Constructor Details
#initialize(project_path) ⇒ CIJoe
Returns a new instance of CIJoe.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cijoe.rb', line 38 def initialize(project_path) project_path = File.(project_path) Dir.chdir(project_path) @user, @project = git_user_and_project @url = "http://github.com/#{@user}/#{@project}" @last_build = nil @current_build = nil trap("INT") { stop } end |
Instance Attribute Details
#current_build ⇒ Object (readonly)
Returns the value of attribute current_build.
36 37 38 |
# File 'lib/cijoe.rb', line 36 def current_build @current_build end |
#last_build ⇒ Object (readonly)
Returns the value of attribute last_build.
36 37 38 |
# File 'lib/cijoe.rb', line 36 def last_build @last_build end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
36 37 38 |
# File 'lib/cijoe.rb', line 36 def project @project end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
36 37 38 |
# File 'lib/cijoe.rb', line 36 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
36 37 38 |
# File 'lib/cijoe.rb', line 36 def user @user end |
Instance Method Details
#build ⇒ Object
run the build but make sure only one is running at a time
89 90 91 92 93 |
# File 'lib/cijoe.rb', line 89 def build return if building? @current_build = Build.new(@user, @project) Thread.new { build! } end |
#build! ⇒ Object
update git then run the build
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cijoe.rb', line 96 def build! out, err, status = '', '', nil git_update @current_build.sha = git_sha status = Open4.popen4(runner_command) do |pid, stdin, stdout, stderr| @pid = pid err, out = stderr.read.strip, stdout.read.strip end status.exitstatus.to_i == 0 ? build_worked(out) : build_failed(out, err) rescue Object => e build_failed('', e.to_s) end |
#build_failed(output, error) ⇒ Object
build callbacks
68 69 70 71 |
# File 'lib/cijoe.rb', line 68 def build_failed(output, error) finish_build :failed, "#{error}\n\n#{output}" run_hook "build-failed" end |
#build_worked(output) ⇒ Object
73 74 75 76 |
# File 'lib/cijoe.rb', line 73 def build_worked(output) finish_build :worked, output run_hook "build-worked" end |
#building? ⇒ Boolean
is a build running?
52 53 54 |
# File 'lib/cijoe.rb', line 52 def building? !!@current_build end |
#finish_build(status, output) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/cijoe.rb', line 78 def finish_build(status, output) @current_build.finished_at = Time.now @current_build.status = status @current_build.output = output @last_build = @current_build @current_build = nil @last_build.notify if @last_build.respond_to? :notify end |
#git_branch ⇒ Object
130 131 132 133 |
# File 'lib/cijoe.rb', line 130 def git_branch branch = Config.cijoe.branch.to_s branch == '' ? "master" : branch end |
#git_sha ⇒ Object
117 118 119 |
# File 'lib/cijoe.rb', line 117 def git_sha `git rev-parse origin/#{git_branch}`.chomp end |
#git_update ⇒ Object
121 122 123 124 |
# File 'lib/cijoe.rb', line 121 def git_update `git fetch origin && git reset --hard origin/#{git_branch}` run_hook "after-reset" end |
#git_user_and_project ⇒ Object
126 127 128 |
# File 'lib/cijoe.rb', line 126 def git_user_and_project Config.remote.origin.url.to_s.chomp('.git').split(':')[-1].split('/')[-2, 2] end |
#pid ⇒ Object
the pid of the running child process
57 58 59 |
# File 'lib/cijoe.rb', line 57 def pid building? and @pid end |
#run_hook(hook) ⇒ Object
massage our repo
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cijoe.rb', line 136 def run_hook(hook) if File.exists?(file=".git/hooks/#{hook}") && File.executable?(file) data = { "MESSAGE" => @last_build.commit., "AUTHOR" => @last_build.commit., "SHA" => @last_build.commit.sha, "OUTPUT" => @last_build.clean_output } env = data.collect { |k, v| %(#{k}=#{v.inspect}) }.join(" ") `#{env} sh #{file}` end end |
#runner_command ⇒ Object
shellin’ out
112 113 114 115 |
# File 'lib/cijoe.rb', line 112 def runner_command runner = Config.cijoe.runner.to_s runner == '' ? "rake -s test:units" : runner end |
#stop ⇒ Object
kill the child and exit
62 63 64 65 |
# File 'lib/cijoe.rb', line 62 def stop Process.kill(9, pid) if pid exit! end |