diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2022-07-30 00:51:14 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2022-07-30 01:12:13 +0900 |
commit | c0e318cd5cb4f7a2362f5606cd9f6f10fa2f2a4e (patch) | |
tree | 1ccc57805bd49e6191c574429f965c824fb03e7c | |
parent | b6344c42dc3138367f6c468c0f9525211242aac7 (diff) | |
download | nyaci-master.tar.gz |
-rw-r--r-- | config.rb.example | 3 | ||||
-rw-r--r-- | notification.rb | 44 |
2 files changed, 46 insertions, 1 deletions
diff --git a/config.rb.example b/config.rb.example index 02bb38e..7b2cf43 100644 --- a/config.rb.example +++ b/config.rb.example @@ -12,6 +12,9 @@ NyaConfig.configure( slack: { webhook: "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX" }, + discord: { + webhook: "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + }, }, modules: { ruby: { diff --git a/notification.rb b/notification.rb index 56d25a8..412adaf 100644 --- a/notification.rb +++ b/notification.rb @@ -5,10 +5,11 @@ require "net/http" require "json" class Notification - def initialize(email: nil, slack: nil) + def initialize(email: nil, slack: nil, discord: nil) @list = [] @list << Email.new(email) if email @list << Slack.new(slack) if slack + @list << Discord.new(discord) if discord end def publish(*args) @@ -107,4 +108,45 @@ class Notification raise "failed to post to Slack: #{body}" if body != "ok" end end + + class Discord + def initialize(webhook:) + @webhook = webhook + end + + def publish(project, jobid, results, elapsed) + failed = results.count { |id, result| !result } + + link = NyaConfig.webroot + "/" + project + "/" + jobid + ary = [] + if failed != 0 + ary << "failed builds:" + results.select { |id, r| !r }.map { |id, r| id }.sort.each { |i| + alink = NyaConfig.webroot + "/" + project + "/" + jobid + "/" + i + ai = i.gsub(/[_~*|`]/, "\\\\\\0") + ary << "- [#{ai}](#{alink})" + } + end + + payload = JSON.generate(embeds: [{ + color: failed == 0 ? 1673044 : 14431557, + title: "nyaci #{failed == 0 ? "passed" : "failed"} - #{project}/#{jobid}", + url: link, + description: ary.join("\n"), + footer: { + text: "#{project} • Finished in #{elapsed} seconds", + }, + timestamp: Time.now.iso8601, + }]) + + uri = URI.parse(@webhook) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = uri.scheme == "https" + body = http.start { + res = http.post(uri.path, payload, "Content-Type" => "application/json") + res.body + } + raise "failed to post to Discord: #{body}" if body != "ok" + end + end end |