22
22
module Elastic
23
23
CURRENT_PATH = Pathname ( File . expand_path ( __dir__ ) )
24
24
class << self
25
- def download_json_spec ( version )
25
+ #
26
+ # Defines the version to download. Useful for different branches. It uses the BRANCH env
27
+ # variable, STACK_VERSION env variable or reads the version from the report GitHub Actions yaml
28
+ # file in that order.
29
+ # When using branch, it will get the latest version for that branch from snapshots.elastic.co.
30
+ # This is useful for the Elasticsearch JSON artifacts. For the elasticsearch-specification, we
31
+ # download the branch.
32
+ #
33
+ def version
34
+ if ENV [ 'BRANCH' ]
35
+ require 'open-uri'
36
+ require 'yaml'
37
+
38
+ versions = URI . open ( "https://snapshots.elastic.co/latest/#{ ENV [ 'BRANCH' ] } .json" ) . read
39
+ YAML . safe_load ( versions ) [ 'version' ]
40
+ else
41
+ ENV [ 'STACK_VERSION' ] || read_version_from_github
42
+ end
43
+ end
44
+
45
+ #
46
+ # If there's no STACK_VERSION or BRANCH specified, this function will read the version to
47
+ # download from the GitHub Actions yaml file which specifies STACK_VERSION.
48
+ #
49
+ def read_version_from_github
50
+ yml = File . read ( File . expand_path ( '../.github/workflows/report.yml' , __dir__ ) )
51
+ regexp = /[0-9.]+(-SNAPSHOT)?/
52
+ yml . split ( "\n " ) . select { |l | l . match? ( 'STACK_VERSION' ) } . first . strip . match ( regexp ) [ 0 ]
53
+ end
54
+
55
+ #
56
+ # Downloads the JSON spec from Elasticsearch.
57
+ #
58
+ def download_json_spec
26
59
json_filename = CURRENT_PATH . join ( 'tmp/artifacts.json' )
27
60
28
61
# Create ./tmp if it doesn't exist
@@ -31,7 +64,6 @@ def download_json_spec(version)
31
64
# Download json file with package information for version:
32
65
json_url = "https://artifacts-api.elastic.co/v1/versions/#{ version } "
33
66
download_file! ( json_url , json_filename )
34
-
35
67
# Parse the downloaded JSON
36
68
begin
37
69
artifacts = JSON . parse ( File . read ( json_filename ) )
@@ -62,16 +94,25 @@ def download_json_spec(version)
62
94
File . write ( CURRENT_PATH . join ( 'tmp/rest-api-spec/build_hash' ) , @build_hash )
63
95
end
64
96
65
- def download_es_specification ( branch = 'main' )
97
+ #
98
+ # Downloads the specification from github.com/elastic/elasticsearch-specification
99
+ # If a branch is specified with an env variable, it uses that, downloads `main` otherwise.
100
+ #
101
+ def download_es_specification
102
+ branch = ENV [ 'BRANCH' ] || 'main'
66
103
filename = CURRENT_PATH . join ( 'tmp/schema.json' )
104
+
67
105
url = "https://github.com/elastic/elasticsearch-specification/raw/#{ branch } /output/schema/schema.json"
68
106
download_file! ( url , filename )
69
107
end
70
108
109
+ #
110
+ # Helper function to download files
111
+ #
71
112
def download_file! ( url , filename )
72
113
puts "Downloading #{ filename } from #{ url } "
73
- File . open ( filename , "w" ) do |downloaded_file |
74
- URI . open ( url , "rb" ) do |artifact_file |
114
+ File . open ( filename , 'w' ) do |downloaded_file |
115
+ URI . open ( url , 'rb' ) do |artifact_file |
75
116
downloaded_file . write ( artifact_file . read )
76
117
end
77
118
end
0 commit comments