lib/jacoco/plugin.rb in danger-jacoco-0.1.3 vs lib/jacoco/plugin.rb in danger-jacoco-0.1.4

- old
+ new

@@ -28,10 +28,11 @@ end # This is a fast report based on SAX parser # # @path path to the xml output of jacoco + # @report_url URL where html report hosted # @delimiter git.modified_files returns full paths to the # changed files. We need to get the class from this path to check the # Jacoco report, # # e.g. src/java/com/example/SomeJavaClass.java -> com/example/SomeJavaClass @@ -41,23 +42,23 @@ # that is your path to source files is something like # # Java => blah/blah/java/slashed_package/Source.java # Kotlin => blah/blah/kotlin/slashed_package/Source.kt # - def report(path, delimiter = %r{\/java\/|\/kotlin\/}) + def report(path, report_url = '', delimiter = %r{\/java\/|\/kotlin\/}) setup classes = classes(delimiter) parser = Jacoco::SAXParser.new(classes) Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(path)) total_covered = total_coverage(path) report_markdown = "### JaCoCO Code Coverage #{total_covered[:covered]}% #{total_covered[:status]}\n" report_markdown << "| Class | Covered | Meta | Status |\n" - report_markdown << "|:---:|:---:|:---:|:---:|\n" - class_coverage_above_minimum = markdown_class(parser, report_markdown) + report_markdown << "|:---|:---:|:---:|:---:|\n" + class_coverage_above_minimum = markdown_class(parser, report_markdown, report_url) markdown(report_markdown) report_fails(class_coverage_above_minimum, total_covered) end @@ -133,19 +134,30 @@ fail("Class coverage is below minimum. Improve to at least #{minimum_class_coverage_percentage}%") end # rubocop:enable Style/SignalException - def markdown_class(parser, report_markdown) + def markdown_class(parser, report_markdown, report_url) class_coverage_above_minimum = true parser.classes.each do |jacoco_class| # Check metrics for each classes rp = report_class(jacoco_class) - ln = "| `#{jacoco_class.name}` | #{rp[:covered]}% | #{rp[:required_coverage_percentage]}% | #{rp[:status]} |\n" + rl = report_link(jacoco_class.name, report_url) + ln = "| #{rl} | #{rp[:covered]}% | #{rp[:required_coverage_percentage]}% | #{rp[:status]} |\n" report_markdown << ln class_coverage_above_minimum &&= rp[:covered] >= rp[:required_coverage_percentage] end class_coverage_above_minimum end + + def report_link(class_name, report_url) + if report_url.empty? + "`#{class_name}`" + else + report_filepath = class_name.gsub(/\/(?=[^\/]*\/.)/, '.') + ".html" + "[`#{class_name}`](#{report_url + report_filepath})" + end + end + end end