Skip to content

Commit 0d664eb

Browse files
committed
Adds elasticsearch-specification to downloads and report
1 parent f1f27b9 commit 0d664eb

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

report/Rakefile

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ task :report do
99
File.write('../apis_report.md', template.result(binding))
1010
end
1111

12-
desc "Download artifacts"
13-
task :download do
14-
Elastic::download_artifacts(ENV['STACK_VERSION'] || '8.13.0-SNAPSHOT')
12+
desc "Download Elasticsearch Stack artifacts"
13+
task :download_json do
14+
Elastic::download_json_spec(ENV['STACK_VERSION'] || '8.13.0-SNAPSHOT')
15+
end
16+
17+
desc "Download Elasticsearch Serverless artifacts"
18+
task :download_spec do
19+
Elastic::download_es_specification
1520
end

report/download_artifacts.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Elastic
66
CURRENT_PATH = Pathname(File.expand_path(__dir__))
77
class << self
8-
def download_artifacts(version)
8+
def download_json_spec(version)
99
json_filename = CURRENT_PATH.join('tmp/artifacts.json')
1010

1111
# Create ./tmp if it doesn't exist
@@ -45,6 +45,12 @@ def download_artifacts(version)
4545
File.write(CURRENT_PATH.join('tmp/rest-api-spec/build_hash'), @build_hash)
4646
end
4747

48+
def download_es_specification(branch = 'main')
49+
filename = CURRENT_PATH.join('tmp/schema.json')
50+
url = "https://github.com/elastic/elasticsearch-specification/raw/#{branch}/output/schema/schema.json"
51+
download_file!(url, filename)
52+
end
53+
4854
def download_file!(url, filename)
4955
puts "Downloading #{filename} from #{url}"
5056
File.open(filename, "w") do |downloaded_file|

report/reporter.rb

+53-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
module Elastic
22
class Reporter
3-
JSON_FILES = "#{File.expand_path('./tmp/rest-api-spec/api')}/*.json".freeze
3+
STACK_FILES = "#{File.expand_path('./tmp/rest-api-spec/api')}/*.json".freeze
44
TESTS_PATH = File.expand_path('../tests/**/*.yml')
55

66
attr_reader :apis, :tested, :untested
77

88
def initialize
9-
@apis = gather_apis
9+
@apis = {}
10+
@apis[:specification] = specification_apis
11+
@apis[:json] = json_apis
1012
@tested = []
1113
@untested = []
1214
report!
1315
end
1416

15-
def gather_apis
16-
endpoints = Dir[JSON_FILES].map do |path|
17-
path.split('/').last.gsub('.json','')
18-
end.reject { |a| a.split('/').last.gsub('.json','').start_with?('_') }
17+
# Stack APIs are obtained from the Elasticsearch Rest JSON specification.
18+
# Use `rake download_stack` to download the spec files to ../tmp.
19+
#
20+
def json_apis
21+
apis = Dir[STACK_FILES].map { |path| path.split('/').last.gsub('.json','') }
22+
reject_internal(apis)
23+
end
24+
25+
# Serverless APIs are obtained from elastic/elasticsearch-specification.
26+
# Use `rake download_serverless` to download the files to ../tmp.
27+
def specification_apis
28+
apis = JSON.parse(File.read('./tmp/schema.json'))['endpoints'].map do |s|
29+
{ 'name' => s['name'], 'availability' => s['availability'] }
30+
end
31+
reject_internal(apis)
32+
end
33+
34+
def serverless_apis
35+
# The absence of an 'availability' field on a property implies that the property is
36+
# available in all flavors.
37+
@apis[:specification].select do |api|
38+
api.dig('availability').nil? ||
39+
(
40+
!!api.dig('availability', 'serverless') &&
41+
api.dig('availability', 'serverless', 'visibility') == 'public'
42+
)
43+
end
1944
end
2045

2146
def report!
22-
@apis.each do |api|
47+
@apis[:json].each do |api|
2348
if (test = find_test(api))
2449
@tested << test
2550
else
@@ -28,12 +53,32 @@ def report!
2853
end
2954
end
3055

56+
def coverage
57+
percentage = @tested.count * 100 / @apis[:json].count
58+
"![](https://geps.dev/progress/#{percentage})"
59+
end
60+
61+
private
62+
3163
def find_test(endpoint)
3264
Dir[TESTS_PATH].map do |path|
3365
relative_path = path[path.index('/tests')..-1]
34-
return { endpoint: endpoint, file: ".#{relative_path}" } if File.readlines(path).grep(/#{endpoint}/).any?
66+
67+
if File.readlines(path).grep(/#{endpoint}/).any?
68+
return { endpoint: endpoint, file: ".#{relative_path}" }
69+
end
3570
end
3671
false
3772
end
73+
74+
def reject_internal(apis)
75+
apis.reject! do |api|
76+
if api.is_a?(Hash)
77+
api.dig('name').start_with?('_')
78+
elsif api.is_a?(String)
79+
api.start_with?('_')
80+
end
81+
end
82+
end
3883
end
3984
end

report/template.erb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Tests report
22

3-
Endpoints: <%= @reporter.apis.count %>
3+
Endpoints in JSON spec: <%= @reporter.apis[:json].count %>
4+
Endpoints in elasticsearch-specification: <%= @reporter.apis[:specification].count %>
5+
Endpoints in Serverless: <%= @reporter.serverless_apis.count %>
46
[Tested](#tested): <%= @reporter.tested.count %>
57
[Untested](#untested): <%= @reporter.untested.count %>
8+
<%= @reporter.coverage %>
69

710
## Tested
811

0 commit comments

Comments
 (0)