diff options
author | Evan Read <eread@gitlab.com> | 2023-03-09 01:29:38 +0300 |
---|---|---|
committer | Evan Read <eread@gitlab.com> | 2023-03-09 01:29:38 +0300 |
commit | d52b32496fb98bf8b073d3484c3ccbd459b594dd (patch) | |
tree | 5f8c36727e8a299081b70680271a37f26d52df50 | |
parent | d9175c3f2e1fad68eb106256f6af756f64b05a47 (diff) | |
parent | 16cbd61ffd8649c4960420341a7b21ab0cfc19de (diff) |
Merge branch 'axil-json-schema-validator' into 'main'
Add test for navigation SCHEMA validation
Closes #1158
See merge request https://gitlab.com/gitlab-org/gitlab-docs/-/merge_requests/3628
Merged-by: Evan Read <eread@gitlab.com>
Approved-by: Evan Read <eread@gitlab.com>
Reviewed-by: Achilleas Pipinellis <axil@gitlab.com>
Reviewed-by: Ash McKenzie <amckenzie@gitlab.com>
Reviewed-by: Evan Read <eread@gitlab.com>
Reviewed-by: Mireya Andres <mandres@gitlab.com>
Co-authored-by: Achilleas Pipinellis <axil@gitlab.com>
-rw-r--r-- | .gitlab/ci/test.gitlab-ci.yml | 2 | ||||
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 10 | ||||
-rwxr-xr-x | bin/json_schemer | 27 | ||||
-rwxr-xr-x | scripts/check-navigation.sh | 17 | ||||
-rw-r--r-- | spec/lib/gitlab/navigation/navigation_schema.json | 78 |
6 files changed, 135 insertions, 0 deletions
diff --git a/.gitlab/ci/test.gitlab-ci.yml b/.gitlab/ci/test.gitlab-ci.yml index dea9dcd8..3749162b 100644 --- a/.gitlab/ci/test.gitlab-ci.yml +++ b/.gitlab/ci/test.gitlab-ci.yml @@ -36,6 +36,7 @@ check_duplicate_redirects: # # - Identical duplicate entries. # - index.html in the entries. +# - Entries that don't conform to the schema. # check_global_nav_entries: extends: @@ -43,6 +44,7 @@ check_global_nav_entries: needs: [] stage: test script: + - bundle install - make check-global-navigation # @@ -28,6 +28,7 @@ group :test, :development do gem 'pry-byebug', '~> 3.10.1', require: false gem 'gitlab-styles', '~> 10.0.0', require: false gem "webrick", "~> 1.8", ">= 1.8.1" + gem 'json_schemer', '~> 0.2', require: false end group :development, :danger do diff --git a/Gemfile.lock b/Gemfile.lock index c657de7c..72f88c86 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,8 @@ GEM debug_inspector (1.1.0) diff-lcs (1.5.0) docile (1.4.0) + ecma-re-validator (0.4.0) + regexp_parser (~> 2.2) em-websocket (0.5.3) eventmachine (>= 0.12.9) http_parser.rb (~> 0) @@ -91,6 +93,7 @@ GEM rouge (~> 4.0.0) hamster (3.0.0) concurrent-ruby (~> 1.0) + hana (1.3.7) highline (2.1.0) http_parser.rb (0.8.0) httparty (0.21.0) @@ -100,6 +103,11 @@ GEM concurrent-ruby (~> 1.0) json (2.6.3) json_schema (0.21.0) + json_schemer (0.2.24) + ecma-re-validator (~> 0.3) + hana (~> 1.3) + regexp_parser (~> 2.0) + uri_template (~> 0.7) kramdown (2.4.0) rexml kramdown-parser-gfm (1.1.0) @@ -277,6 +285,7 @@ GEM unparser (0.6.7) diff-lcs (~> 1.3) parser (>= 3.2.0) + uri_template (0.7.0) webrick (1.8.1) zeitwerk (2.6.6) @@ -289,6 +298,7 @@ DEPENDENCIES gitlab-styles (~> 10.0.0) gitlab_kramdown (~> 0.25.0) highline (~> 2.1.0) + json_schemer (~> 0.2) nanoc (~> 4.12.15) nanoc-live pry-byebug (~> 3.10.1) diff --git a/bin/json_schemer b/bin/json_schemer new file mode 100755 index 00000000..701e45ec --- /dev/null +++ b/bin/json_schemer @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'json_schemer' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("json_schemer", "json_schemer") diff --git a/scripts/check-navigation.sh b/scripts/check-navigation.sh index 9274e576..5180fa0f 100755 --- a/scripts/check-navigation.sh +++ b/scripts/check-navigation.sh @@ -36,4 +36,21 @@ else printf "${COLOR_GREEN}INFO: No entries with index.html found!${COLOR_RESET}\n" fi +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking global navigation against schema...${COLOR_RESET}\n" +JSON_NAVIGATION=$(ruby -ryaml -rjson -e "puts YAML.load_file('content/_data/navigation.yaml').to_json") +echo "$JSON_NAVIGATION" | bin/json_schemer spec/lib/gitlab/navigation/navigation_schema.json - > /dev/null +RETURN_CODE="$?" +if [[ $RETURN_CODE == 0 ]]; then + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Global navigation matches schema!${COLOR_RESET}\n" +else + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Global navigation doesn't match schema${COLOR_RESET}\n" + echo "$JSON_NAVIGATION" | bin/json_schemer spec/lib/gitlab/navigation/navigation_schema.json - > error.log + # shellcheck disable=2002 + cat error.log | while IFS= read -r error; do echo "$error" | jq; done + RETURN_CODE=1 +fi + if [[ $RETURN_CODE == 1 ]]; then exit 1; else exit 0; fi diff --git a/spec/lib/gitlab/navigation/navigation_schema.json b/spec/lib/gitlab/navigation/navigation_schema.json new file mode 100644 index 00000000..186e9eea --- /dev/null +++ b/spec/lib/gitlab/navigation/navigation_schema.json @@ -0,0 +1,78 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Global navigation for GitLab Docs", + "type": "object", + "properties": { + "sections": { + "description": "GitLab Docs site sections (first level)", + "type": "array", + "uniqueItems": true, + "items": { + "type": "object", + "properties": { + "section_title": { + "description": "GitLab Docs section (first level) title", + "type": "string" + }, + "section_url": { + "description": "GitLab Docs section (first level) URL", + "type": "string" + }, + "section_categories": { + "description": "GitLab Docs site categories (second level)", + "type": "array", + "uniqueItems": true, + "items": { + "type": "object", + "additionalProperties": false, + "properties": { + "category_title": { + "description": "GitLab Docs category (second level) title", + "type": "string" + }, + "category_url": { + "description": "GitLab Docs category (second level) URL", + "type": "string" + }, + "docs": { + "$ref": "#/definitions/docs" + } + }, + "required": ["category_title", "category_url"] + } + } + }, + "required": ["section_title", "section_url"] + } + } + }, + "required": ["sections"], + "definitions": { + "docs": { + "description": "GitLab Docs category documentation", + "type": "array", + "uniqueItems": true, + "items": { + "type": "object", + "properties": { + "doc_title": { + "description": "GitLab Docs documentation title", + "type": "string" + }, + "doc_url": { + "description": "GitLab Docs documentation URL", + "type": "string" + }, + "external_url": { + "description": "Link to a URL outside of GitLab docs", + "type": "boolean" + }, + "docs": { + "$ref": "#/definitions/docs" + } + }, + "required": ["doc_title", "doc_url"] + } + } + } +} |