2
2
# This is executed by `/lib/systemd/system/git-sync-check.service` as User=git
3
3
# which is triggered every 10 minutes by `/lib/systemd/system/git-sync-check.timer`.
4
4
5
+ require 'net/http'
6
+ require 'uri'
7
+
5
8
module Git
6
9
# cgit bare repository
7
10
GIT_DIR = '/var/git/ruby.git'
@@ -27,6 +30,38 @@ def git(*cmd)
27
30
end
28
31
end
29
32
33
+ module Slack
34
+ WEBHOOK_URL = File . read ( File . expand_path ( '~git/config/slack-webhook-alerts' ) )
35
+
36
+ class << self
37
+ def notify ( message )
38
+ attachment = {
39
+ title : 'ruby/ruby-commit-hook - bin/git-sync-check.rb' ,
40
+ title_link : 'https://github.com/ruby/ruby-commit-hook/blob/master/bin/git-sync-check.rb' ,
41
+ text : message ,
42
+ color : 'danger' ,
43
+ }
44
+
45
+ payload = { attachments : [ attachment ] }
46
+ resp = post ( WEBHOOK_URL , payload : payload )
47
+ puts "#{ resp . code } (#{ resp . body } ) -- #{ payload . to_json } "
48
+ end
49
+
50
+ private
51
+
52
+ def post ( url , payload :)
53
+ uri = URI . parse ( url )
54
+ http = Net ::HTTP . new ( uri . host , uri . port )
55
+ http . use_ssl = ( uri . scheme == 'https' )
56
+ http . start do
57
+ req = Net ::HTTP ::Post . new ( uri . path )
58
+ req . set_form_data ( payload : payload . to_json )
59
+ http . request ( req )
60
+ end
61
+ end
62
+ end
63
+ end
64
+
30
65
# Quickly finish collecting facts to avoid a race condition as much as possible.
31
66
# TODO: Retry this operation several times if the race happens often.
32
67
ls_remote = Git . ls_remote ( 'github' )
@@ -53,6 +88,7 @@ def git(*cmd)
53
88
54
89
if errors . empty?
55
90
puts 'SUCCUESS: Everything is consistent.'
91
+ Slack . notify ( 'test' )
56
92
else
57
93
puts 'FAILURE: Following inconsistencies are found.'
58
94
errors . each do |remote_rev , local_rev |
0 commit comments