Skip to content

Commit b21443f

Browse files
committed
[API] Generator: Adds build hash from download artifacts to generated code
1 parent c5a951c commit b21443f

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Diff for: elasticsearch-api/utils/thor/generate_source.rb

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class SourceGenerator < Thor
4747
method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
4848

4949
def generate
50+
@build_hash = File.read(File.expand_path('../../../tmp/rest-api-spec/build_hash',__dir__))
5051
self.class.source_root File.expand_path(__dir__)
5152
generate_source
5253
# -- Tree output

Diff for: elasticsearch-api/utils/thor/templates/method.erb

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
17+
#
18+
# Auto generated from build hash <%= @build_hash %>
19+
# @see https://github.com/elastic/elasticsearch/tree/main/rest-api-spec
20+
#
1821
module Elasticsearch
1922
<%= ' '*(@namespace_depth) %>module API
2023
<%- @module_namespace.each_with_index do |name, i| -%>

Diff for: rake_tasks/elasticsearch_tasks.rake

+13-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace :elasticsearch do
4343
end
4444
end
4545

46-
def package_url(filename, build_hash)
46+
def package_url(filename)
4747
begin
4848
artifacts = JSON.parse(File.read(filename))
4949
rescue StandardError => e
@@ -52,16 +52,18 @@ namespace :elasticsearch do
5252
end
5353

5454
build_hash_artifact = artifacts['version']['builds'].select do |build|
55-
build.dig('projects', 'elasticsearch', 'commit_hash') == build_hash
55+
build.dig('projects', 'elasticsearch', 'commit_hash') == @build_hash
5656
end.first
5757

5858
unless build_hash_artifact
59-
STDERR.puts "[!] Could not find artifact with build hash #{build_hash}, using latest instead"
59+
STDERR.puts "[!] Could not find artifact with build hash #{@build_hash}, using latest instead"
60+
6061
build_hash_artifact = artifacts['version']['builds'].first
62+
@build_hash = artifacts['version']['builds'].first['projects']['elasticsearch']['commit_hash']
6163
end
6264

6365
# Dig into the elasticsearch packages, search for the rest-resources-zip package and return the URL:
64-
build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k,v| k =~ /rest-resources-zip/ }.map { | _, v| v['url'] }.first
66+
build_hash_artifact.dig('projects', 'elasticsearch', 'packages').select { |k, _| k =~ /rest-resources-zip/ }.map { |_, v| v['url'] }.first
6567
end
6668

6769
def download_file!(url, filename)
@@ -73,8 +75,8 @@ namespace :elasticsearch do
7375
end
7476
puts "Successfully downloaded #{filename}"
7577

76-
unless File.exists?(filename)
77-
STDERR.puts "[!] Couldn't download #{filename}"
78+
unless File.exist?(filename)
79+
warn "[!] Couldn't download #{filename}"
7880
exit 1
7981
end
8082
rescue StandardError => e
@@ -88,8 +90,8 @@ namespace :elasticsearch do
8890
unless (version_number = args[:version] || ENV['STACK_VERSION'])
8991
# Get version number and build hash of running cluster:
9092
version_number = cluster_info['number']
91-
build_hash = cluster_info['build_hash']
92-
puts "Build hash: #{build_hash}"
93+
@build_hash = cluster_info['build_hash']
94+
puts "Build hash: #{@build_hash}"
9395
end
9496

9597
# Create ./tmp if it doesn't exist
@@ -100,7 +102,7 @@ namespace :elasticsearch do
100102
download_file!(json_url, json_filename)
101103

102104
# Get the package url from the json file given the build hash
103-
zip_url = package_url(json_filename, build_hash)
105+
zip_url = package_url(json_filename)
104106

105107
# Download the zip file
106108
filename = CURRENT_PATH.join("tmp/#{zip_url.split('/').last}")
@@ -112,6 +114,7 @@ namespace :elasticsearch do
112114
puts "Unzipping file #{filename}"
113115
`unzip -o #{filename} -d tmp/`
114116
`rm #{filename}`
115-
puts 'Artifacts downloaded in ./tmp'
117+
puts "Artifacts downloaded in ./tmp, build hash #{@build_hash}"
118+
File.write(CURRENT_PATH.join('tmp/rest-api-spec/build_hash'), @build_hash)
116119
end
117120
end

0 commit comments

Comments
 (0)