Skip to content

Commit f8c132b

Browse files
committed
Updates to report logic to download the right branch for report
- Adds branch to download for elasticsearch-specification. - Adds getting the version for artifacts from the snapshots url. - Fixes `8.x` branches reporting on `main`.
1 parent 3fc6d0e commit f8c132b

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

.github/workflows/report.yml

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
jobs:
1111
generate_report:
1212
runs-on: ubuntu-latest
13+
env:
14+
STACK_VERSION: 9.0.0-SNAPSHOT
15+
BRANCH: 'main' # Branch for downloading the specs
1316
strategy:
1417
matrix:
1518
branch:
@@ -21,13 +24,11 @@ jobs:
2124
- uses: actions/checkout@v4
2225
with:
2326
persist-credentials: false
24-
ref: ${{ matrix.branch }}
27+
ref: ${{ env.BRANCH }}
2528
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
2629
- uses: ruby/setup-ruby@v1
2730
with:
2831
ruby-version: 3.3
29-
env:
30-
STACK_VERSION: 9.0.0-SNAPSHOT
3132
- name: Build
3233
run: |
3334
cd report && bundle install
@@ -41,14 +42,14 @@ jobs:
4142
id: cpr
4243
with:
4344
token: ${{ secrets.GITHUB_TOKEN }}
44-
commit-message: Updates API report ${{env.REPORT_DATE}} ${{ matrix.branch }}
45-
branch: update_report_${{ matrix.branch }}
46-
title: Updates API report ${{ matrix.branch }}
45+
commit-message: Updates API report ${{env.REPORT_DATE}} ${{ env.BRANCH }}
46+
branch: update_report_${{ env.BRANCH }}
47+
title: Updates API report ${{ env.BRANCH }}
4748
body: 'As titled'
48-
base: ${{ matrix.branch }}
49+
base: ${{ env.BRANCH }}
4950
committer: 'Elastic Machine <elasticmachine@users.noreply.github.com>'
5051
author: 'Elastic Machine <elasticmachine@users.noreply.github.com>'
5152
- name: Pull Request Summary
5253
if: ${{ steps.cpr.outputs.pull-request-url }}
5354
run: |
54-
echo "${{ matrix.branch }} - ${{ steps.cpr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY
55+
echo "${{ env.BRANCH }} - ${{ steps.cpr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY

report/Rakefile

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ end
2828

2929
desc 'Download Elasticsearch Stack artifacts'
3030
task :download_json do
31-
version = ENV['STACK_VERSION'] || read_version_from_github
32-
Elastic::download_json_spec(version)
33-
end
34-
35-
def read_version_from_github
36-
yml = File.read(File.expand_path('../.github/workflows/report.yml', __dir__))
37-
regexp = /[0-9.]+(-SNAPSHOT)?/
38-
yml.split("\n").select { |l| l.match?('STACK_VERSION') }.first.strip.match(regexp)[0]
31+
Elastic::download_json_spec
3932
end
4033

4134
desc 'Download Elasticsearch Serverless artifacts'

report/download_artifacts.rb

+46-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,40 @@
2222
module Elastic
2323
CURRENT_PATH = Pathname(File.expand_path(__dir__))
2424
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
2659
json_filename = CURRENT_PATH.join('tmp/artifacts.json')
2760

2861
# Create ./tmp if it doesn't exist
@@ -31,7 +64,6 @@ def download_json_spec(version)
3164
# Download json file with package information for version:
3265
json_url = "https://artifacts-api.elastic.co/v1/versions/#{version}"
3366
download_file!(json_url, json_filename)
34-
3567
# Parse the downloaded JSON
3668
begin
3769
artifacts = JSON.parse(File.read(json_filename))
@@ -62,16 +94,25 @@ def download_json_spec(version)
6294
File.write(CURRENT_PATH.join('tmp/rest-api-spec/build_hash'), @build_hash)
6395
end
6496

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'
66103
filename = CURRENT_PATH.join('tmp/schema.json')
104+
67105
url = "https://github.com/elastic/elasticsearch-specification/raw/#{branch}/output/schema/schema.json"
68106
download_file!(url, filename)
69107
end
70108

109+
#
110+
# Helper function to download files
111+
#
71112
def download_file!(url, filename)
72113
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|
75116
downloaded_file.write(artifact_file.read)
76117
end
77118
end

0 commit comments

Comments
 (0)