forked from dependabot/dependabot-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
172 lines (148 loc) · 4.66 KB
/
Rakefile
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# frozen_string_literal: true
require "fileutils"
require "English"
require "net/http"
require "uri"
require "json"
require "shellwords"
require "rubygems/package"
require "bundler"
require "./common/lib/dependabot/version"
require "yaml"
GEMSPECS = %w(
common/dependabot-common.gemspec
go_modules/dependabot-go_modules.gemspec
terraform/dependabot-terraform.gemspec
docker/dependabot-docker.gemspec
git_submodules/dependabot-git_submodules.gemspec
github_actions/dependabot-github_actions.gemspec
nuget/dependabot-nuget.gemspec
gradle/dependabot-gradle.gemspec
maven/dependabot-maven.gemspec
bundler/dependabot-bundler.gemspec
elm/dependabot-elm.gemspec
cargo/dependabot-cargo.gemspec
npm_and_yarn/dependabot-npm_and_yarn.gemspec
composer/dependabot-composer.gemspec
hex/dependabot-hex.gemspec
python/dependabot-python.gemspec
omnibus/dependabot-omnibus.gemspec
).freeze
def run_command(command)
puts "> #{command}"
exit 1 unless system(command)
end
# rubocop:disable Metrics/BlockLength
namespace :gems do
task build: :clean do
root_path = Dir.getwd
pkg_path = File.join(root_path, "pkg")
Dir.mkdir(pkg_path) unless File.directory?(pkg_path)
GEMSPECS.each do |gemspec_path|
puts "> Building #{gemspec_path}"
Dir.chdir(File.dirname(gemspec_path)) do
gemspec = Bundler.load_gemspec_uncached(File.basename(gemspec_path))
pkg = ::Gem::Package.build(gemspec)
FileUtils.mv(pkg, File.join(pkg_path, pkg))
end
end
end
task release: [:build] do
guard_tag_match
GEMSPECS.each do |gemspec_path|
gem_name = File.basename(gemspec_path).sub(/\.gemspec$/, "")
gem_path = "pkg/#{gem_name}-#{Dependabot::VERSION}.gem"
attempts = 0
loop do
if rubygems_release_exists?(gem_name, Dependabot::VERSION)
puts "- Skipping #{gem_path} as it already exists on rubygems"
break
else
puts "> Releasing #{gem_path}"
attempts += 1
sleep(2)
begin
sh "gem push #{gem_path}"
break
rescue StandardError => e
puts "! `gem push` failed with error: #{e}"
raise if attempts >= 3
end
end
end
end
end
task :clean do
FileUtils.rm(Dir["pkg/*.gem"])
end
end
class Hash
def sort_by_key(recursive = false, &block)
keys.sort(&block).each_with_object({}) do |key, seed|
seed[key] = self[key]
seed[key] = seed[key].sort_by_key(true, &block) if recursive && seed[key].is_a?(Hash)
seed
end
end
end
namespace :rubocop do
task :sort do
File.write(
".rubocop.yml",
YAML.load_file(".rubocop.yml").sort_by_key(true).to_yaml
)
end
end
def guard_tag_match
tag = "v#{Dependabot::VERSION}"
tag_commit = `git rev-list -n 1 #{tag} 2> /dev/null`.strip
abort "Can't release - tag #{tag} does not exist" unless $CHILD_STATUS == 0
head_commit = `git rev-parse HEAD`.strip
return if tag_commit == head_commit
abort "Can't release - HEAD (#{head_commit[0..9]}) does not match " \
"tag #{tag} (#{tag_commit[0..9]})"
end
def rubygems_release_exists?(name, version)
uri = URI.parse("https://rubygems.org/api/v1/versions/#{name}.json")
response = Net::HTTP.get_response(uri)
abort "Gem #{name} doesn't exist on rubygems" if response.code != "200"
body = JSON.parse(response.body)
existing_versions = body.map { |b| b["number"] }
existing_versions.include?(version)
end
def changed_packages
all_packages = GEMSPECS.
select { |gs| gs.include?("/") }.
map { |gs| "./" + gs.split("/").first }
compare_url = ENV["CIRCLE_COMPARE_URL"]
if compare_url.nil?
warn "CIRCLE_COMPARE_URL not set, so changed packages can't be calculated"
return all_packages
end
puts "CIRCLE_COMPARE_URL: #{compare_url}"
range = compare_url.split("/").last
puts "Detected commit range '#{range}' from CIRCLE_COMPARE_URL"
unless range&.include?("..")
warn "Invalid commit range, so changed packages can't be calculated"
return all_packages
end
core_paths = %w(Dockerfile Dockerfile.ci common/lib common/bin
common/dependabot-common.gemspec)
core_changed = commit_range_changes_paths?(range, core_paths)
packages = all_packages.select do |package|
next true if core_changed
if commit_range_changes_paths?(range, [package])
puts "Commit range changes #{package}"
true
else
puts "Commit range doesn't change #{package}"
false
end
end
packages
end
def commit_range_changes_paths?(range, paths)
cmd = %w(git diff --quiet) + [range, "--"] + paths
!system(Shellwords.join(cmd))
end
# rubocop:enable Metrics/BlockLength