snakeeyes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/bin/snakeeyes +7 -0
  3. data/lib/snakeeyes.rb +170 -0
  4. metadata +69 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Scott Chacon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/snakeeyes ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'snakeeyes'
5
+
6
+ SnakeEyes.new(File.expand_path(ARGV[0])).start
7
+
data/lib/snakeeyes.rb ADDED
@@ -0,0 +1,170 @@
1
+ require 'rubygems'
2
+ require "json"
3
+ require "uri"
4
+ require 'popen4'
5
+ require "net/http"
6
+ require 'pp'
7
+
8
+ class SnakeEyes
9
+
10
+ attr_accessor :path, :sleep, :debug_level, :run_count, :master, :omaster
11
+
12
+ def initialize(path)
13
+ @path = path
14
+ @sleep = 5 * 60 # 5 minutes
15
+ @sleep = 5
16
+ @debug_level = 99
17
+ @run_count = 0
18
+ end
19
+
20
+ def start
21
+ Dir.chdir(@path) do
22
+ debug "Starting loop"
23
+ main_loop
24
+ end
25
+ end
26
+
27
+ def main_loop
28
+ while true
29
+ debug "Start #{@path}"
30
+
31
+ if has_new_commits || first_run
32
+ reset_to_newest_commit
33
+ pass, output = run_tests
34
+ report_tests(pass, output)
35
+ end
36
+
37
+ sleepy_time
38
+ @run_count += 1
39
+ end
40
+ end
41
+
42
+ def first_run
43
+ @run_count == 0
44
+ end
45
+
46
+ # if something is new, reset to it
47
+ def reset_to_newest_commit
48
+ debug "reset to newest commit (#{@omaster})"
49
+ git("reset --hard #{@omaster}")
50
+ end
51
+
52
+ def run_tests
53
+ debug "run tests"
54
+ command = git("config cijoe.runner")
55
+ debug "running '#{command}'...", 1
56
+ output = ''
57
+ status = POpen4::popen4(command) do |stdout, stderr, stdin, pid|
58
+ out = stdout.read
59
+ err = stderr.read
60
+ output = out + err
61
+ end
62
+ debug "test exitstatus : #{ status.exitstatus }", 2
63
+ [(status.exitstatus == 0), output]
64
+ end
65
+
66
+ # report the output to general hawk
67
+ def report_tests(pass, output)
68
+ status = pass ? 'good' : 'bad'
69
+ debug "reporting test results [#{status}] {#{@master}}"
70
+ data = git('log -1 --format="%s:;:%an" ' + @master)
71
+ message, author = data.split(":;:")
72
+ post_results(status, output, message, author, @master)
73
+ end
74
+
75
+ def has_new_commits
76
+ debug "check for new commits"
77
+
78
+ # look at origin/master branch
79
+ current_master = git("rev-parse origin/master")
80
+ debug "current o/master : #{current_master}", 1
81
+
82
+ debug "fetching commits", 1
83
+ git('fetch')
84
+
85
+ # look at origin/master branch again
86
+ new_master = git("rev-parse origin/master")
87
+ debug "new o/master : #{new_master}", 1
88
+
89
+ # set master branch SHA internally
90
+ @omaster = new_master
91
+
92
+ @master = git("rev-parse refs/heads/master")
93
+ debug "master : #{@master}", 1
94
+
95
+ # return true if they differ
96
+ new_master != current_master
97
+ end
98
+
99
+ def sleepy_time
100
+ debug
101
+ debug "OK, sleeping for a while (#{@sleep})..."
102
+ debug
103
+ Kernel.sleep @sleep
104
+ end
105
+
106
+ def git(command)
107
+ out = ''
108
+ status = POpen4::popen4("git #{command}") do |stdout, stderr, stdin, pid|
109
+ out = stdout.read
110
+ end
111
+ out.chomp
112
+ end
113
+
114
+ def debug(message = "", level = 0)
115
+ if level <= @debug_level
116
+ tabs = "\t" * level
117
+ puts tabs + message
118
+ end
119
+ end
120
+
121
+ ## General Hawk Stuff ##
122
+
123
+ def post_results(status, output, message, author, sha)
124
+ config = hawk_config
125
+ data = {
126
+ "agent" => config[:agent],
127
+ "description" => config[:description],
128
+ "branch" => "master",
129
+ "author" => author,
130
+ "sha" => sha,
131
+ "status" => status,
132
+ "url" => config[:url],
133
+ "message" => message,
134
+ "output" => output
135
+ }
136
+ post_update(data.to_json) # POST JSON TO URL
137
+ end
138
+
139
+ def hawk_config
140
+ return @config if @config
141
+ c = {}
142
+ config = git('config --list')
143
+ config.split("\n").each do |line|
144
+ k, v = line.split('=')
145
+ c[k] = v
146
+ end
147
+ url = ''
148
+ u = c['remote.origin.url']
149
+ if m = /github\.com.(.*?)\/(.*?)\.git/.match(u)
150
+ user = m[1]
151
+ proj = m[2]
152
+ url = "https://github.com/#{user}/#{proj}"
153
+ end
154
+
155
+ @config = {
156
+ :server => c['hawk.server'],
157
+ :token => c['hawk.token'],
158
+ :agent => c['hawk.agent'],
159
+ :description => c['hawk.description'],
160
+ :url => url
161
+ }
162
+ end
163
+
164
+ def post_update(data)
165
+ config = hawk_config
166
+ ws = "#{config[:server]}/update/#{config[:token]}"
167
+ x = Net::HTTP.post_form(URI.parse(ws), {'data' => data})
168
+ pp x
169
+ end
170
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snakeeyes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Scott Chacon
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-16 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: " snakeeyes is a polling, command line based cijoe replacement that will\n report to generalhawk.\n"
23
+ email: schacon@gmail.com
24
+ executables:
25
+ - snakeeyes
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - LICENSE
32
+ - lib/snakeeyes.rb
33
+ - bin/snakeeyes
34
+ has_rdoc: true
35
+ homepage: http://github.com/schacon/snakeeyes
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: snakeeyes reports to general hawk. hes the coolest cijoe
68
+ test_files: []
69
+