Class: SnakeEyes

Inherits:
Object
  • Object
show all
Defined in:
lib/snakeeyes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SnakeEyes

Returns a new instance of SnakeEyes.



12
13
14
15
16
17
# File 'lib/snakeeyes.rb', line 12

def initialize(path)
  @path = path
  @sleep = 5 * 60 # 5 minutes
  @debug_level = 99
  @run_count = 0
end

Instance Attribute Details

#debug_levelObject

Returns the value of attribute debug_level.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def debug_level
  @debug_level
end

#masterObject

Returns the value of attribute master.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def master
  @master
end

#omasterObject

Returns the value of attribute omaster.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def omaster
  @omaster
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def path
  @path
end

#run_countObject

Returns the value of attribute run_count.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def run_count
  @run_count
end

#sleepObject

Returns the value of attribute sleep.



10
11
12
# File 'lib/snakeeyes.rb', line 10

def sleep
  @sleep
end

Instance Method Details

#debug(message = "", level = 0) ⇒ Object



113
114
115
116
117
118
# File 'lib/snakeeyes.rb', line 113

def debug(message = "", level = 0)
  if level <= @debug_level
    tabs = "\t" * level
    puts tabs + message 
  end
end

#first_runObject



41
42
43
# File 'lib/snakeeyes.rb', line 41

def first_run
  @run_count == 0
end

#git(command) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/snakeeyes.rb', line 105

def git(command)
  out = ''
  status = POpen4::popen4("git #{command}") do |stdout, stderr, stdin, pid|
    out = stdout.read
  end
  out.chomp
end

#has_new_commitsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/snakeeyes.rb', line 74

def has_new_commits
  debug "check for new commits"

  # look at origin/master branch
  current_master = git("rev-parse origin/master")
  debug "current o/master : #{current_master}", 1

  debug "fetching commits", 1
  git('fetch')

  # look at origin/master branch again
  new_master = git("rev-parse origin/master")
  debug "new o/master     : #{new_master}", 1

  # set master branch SHA internally
  @omaster = new_master

  @master = git("rev-parse refs/heads/master")
  debug "master           : #{@master}", 1

  # return true if they differ
  new_master != current_master
end

#hawk_configObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/snakeeyes.rb', line 138

def hawk_config
  return @config if @config
  c = {}
  config = git('config --list')
  config.split("\n").each do |line| 
    k, v = line.split('=')
    c[k] = v
  end
  url = ''
  u = c['remote.origin.url']
  if m = /github\.com.(.*?)\/(.*?)\.git/.match(u)
    user = m[1]
    proj = m[2]
    url = "https://github.com/#{user}/#{proj}"
  end

  @config = {
    :server    => c['hawk.server'],
    :token     => c['hawk.token'],
    :agent     => c['hawk.agent'],
    :description => c['hawk.description'],
    :url => url
  }
end

#main_loopObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/snakeeyes.rb', line 26

def main_loop
  while true
    debug "Start #{@path}"

    if has_new_commits || first_run
      reset_to_newest_commit
      pass, output = run_tests
      report_tests(pass, output)
    end

    sleepy_time
    @run_count += 1
  end
end

#post_results(status, output, message, author, sha) ⇒ Object

General Hawk Stuff ##



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/snakeeyes.rb', line 122

def post_results(status, output, message, author, sha)
  config = hawk_config
  data = {
      "agent"       => config[:agent],
      "description" => config[:description],
      "branch"      => "master",
      "author"      => author,
      "sha"         => sha,
      "status"      => status,
      "url"         => config[:url],
      "message"     => message,
      "output"      => output
    }
  post_update(data.to_json) # POST JSON TO URL
end

#post_update(data) ⇒ Object



163
164
165
166
167
168
# File 'lib/snakeeyes.rb', line 163

def post_update(data)
  config = hawk_config
  ws = "#{config[:server]}/update/#{config[:token]}"
  x = Net::HTTP.post_form(URI.parse(ws), {'data' => data})
  pp x
end

#report_tests(pass, output) ⇒ Object

report the output to general hawk



66
67
68
69
70
71
72
# File 'lib/snakeeyes.rb', line 66

def report_tests(pass, output)
  status = pass ? 'good' : 'bad'
  debug "reporting test results [#{status}] {#{@master}}"
  data = git('log -1 --format="%s:;:%an" ' + @master)
  message, author = data.split(":;:")
  post_results(status, output, message, author, @master)
end

#reset_to_newest_commitObject

if something is new, reset to it



46
47
48
49
# File 'lib/snakeeyes.rb', line 46

def reset_to_newest_commit
  debug "reset to newest commit (#{@omaster})"
  git("reset --hard #{@omaster}")
end

#run_testsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/snakeeyes.rb', line 51

def run_tests
  debug "run tests"
  command = git("config cijoe.runner")
  debug "running '#{command}'...", 1
  output = ''
  status = POpen4::popen4(command) do |stdout, stderr, stdin, pid|
    out = stdout.read
    err = stderr.read
    output = out + err
  end
  debug "test exitstatus : #{ status.exitstatus }", 2
  [(status.exitstatus == 0), output]
end

#sleepy_timeObject



98
99
100
101
102
103
# File 'lib/snakeeyes.rb', line 98

def sleepy_time
  debug
  debug "OK, sleeping for a while (#{@sleep})..."
  debug
  Kernel.sleep @sleep
end

#startObject



19
20
21
22
23
24
# File 'lib/snakeeyes.rb', line 19

def start
  Dir.chdir(@path) do
    debug "Starting loop"
    main_loop
  end
end