This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
146 lines (112 loc) · 4.01 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'bundler'
require 'json'
Bundler.require
require 'dependabot/file_fetchers'
require 'dependabot/pull_request_creator'
require 'dependabot/file_parsers'
require 'dependabot/update_checkers'
require 'dependabot/metadata_finders'
require 'dependabot/file_updaters'
class Lib2Pr < 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
create_pr(@data['repository'], @data['platform'], @data['name'])
status 200
body ''
end
def create_pr(repository, platform, name)
return if ENV['SKIP_PRERELEASE'] && prerelease?(platform, version)
return if satisfied_by_requirements?(requiremnts, version, platform)
send_prs(repo, platform, name, 'github')
end
def platform_to_package_manager(platform)
{
'rubygems' => 'bundler',
'npm' => 'npm_and_yarn',
'maven' => 'maven',
'pypi' => 'pip',
'packagist' => 'composer',
'hex' => 'hex'
}[platform.downcase]
end
def send_prs(repo_name, platform, name, host)
package_manager = platform_to_package_manager(platform)
github_client = Octokit::Client.new(access_token: ENV['GITHUB_TOKEN'])
credentials = [{"host" => "github.com", 'password' => ENV['GITHUB_TOKEN']}]
repo = github_client.repo(repo_name)
branch = github_client.branch(repo_name, repo.default_branch)
base_commit = branch.commit.sha
fetcher_class = Dependabot::FileFetchers.for_package_manager(package_manager)
file_updater_class = Dependabot::FileUpdaters.for_package_manager(package_manager)
update_checker_class = Dependabot::UpdateCheckers.for_package_manager(package_manager)
parser_class = Dependabot::FileParsers.for_package_manager(package_manager)
metadata_finder_class = Dependabot::MetadataFinders.for_package_manager(package_manager)
filenames = github_client.contents(repo_name).map(&:name)
unless fetcher_class.required_files_in?(filenames)
raise fetcher_class.required_files_message
end
fetcher = fetcher_class.new(source: {repo: repo_name, host: host}, credentials: credentials)
files = fetcher.files
parser = parser_class.new(dependency_files: files, repo: repo)
dependencies = parser.parse
dependencies_needing_update = []
dependencies.select{|d| d.name == name }.each do |dependency|
update_checker = update_checker_class.new(
dependency: dependency,
dependency_files: files,
credentials: credentials
)
dependency = update_checker.updated_dependencies(requirements_to_unlock: :own)
unless dependency.length.zero?
metadata_finder = metadata_finder_class.new(
dependency: dependency,
credentials: credentials
)
dependencies_needing_update << dependency
end
end
dependencies_needing_update.flatten!
dependencies_needing_update.each do |dependency|
file_updater = file_updater_class.new(
dependencies: [dependency],
dependency_files: files,
credentials: credentials
)
creator = Dependabot::PullRequestCreator.new(repo: repo_name,
base_commit: base_commit,
dependencies: [dependency],
files: file_updater.updated_dependency_files,
github_client: github_client
)
creator.create
end
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