From 346dd085fa4c20f4a1e0b18687896bfa8feec0cc Mon Sep 17 00:00:00 2001 From: Jeff Mesnil Date: Mon, 3 Jun 2024 09:37:34 +0200 Subject: [PATCH] [#568] Add GitHub action to verify the front matter metadata This fixes #568 Signed-off-by: Jeff Mesnil --- .github/workflows/check_process.yml | 43 +++++++++++++ .scripts/check_proposals | 96 +++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 .github/workflows/check_process.yml create mode 100755 .scripts/check_proposals diff --git a/.github/workflows/check_process.yml b/.github/workflows/check_process.yml new file mode 100644 index 00000000..0d49063e --- /dev/null +++ b/.github/workflows/check_process.yml @@ -0,0 +1,43 @@ +name: Check Proposals + +on: + pull_request_target: + +jobs: + check-proposals: + runs-on: ubuntu-latest + + permissions: + contents: read # to download a workflow artifact + pull-requests: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: "refs/pull/${{ github.event.number }}/merge" + - name: Setup Ruby + uses: ruby/setup-ruby@8575951200e472d5f2d95c625da0c7bec8217c42 # v1.161.0 + with: + ruby-version: '3.1' # Not needed with a .ruby-version file + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + cache-version: 0 # Increment this number if you need to re-download cached gems + - name: Get all proposals files that have changed + id: changed-proposals + uses: tj-actions/changed-files@v44 + with: + files: | + **/*.adoc + files_ignore: | + _*/** + index.adoc + design-doc-template.adoc + - name: Check files that have changed + if: steps.changed-proposals.outputs.any_changed == 'true' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PROPOSAL_FILES: ${{ steps.changed-proposals.outputs.all_changed_files }} + PR_NUMBER: ${{ github.event.number }} + run: | + echo "Check all proposals files that have changed: $PROPOSAL_FILES" + ./.scripts/check_proposals $PR_NUMBER $PROPOSAL_FILES \ No newline at end of file diff --git a/.scripts/check_proposals b/.scripts/check_proposals new file mode 100755 index 00000000..594ab165 --- /dev/null +++ b/.scripts/check_proposals @@ -0,0 +1,96 @@ +#!/usr/bin/env ruby + +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +require 'bundler/setup' +require 'fileutils' +require 'yaml' + +class FrontMatterExtractor + def process lines + return {} if lines.empty? + + front_matter = [] + if lines.first.chomp == '---' + original_lines = lines.dup + lines.shift + while !lines.empty? && lines.first.chomp != '---' + front_matter << lines.shift + end + + YAML.load(front_matter.join("\n")) + end + end +end + +class MetadataChecker + def initialize + @categories = load_all_categories() + end + + def check pr_number, front_matter + STDERR.puts "Check front matter #{front_matter}" + errors = [] + categories_errors = check_categories(pr_number, front_matter) + if categories_errors.length() > 0 + errors << categories_errors + system("gh pr edit #{pr_number} --add-label invalid-categories") + else + system("gh pr edit #{pr_number} --remove-label invalid-categories") + end + errors + end + + def check_categories pr_number, front_matter + errors = [] + unless (front_matter.key? "categories") && (front_matter["categories"] != nil) && (front_matter["categories"].length() > 0) + errors << "Missing field: categories" + else + front_matter["categories"].each do | cat_id | + if @categories.find { | global_cat | cat_id == global_cat["id"] } == nil + errors << "New category #{cat_id} must be added to _data/wildfly-categories.yaml" + end + end + end + errors + end + + def load_all_categories + yaml = YAML.load_file("_data/wildfly-categories.yaml") + return yaml["categories"] + end +end + +front_matter_extractor = FrontMatterExtractor.new +metadata_checker = MetadataChecker.new + +check_succeeds = true + +pr_number = ARGV[0] +ARGV.shift + +puts "Checking PR #{pr_number}" + +# Read and print the content of each file passed as arguments +ARGV.each do |file| + + begin + content = File.read(file) + front_matter = front_matter_extractor.process content.split("\n") + errors = metadata_checker.check pr_number, front_matter + if errors.length() > 0 + check_succeeds = false + puts "❌ #{file}" + puts "\tFront matter is not valid: #{front_matter}" + puts "\tGot errors:" + errors.each do |err| + puts "\t* #{err}" + end + else + puts "✅ #{file}" + end + rescue => e + puts "Error reading file #{file}: #{e.message}" + end +end + +exit check_succeeds \ No newline at end of file