Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#568] Add GitHub action to verify the front matter metadata #576

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/check_process.yml
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions .scripts/check_proposals
Original file line number Diff line number Diff line change
@@ -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