watchdoge 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/watchdoge/notification/gitlab_repo.rb +107 -0
- data/lib/watchdoge/notification/mattermost.rb +13 -5
- data/lib/watchdoge/notification/slack_webhook.rb +12 -4
- data/lib/watchdoge/notification.rb +32 -7
- data/lib/watchdoge/version.rb +1 -1
- data/watchdoge.gemspec +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31dedfc4a4c2e9534672ab388d89afeccb56e9855b5e14e9c454f9b4b0750670
|
4
|
+
data.tar.gz: c91210715bedf6187b55e543ff95f82a4390457bd9a4c83026c36d1ab130175b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aead7efe871204a654be431a18eacbd5eb1b22e299f0f2eb6d941022d58826fb0cb5024623dff704758558a07a2531c6d3e825409a0f5d4c74b66b5115ee29b4
|
7
|
+
data.tar.gz: e255c5c965a8a3dcaa8709a762a07127d9aa6850b3f06ecacfb67dac9accca785fd4a84e48fd53bb0988b738a27b0a29a6ca06a714f1e39dd26e3fe3201f2a00
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'net_http_ssl_fix'
|
3
|
+
|
4
|
+
require 'chunky_png'
|
5
|
+
require 'json'
|
6
|
+
require 'net/http'
|
7
|
+
require 'net/http/post/multipart'
|
8
|
+
require 'uri'
|
9
|
+
|
10
|
+
module WatchDoge
|
11
|
+
module Notification
|
12
|
+
class GitlabRepo
|
13
|
+
def initialize opt
|
14
|
+
@host = opt[:host] || ENV['CI_API_V4_URL']
|
15
|
+
@project_id = opt[:project_id] || ENV['CI_PROJECT_ID']
|
16
|
+
@source_branch = opt[:source_branch] || ENV['CI_COMMIT_REF_NAME']
|
17
|
+
|
18
|
+
@private_token = opt[:private_token]
|
19
|
+
|
20
|
+
@message_queue = []
|
21
|
+
end
|
22
|
+
|
23
|
+
def push message
|
24
|
+
@message_queue << message
|
25
|
+
end
|
26
|
+
|
27
|
+
def flush
|
28
|
+
@message_queue.each do |message|
|
29
|
+
iid = get_latest_request_iid
|
30
|
+
|
31
|
+
return if iid.nil?
|
32
|
+
|
33
|
+
case message
|
34
|
+
when String
|
35
|
+
post_discussion request_iid: iid, message: message
|
36
|
+
when ChunkyPNG::Image
|
37
|
+
image_blob = message.to_blob
|
38
|
+
res = upload_image image_blob
|
39
|
+
image_link = "![image](#{ENV['CI_PROJECT_URL']}#{res['url']})"
|
40
|
+
post_discussion request_iid: iid, message: image_link
|
41
|
+
|
42
|
+
# TODO: should add WatchDoge::Diff as pair of before/after iamges
|
43
|
+
# when WatchDoge::Diff
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def post_discussion request_iid:, message:
|
49
|
+
uri = project_uri "/merge_requests/#{request_iid}/discussions"
|
50
|
+
|
51
|
+
Net::HTTP.post uri,
|
52
|
+
{ body: message }.to_json,
|
53
|
+
"Content-Type": "application/json",
|
54
|
+
"PRIVATE-TOKEN": @private_token
|
55
|
+
end
|
56
|
+
|
57
|
+
def upload_image blob
|
58
|
+
uri = project_uri "/uploads"
|
59
|
+
|
60
|
+
req = Net::HTTP::Post::Multipart.new uri.path, {
|
61
|
+
file: UploadIO.new(StringIO.new(blob), 'image/png', 'image.png')
|
62
|
+
}
|
63
|
+
|
64
|
+
req.add_field("PRIVATE-TOKEN", @private_token)
|
65
|
+
|
66
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
|
67
|
+
res = http.request(req).body
|
68
|
+
end
|
69
|
+
|
70
|
+
JSON.parse(res)
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_latest_request_iid
|
74
|
+
uri = project_uri "/merge_requests?source_branch=#{@source_branch}&view=simple"
|
75
|
+
|
76
|
+
req = Net::HTTP::Get.new uri
|
77
|
+
req.add_field("PRIVATE-TOKEN", @private_token)
|
78
|
+
|
79
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
|
80
|
+
http.request(req).body
|
81
|
+
end
|
82
|
+
|
83
|
+
return nil if res.empty?
|
84
|
+
|
85
|
+
JSON.parse(res)[0]['iid']
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
# markdown_table do |context|
|
91
|
+
def markdown_table
|
92
|
+
"<table>
|
93
|
+
<tr>
|
94
|
+
<th>before</th>
|
95
|
+
<th>after</th>
|
96
|
+
<th>diff</th>
|
97
|
+
</tr>
|
98
|
+
#{yield("")}
|
99
|
+
</table>"
|
100
|
+
end
|
101
|
+
|
102
|
+
def project_uri path
|
103
|
+
URI(@host + "/projects/#{@project_id}#{path}")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -11,14 +11,22 @@ module WatchDoge
|
|
11
11
|
@host = args[:host]
|
12
12
|
@channel_id = args[:channel_id]
|
13
13
|
@auth_token = args[:auth_token]
|
14
|
+
|
15
|
+
@message_queue = []
|
14
16
|
end
|
15
17
|
|
16
18
|
def push message
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
@message_queue << message
|
20
|
+
end
|
21
|
+
|
22
|
+
def flush
|
23
|
+
@message_queue.each do |message|
|
24
|
+
case message
|
25
|
+
when String
|
26
|
+
post matter_most_meta(message)
|
27
|
+
when ChunkyPNG::Image
|
28
|
+
post_file matter_most_meta(message)
|
29
|
+
end
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
@@ -8,13 +8,21 @@ module WatchDoge
|
|
8
8
|
class SlackWebhook
|
9
9
|
def initialize opt
|
10
10
|
@incoming_url = opt[:incoming_url]
|
11
|
+
|
12
|
+
@message_queue = []
|
11
13
|
end
|
12
14
|
|
13
15
|
def push message
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
@message_queue << message
|
17
|
+
end
|
18
|
+
|
19
|
+
def flush
|
20
|
+
@message_queue.each do |message|
|
21
|
+
body = slack_body(message).to_json
|
22
|
+
Net::HTTP.post URI(@incoming_url),
|
23
|
+
body,
|
24
|
+
"Content-Type" => "application/json"
|
25
|
+
end
|
18
26
|
end
|
19
27
|
|
20
28
|
private
|
@@ -23,24 +23,49 @@
|
|
23
23
|
# built-in sources
|
24
24
|
require 'watchdoge/notification/slack_webhook'
|
25
25
|
require 'watchdoge/notification/mattermost'
|
26
|
+
require 'watchdoge/notification/gitlab_repo'
|
26
27
|
|
27
28
|
module WatchDoge
|
29
|
+
class RegressionError < StandardError; end
|
30
|
+
|
28
31
|
module Notification
|
29
32
|
class << self
|
30
33
|
def push message
|
31
|
-
sources
|
32
|
-
|
33
|
-
|
34
|
+
sources do |klass|
|
35
|
+
puts "new msg push to #{klass}"
|
36
|
+
klass.push message
|
37
|
+
end
|
38
|
+
end
|
34
39
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
40
|
+
def flush
|
41
|
+
sources do |klass|
|
42
|
+
puts 'flush!'
|
43
|
+
klass.flush
|
39
44
|
end
|
45
|
+
|
46
|
+
raise WatchDoge::RegressionError, 'Has detected style changed. Regression failed'
|
40
47
|
end
|
41
48
|
|
42
49
|
private
|
43
50
|
|
51
|
+
def sources
|
52
|
+
@sources ||= WatchDoge.configuration.notifications
|
53
|
+
|
54
|
+
raise "can't find notification sources in configuration" if @sources.empty?
|
55
|
+
|
56
|
+
@sources.each do |klass_sym, source_args|
|
57
|
+
sources_map[klass_sym] ||=
|
58
|
+
get_source_class(klass_sym).new source_args
|
59
|
+
|
60
|
+
yield(sources_map[klass_sym])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def sources_map
|
65
|
+
@sources_map ||= {}
|
66
|
+
@sources_map
|
67
|
+
end
|
68
|
+
|
44
69
|
def get_source_class klass_sym
|
45
70
|
klass = klass_sym.to_s.split('_').collect(&:capitalize).join
|
46
71
|
|
data/lib/watchdoge/version.rb
CHANGED
data/watchdoge.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: watchdoge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shen Lee
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/watchdoge/cookie_pool.rb
|
117
117
|
- lib/watchdoge/image_diff.rb
|
118
118
|
- lib/watchdoge/notification.rb
|
119
|
+
- lib/watchdoge/notification/gitlab_repo.rb
|
119
120
|
- lib/watchdoge/notification/mattermost.rb
|
120
121
|
- lib/watchdoge/notification/slack_webhook.rb
|
121
122
|
- lib/watchdoge/rails/generator.rb
|