debian-mirror-gitlab/scripts/lib/glfm/render_static_html.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
3.1 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'spec_helper'
2022-07-16 23:28:13 +05:30
require_relative 'constants'
require_relative 'shared'
# Purpose:
# - Reads a set of markdown examples from a hash which has been serialized to disk
2022-10-11 01:57:18 +05:30
# - Sets up the appropriate fixture data for the markdown examples
# - Converts each example to static HTML using the appropriate API markdown endpoint
2022-07-16 23:28:13 +05:30
# - Writes the HTML for each example to a hash which is serialized to disk
#
2022-10-11 01:57:18 +05:30
# Requirements:
# The input and output files are specified via these environment variables:
# - INPUT_MARKDOWN_YML_PATH
# - OUTPUT_STATIC_HTML_TEMPFILE_PATH
#
# Although it is implemented as an RSpec test, it is not a unit test. We use
# RSpec because that is the simplest environment in which we can use the
# Factorybot factory methods to create persisted model objects with stable
# and consistent data values, to ensure consistent example snapshot HTML
# across various machines and environments. RSpec also makes it easy to invoke
2022-11-25 23:54:43 +05:30
# the API and obtain the response.
2022-10-11 01:57:18 +05:30
#
# It is intended to be invoked as a helper subprocess from the `update_example_snapshots.rb`
2023-03-04 22:38:38 +05:30
# and `update_specification.rb` script classes. It's not intended to be run or used directly.
# This usage is also reinforced by not naming the file with a `_spec.rb` ending.
2022-11-25 23:54:43 +05:30
RSpec.describe 'Render Static HTML', :api, type: :request do
2022-10-11 01:57:18 +05:30
include Glfm::Constants
include Glfm::Shared
2022-07-16 23:28:13 +05:30
2022-10-11 01:57:18 +05:30
# noinspection RailsParamDefResolve (RubyMine can't find the shared context from this file location)
include_context 'with GLFM example snapshot fixtures'
2022-07-23 23:45:48 +05:30
2022-11-25 23:54:43 +05:30
it do
2022-10-11 01:57:18 +05:30
markdown_hash = YAML.safe_load(File.open(ENV.fetch('INPUT_MARKDOWN_YML_PATH')), symbolize_names: true)
2022-11-25 23:54:43 +05:30
metadata_hash =
if input_metadata_yml_path = ENV['INPUT_METADATA_YML_PATH']
YAML.safe_load(File.open(input_metadata_yml_path), symbolize_names: true) || {}
else
{}
end
2022-07-16 23:28:13 +05:30
2022-10-11 01:57:18 +05:30
# NOTE: We cannot parallelize this loop like the Javascript WYSIWYG example generation does,
# because the rspec `post` API cannot be parallized (it is not thread-safe, it can't find
# the controller).
static_html_hash = markdown_hash.transform_values.with_index do |markdown, index|
name = markdown_hash.keys[index]
api_url = metadata_hash.dig(name, :api_request_override_path) || (api "/markdown")
2022-07-16 23:28:13 +05:30
2022-10-11 01:57:18 +05:30
post api_url, params: { text: markdown, gfm: true }
# noinspection RubyResolve
expect(response).to be_successful
2022-07-16 23:28:13 +05:30
2022-10-11 01:57:18 +05:30
returned_html_value =
begin
parsed_response = Gitlab::Json.parse(response.body, symbolize_names: true)
# Some responses have the HTML in the `html` key, others in the `body` key.
parsed_response[:body] || parsed_response[:html]
rescue JSON::ParserError
# if we got a parsing error, just return the raw response body for debugging purposes.
response.body
end
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
returned_html_value
end
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
write_output_file(static_html_hash)
end
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
private
2022-07-23 23:45:48 +05:30
2022-10-11 01:57:18 +05:30
def write_output_file(static_html_hash)
yaml_string = dump_yaml_with_formatting(static_html_hash)
2022-11-25 23:54:43 +05:30
write_file(ENV.fetch('OUTPUT_STATIC_HTML_TEMPFILE_PATH'), yaml_string)
2022-07-23 23:45:48 +05:30
end
2022-07-16 23:28:13 +05:30
end