Skip to content

Instantly share code, notes, and snippets.

@kateinoigakukun
Created April 24, 2022 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateinoigakukun/821b9643443f75f2712899afec595c0b to your computer and use it in GitHub Desktop.
Save kateinoigakukun/821b9643443f75f2712899afec595c0b to your computer and use it in GitHub Desktop.
require "net/http"
require "rss"
require "json"
class JenkinsFeed
def self.fetch_all(source)
uri = URI.parse(source)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get(uri)
JenkinsFeed.new(RSS::Parser.parse(response.body))
end
def self.status(feed)
/.+ \((.+)\)$/.match(feed.title.content)[1]
end
def initialize(rss)
@rss = rss
@feeds = rss.items.sort_by{ _1.updated.content }
end
def newer_feeds(latest_feed)
@feeds.select do |feed|
latest_feed.nil? or feed.updated.content > latest_feed.updated.content
end
end
def latest_feed
@feeds.last
end
end
module Discord
WEBHOOK_URL = ENV["SWIFTWASM_STATUS_CI_WEBHOOK_URL"] || abort("missing SWIFTWASM_STATUS_CI_WEBHOOK_URL")
class << self
def notify(feed, color:)
payload = {
content: nil,
embeds: [
{
title: feed.title.content,
url: feed.link.href,
color: color,
author: {
name: "Swift Community CI",
url: "https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-20.04-webassembly/",
icon_url: "https://github.com/swift-ci.png"
}
}
],
attachments: []
}
post(WEBHOOK_URL, payload: payload)
end
def post(url, payload:)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
http.start do
headers = { "Content-Type" => "application/json" }
response = http.post(uri.path, payload.to_json, headers)
unless response.is_a?(Net::HTTPSuccess)
warn "post failed: #{response}"
end
end
end
end
end
def main
latest_feed = nil
loop do
rss = JenkinsFeed.fetch_all("https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-20.04-webassembly/rssAll")
new_feeds = if latest_feed
rss.newer_feeds(latest_feed)
elsif rss.latest_feed
[rss.latest_feed]
else
[]
end
new_feeds.each do |feed|
status = JenkinsFeed.status(feed)
if status.start_with?("broken since build")
Discord.notify(feed, color: 0xf84740)
elsif status.start_with?("back to normal")
Discord.notify(feed, color: 0x38b046)
end
end
unless new_feeds.empty?
latest_feed = new_feeds.last
end
Kernel.sleep(60)
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment