forked from tinglesoftware/dependabot-azure-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
134 lines (118 loc) · 3.7 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
# frozen_string_literal: true
require "fileutils"
require "English"
require "net/http"
require "uri"
require "json"
require "rubygems/package"
require "bundler"
require "./common/lib/dependabot"
require "yaml"
# ./dependabot-core.gemspec is purposefully excluded from this list
# because it's an empty gem as a placeholder to prevent namesquatting.
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
pub/dependabot-pub.gemspec
omnibus/dependabot-omnibus.gemspec
silent/dependabot-silent.gemspec
swift/dependabot-swift.gemspec
devcontainers/dependabot-devcontainers.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(
"omnibus/.rubocop.yml",
YAML.load_file("omnibus/.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)
return false if response.code != "200"
body = JSON.parse(response.body)
existing_versions = body.map { |b| b["number"] }
existing_versions.include?(version)
end
# rubocop:enable Metrics/BlockLength