Skip to content

Commit

Permalink
Merge pull request #576 from wildfly/github_action_frontmatter
Browse files Browse the repository at this point in the history
[#568] Add GitHub action to verify the front matter metadata
  • Loading branch information
bstansberry authored Jun 6, 2024
2 parents 83901aa + 346dd08 commit 1b47d85
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
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

0 comments on commit 1b47d85

Please sign in to comment.