This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git

The following commit(s) were added to refs/heads/master by this push:
       new  45fc323   Add monitor for git pull job
45fc323 is described below

commit 45fc323b576140f69b243418ba60558cbc1e3770
Author: Sebb <[email protected]>
AuthorDate: Sat Jun 3 23:31:27 2017 +0100

    Add monitor for git pull job
---
 www/status/monitors/git.rb | 146 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 146 insertions(+)

diff --git a/www/status/monitors/git.rb b/www/status/monitors/git.rb
new file mode 100644
index 0000000..15d9b32
--- /dev/null
+++ b/www/status/monitors/git.rb
@@ -0,0 +1,146 @@
+#
+# Monitor status of git updates
+#
+=begin
+Sample input: See DATA section
+
+Output status level can be:
+Success - workspace is up to date
+Info - one or more files updated
+Warning - partial response
+Danger - unexpected text in log file
+
+=end
+
+require 'fileutils'
+
+def Monitor.git(previous_status)
+  logdir = File.expand_path('../../../logs', __FILE__)
+  log = File.join(logdir, 'git-pull')
+
+  archive = File.join(logdir, 'archive')
+  FileUtils.mkdir(archive) unless File.directory?(archive)
+
+  # read cron log
+  if __FILE__ == $0 # unit test
+    fdata = DATA.read
+  else
+    fdata = File.open(log) {|file| file.flock(File::LOCK_EX); file.read}
+  end
+
+  updates = fdata.split(%r{\n(?:/\w+)*/srv/git/})[1..-1]
+
+  status = {}
+  seen_level = {}
+
+  # extract status for each repository
+  updates.each do |update|
+    level = 'success'
+    title = nil
+    data = revision = update[/^(Already up-to-date.|Updating 
[0-9a-f]+\.\.[0-9a-f]+)$/]
+    show 'data', data
+
+    lines = update.split("\n")
+    repository = lines.shift.to_sym
+    show 'repository', repository
+
+    start_ignores = [
+      'Already ',
+      'Your branch is up-to-date with',
+      'Your branch is behind',
+      '  (use "git pull" ',
+      'Fast-forward',
+      'Updating ',
+    ]
+      
+    lines.reject! do |line| 
+      line.start_with?(*start_ignores) or
+      line =~ /^ \d+ files changed, \d+ insertions\(\+\), \d+ deletions\(-\)$/
+    end
+
+    unless lines.empty?
+      level = 'info'
+      data = lines.dup
+    end
+
+    # Drop the individual file details
+    lines.reject! {|line| line =~  /^ \S+ +\| [ \d]\d /}
+
+    show 'lines', lines
+    if lines.empty?
+      if not data
+        title = "partial response"
+        level = 'warning'
+        seen_level[level] = true
+      elsif String  === data
+        title = "No files updated"
+      elsif data.length == 1
+        title = "1 file updated"
+      else
+        title = "#{data.length} files updated"
+      end
+
+      data << revision if revision and data.instance_of? Array
+    else
+      level = 'danger'
+      data = lines.dup
+      seen_level[level] = true
+    end
+
+    status[repository] = {level: level, data: data, href: '../logs/svn-update'}
+    status[repository][:title] = title if title
+  end
+
+  # save as the highest level seen
+  %w{danger warning}.each do |lvl|
+    if seen_level[lvl]
+      # Save a copy of the log; append the severity so can track more problems
+      file = File.basename(log)
+      if __FILE__ == $0 # unit test
+        puts "Would copy log to " + File.join(archive, file + '.' + lvl)
+      else
+        FileUtils.copy log, File.join(archive, file + '.' + lvl), preserve: 
true
+      end
+      break
+    end
+  end
+  
+  {data: status}
+end
+
+private
+
+def show(name,value)
+#  $stderr.puts "#{name}='#{value.to_s}'"
+end
+
+# for debugging purposes
+if __FILE__ == $0
+  require_relative 'unit_test'
+  runtest('git') # must agree with method name above
+#  DATA.each do |l|
+#    puts l
+#  end
+end
+
+# test data
+__END__
+
+/x1/srv/git/infrastructure-puppet
+Already on 'deployment'
+Your branch is behind 'origin/deployment' by 1 commit, and can be 
fast-forwarded.
+  (use "git pull" to update your local branch)
+Updating 74bdd49..83e4220
+Fast-forward
+ data/ubuntu/1404.yaml                     |  1 +
+ data/ubuntu/1604.yaml                     |  1 +
+ modules/build_slaves/manifests/jenkins.pp | 38 
+++++++++++++++++++-------------------
+ 3 files changed, 21 insertions(+), 19 deletions(-)
+
+/x1/srv/git/infrastructure-puppet2
+Already on 'deployment'
+Your branch is up-to-date with 'origin/deployment'.
+Already up-to-date.
+
+/x1/srv/git/letsencrypt
+Already up-to-date.

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to