This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
62 lines (50 loc) · 1.59 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'bundler'
require 'json'
Bundler.require
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
class Lib2Slack < Sinatra::Base
use Rack::Deflater
configure do
set :logging, true
set :dump_errors, false
set :raise_errors, true
set :show_exceptions, false
end
before do
request.body.rewind
@data = JSON.parse request.body.read
end
post '/webhook' do
content_type :json
post_to_slack(@data['repository'], @data['platform'], @data['name'], @data['version'], @data['requirements'])
status 200
body ''
end
def post_to_slack(repository, platform, name, version, requiremnts)
return if ENV['SKIP_PRERELEASE'] && prerelease?(platform, version)
return if satisfied_by_requirements?(requiremnts, version, platform)
text = "There's a newer version of #{name} that #{repository} depends on.
More info: https://libraries.io/#{platform.downcase}/#{name}/#{version}"
client = Slack::Web::Client.new
client.chat_postMessage(channel: ENV['SLACK_CHANNEL'], text: text, as_user: true)
end
def satisfied_by_requirements?(requiremnts, version, platform = nil)
return false if requiremnts.nil? || requiremnts.empty?
requiremnts.none? do |requirement|
SemanticRange.gtr(version, requirement, false, platform)
end
rescue
false
end
def prerelease?(platform, version)
parsed_version = SemanticRange.parse(version) rescue nil
return true if parsed_version && parsed_version.prerelease.length > 0
if platform.downcase == 'rubygems'
!!(version =~ /[a-zA-Z]/)
else
false
end
end
end