Danger-kotlin plugin to parse and report Detekt violations

Overview

Maven Central

Detekt Plugin for Danger Kotlin

Plugin for danger/kotlin which helps to parse and report detekt violations from its XML report files.

How it looks like

Depends on you. We constrained only with danger/kotlin file and line appearance, and repository hosting (e.g. GitHub) markdown capabilities. Anything else is customizable.

Like this

With inline comments

Or this

Usage

Dangerfile.df.kts is the main configuration file of any danger/kotlin setup. To use this plugin you should add it as a dependency on top of this file and call register.

@file:DependsOn("xyz.pavelkorolev.danger.detekt:plugin:x.y.z")

register.plugin(DetektPlugin)

Basic

Single file parse and report

This does what it says. If you have one detekt report and don't want any customization - that's probably your choice.

DetektPlugin.parseAndReport(reportFile)

Multiple files parse and report

Actually parameters of all parse functions are varargs, so you could provide it as many report files as you want.

DetektPlugin.parseAndReport(reportFile1, reportFile2, reportFile3)

or

val files: Array<File> = findReportFilesByYourself()
DetektPlugin.parseAndReport(*files)

Parse

You could also parse files without immediate reporting.

val report: DetektReport = DetektPlugin.parse(files)

This DetektReport contains everything from parsed detekt reports, so it could be useful you want to check something before actual reporting.

Report

You could also report it like this

DetektPlugin.report(report)

Please note, in order to make danger report files correctly you should configure detekt to return paths relative to working directory.

detekt {
    basePath = rootDir.absolutePath
}

Full example

@file:DependsOn("xyz.pavelkorolev.danger.detekt:plugin:x.y.z")

import systems.danger.kotlin.*
import systems.danger.kotlin.models.github.*
import xyz.pavelkorolev.danger.detekt.DetektPlugin
import java.io.File

register.plugin(DetektPlugin)

danger(args) {
    warnDetekt()
}

fun warnDetekt() {
    val file = File("build/reports/detekt/report.xml")
    if (!file.exists()) {
        warn(
            "πŸ™ˆ No detekt report found",
        )
        return
    }
    with(DetektPlugin) {
        val report = parse(file)
        val count = report.count
        if (count == 0) {
            message("πŸ‘πŸ‘πŸ‘ Good job! Detekt found no violations here!")
            return
        }
        fail(
            "πŸ™ Detekt violations found: **${report.count}**.\n" +
                    "Please fix them to proceed. We have zero-warning policy"
        )
        report(report)
    }
}

Customization

Functions DetektPlugin.report and DetektPlugin.parseAndReport have reporter: DetektErrorReporter parameter, which is in fact a functional interface with report(error: DetektError, fileName: String?) function.

By implementing this you could customize reporting logic and appearance as you want.

By default, there is DefaultDetektErrorReporter which has its own opinionated way to create messages and respect violations severities.

DefaultDetektErrorReporter uses inline comments if file and line provided for error. If you want to use only global report without inline comments then use DefaultDetektErrorReporter(context, isInlineEnabled = false) instead.

Implementation

Let's say you want to send everything found into fail table with emojis at the end. Write it like this.

class FailReporter(private val context: DangerContext) : DetektErrorReporter {

    override fun report(error: DetektError, fileName: String?) {
        val message = error.message ?: return
        context.fail("$message πŸ’₯πŸ’₯πŸ’₯")
    }
}

And use it like this

DetektPlugin.report(report, reporter = FailReporter(context))

Or you could implement the same as inline reporter thanks to its functional interface nature like so.

plugin.report(report) { error, _ ->
    error.message?.let(context::fail)
}

Ideas

  • You may want to send some warning if there are no detekt reports at all. Maybe something went wrong and detekt didn't even run for this build.
  • You may want to check violations count before reporting. If nothing found say "Good job" to PR author. Be grateful for such moments, appreciate your colleagues work.
  • You may want to check violations count to NOT send huge comments into PR. GitHub for example has a limitation on comment size. If there are many violations then Danger will fail sending them. Set a reasonable limit and print some generic message instead.
  • You even may want to check if some violations reported in someone's module or directory to mention them automatically.
  • Add a ton of emojis. Everyone loves them. Just kidding 😁 . Or not πŸ€” .

Side notes

This plugin is heavily inspired by AckeeCZ/danger-kotlin-detekt which is great, but lacks of customization and preliminary checks capabilities.

You might also like...
A flutter plugin to scan stripe readers and connect to the them and get the payment methods.

stripe_terminal A flutter plugin to scan stripe readers and connect to the them and get the payment methods. Installation Android No Configuration nee

Spigot-Plugin message providing system written in Kotlin

teller Spigot-Plugin message providing system written in Kotlin Usage Create an instance of PropertiesMessageProvider using the Constructor with an in

An under development minecraft plugin (1.8.8) to learning Kotlin language

CorePlus CorePlus is a minecraft plugin coded with Kotlin language. Still under development CorePlus will be an essential for each minecraft servers !

Kotlin Native Xcode Plugin
Kotlin Native Xcode Plugin

Kotlin Native Xcode Support Plugin to facilitate debugging iOS applications using Kotlin Native in Xcode. Defines Kotlin files as source code, with ba

Gradle plugin for simplify Kotlin Multiplatform mobile configurations
Gradle plugin for simplify Kotlin Multiplatform mobile configurations

Mobile Multiplatform gradle plugin This is a Gradle plugin for simple setup of Kotlin Multiplatform mobile Gradle modules. Setup buildSrc/build.gradle

A Kotlin compiler plugin that allows Java callers to pass in null for default parameters

kotlin-null-defaults (Compiler plugin) (Gradle Plugin) ( Currently pending approval) A Kotlin compiler plugin that allows Java callers to pass in null

LanServers - A small plugin written in Kotlin that runs on all major Minecraft Servers

LanServers This is a small plugin written in Kotlin that runs on all major Minec

kinstall is an easy way to install gradle-based command-line kotlin projects that use the application plugin.

kinstall kinstall is an easy way to install gradle-based command-line kotlin projects that use the application plugin. use First, install kinstall its

Godot's AdMob Plugin for Android (3.2.2+) using GitHub Actions for CI/CD. Working on Standard and Mono Godot versions.
Godot's AdMob Plugin for Android (3.2.2+) using GitHub Actions for CI/CD. Working on Standard and Mono Godot versions.

Godot AdMob Android A Godot's plugin for Android of AdMob. About β€’ Installation β€’ Docs β€’ Downloads About This repository is for a Godot Engine Plugin

Comments
  • Added SARIF format support

    Added SARIF format support

    • Updated Kotlin, detekt, kotest and jackson versions
    • Created new public API models like DetektReport, DetektViolation and so on
    • Previously used models now became Checkstyle-prefixed internal models
    • Sarif-prefixed internal models added
    • Previously used parser now could be used for both Checkstyle and SARIF files. It just delegates parsing logic to specific parsers which return public models, and merges violations into final report.
    opened by pavelkorolevxyz 0
Releases(v1.2.0)
  • v1.2.0(Dec 16, 2022)

    What's Changed

    • Added SARIF format support #1
      • That's why DetektReport model changed. Before this change DetektReport was exactly the same as Checkstyle XML structure. This Checkstyle XML and new SARIF JSON model are hidden now, they are mapped to generic DetektReport on parse.
      • Also Error word changed for Violation everywhere.
      • Because of DetektReport change, DetektViolationReporter doesn't need separate file parameter, only violation. DetektViolation has it inside.
    Source code(tar.gz)
    Source code(zip)
Detekt rule no string parameter with kotlin

Detekt custom rule template This repository is a template. You can use it to generate your own repository to write and share your custom rules. How to

Christian WΓΌstner 0 Nov 26, 2021
Plugin-shared-preferences - Pluto plugin to manage your Shared Preferences

Pluto Shared Preferences Plugin Pluto Shared Preferences is a Pluto plugin to in

Pluto 1 Feb 14, 2022
A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

common sense OSS 0 Jan 20, 2022
Kotlin-client-dsl - A kotlin-based dsl project for a (Client) -> (Plugin) styled program

kotlin-client-dsl a kotlin-based dsl project for a (Client) -> (Plugin) styled p

jackson 3 Dec 10, 2022
An annotation and Kotlin compiler plugin for enforcing a when statement is exhaustive

An annotation and Kotlin compiler plugin for enforcing a when statement is exhaustive

Cash App 468 Jan 4, 2023
A composite Github Action to execute the Kotlin Script with compiler plugin and dependency caching!

Kotlin Script Github Action Kotlin can also be used as a scripting language, which is more safer, concise, and fun to write than bash or python. Githu

Suresh 9 Nov 28, 2022
The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

Martmists 14 Dec 26, 2022
Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

Lorris Creantor 18 Nov 24, 2022
A Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a FatFramework for iOS targets, and manages the publishing process in a CocoaPod Repository.

KMP Framework Bundler KMP Framework Bundler is a Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a Fa

Marco Gomiero 17 Oct 29, 2022
Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders

Flutter plugin that leverages Storage Access Framework (SAF) API to get access and perform the operations on files and folders.

Vehement 8 Nov 26, 2022