Skip to content

Commit f0ce532

Browse files
committed
Implement test Slack notification
1 parent e7dbcba commit f0ce532

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

bin/git-sync-check.rb

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# This is executed by `/lib/systemd/system/git-sync-check.service` as User=git
33
# which is triggered every 10 minutes by `/lib/systemd/system/git-sync-check.timer`.
44

5+
require 'net/http'
6+
require 'uri'
7+
58
module Git
69
# cgit bare repository
710
GIT_DIR = '/var/git/ruby.git'
@@ -27,6 +30,38 @@ def git(*cmd)
2730
end
2831
end
2932

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+
3065
# Quickly finish collecting facts to avoid a race condition as much as possible.
3166
# TODO: Retry this operation several times if the race happens often.
3267
ls_remote = Git.ls_remote('github')
@@ -53,6 +88,7 @@ def git(*cmd)
5388

5489
if errors.empty?
5590
puts 'SUCCUESS: Everything is consistent.'
91+
Slack.notify('test')
5692
else
5793
puts 'FAILURE: Following inconsistencies are found.'
5894
errors.each do |remote_rev, local_rev|

0 commit comments

Comments
 (0)