Skip to content

Commit

Permalink
Add GitHub actions for validating regular expressions (matomo-org#6991)
Browse files Browse the repository at this point in the history
* Add GitHub actions for validating regular expressions

* Update .github/workflows/regular_expressions.yml

Co-authored-by: Stefan Giehl <[email protected]>
  • Loading branch information
nickcampbell18 and sgiehl authored Feb 15, 2022
1 parent 6980b29 commit 46a45dc
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .github/workflows/regular_expressions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Validate regular Expressions

on:
pull_request:
push:
branches: [ master ]

permissions:
contents: read

jobs:
regex:
name: Validate regular expressions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/[email protected]
with:
ruby-version: '3.1'
bundler-cache: true
- name: Validate regular expressions
run: |
cat <<- EOF > validate-regular-expressions.rb
require "yaml"
RegexSyntaxError = Class.new(StandardError)
module Warning
extend self
def warn(message)
raise RegexSyntaxError, message.gsub(/\A.+\.rb:\d+: warning: /, '').gsub(/\n$/, "")
end
end
def find_regexes(object)
result = []
object.each { |key, value|
if key == "regex"
result.push(value)
else
if key.is_a?(Hash) || key.is_a?(Array)
result.push(*find_regexes(key))
end
if value.is_a?(Hash) || value.is_a?(Array)
result.push(*find_regexes(value))
end
end
}
return result
end
success = true
Dir["regexes/**/*.yml"].each do |file|
data = YAML.load_file(file)
print "Checking file #{file}... "
warnings = []
regexes = 0
find_regexes(data).each do |regex_string|
regexes += 1
Regexp.new(regex_string, Regexp::IGNORECASE)
rescue RegexSyntaxError => e
success = false
warnings << "'#{regex_string}', warning: #{e.message}"
end
if warnings.any?
print "err\n\n"
warnings.each { puts " " + _1 }
puts "\n"
else
print "found #{regexes} regular expressions, all ok\n"
end
rescue
print "could not find regular expressions\n"
end
success || exit(1)
EOF
ruby validate-regular-expressions.rb

0 comments on commit 46a45dc

Please sign in to comment.