place the file in danger/plugins
Created
December 5, 2022 09:05
-
-
Save heyhey1028/9311abf49e1c42ae5dfdf376e49d19cb to your computer and use it in GitHub Desktop.
Flutter analyze for Danger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Danger | |
class DangerFlutterAnalyze < Plugin | |
FlutterAnalyzeViolation = Struct.new(:level, :rule, :description, :file, :line) | |
attr_accessor :report_name | |
attr_accessor :report_path | |
def lint | |
report = File.open(report_path) | |
lines = report.readlines | |
unless validate_report(lines) | |
failure("[#{report_name}] 不正なファイルです ❌") | |
return | |
end | |
violations = get_violations(lines) | |
if violations.empty? | |
message("[#{report_name}] No issues found! ✅") | |
else | |
failure("[#{report_name}] #{violations.length} issues found! ❌") | |
markdown(markdown_table(violations)) | |
end | |
end | |
private | |
def validate_report(lines) | |
lines.any? { |line| line.include?("Analyzing") } | |
end | |
def markdown_table(violations) | |
table = "| Level | File | Line | Rule |\n" | |
table << "| ----- | ---- | ---- | ---- |\n" | |
return violations.reduce(table) { |acc, violation| acc << table_row(violation) } | |
end | |
def table_row(violation) | |
"| #{violation.level} | `#{violation.file}` | #{violation.line} | #{violation.rule} |\n" | |
end | |
def get_violations(lines) | |
filtered_input = filter_input(lines) | |
return [] if filtered_input.detect { |element| element.include? "No issues found!" } | |
filtered_input | |
.select { |line| line.start_with?("info", "warning", "error") } | |
.map(&method(:parse_line)) | |
end | |
def filter_input(lines) | |
lines | |
.map(&:strip) | |
.reject(&:empty?) | |
end | |
def parse_line(line) | |
level, description, file_with_line_number, rule = line.split(" • ") | |
file, line = file_with_line_number.split(":") | |
FlutterAnalyzeViolation.new(level, rule, description, file, line.to_i) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment