Skip to content

Commit

Permalink
Supported multiple reporters in maven
Browse files Browse the repository at this point in the history
### What's done:
- supported reporters to configure reporters in maven
- supported a default location for all reporters

It closes #1818
  • Loading branch information
nulls committed Nov 27, 2023
1 parent 2030a14 commit e857f3d
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Utilities for diktat gradle plugin
*/

@file:Suppress("FILE_NAME_MATCH_CLASS", "MatchingDeclarationName")

package com.saveourtool.diktat.plugin.gradle

import org.gradle.api.Project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.saveourtool.diktat.plugin.gradle.extension

import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import com.saveourtool.diktat.api.DiktatReporterType
import com.saveourtool.diktat.plugin.gradle.defaultReportLocation
import org.gradle.api.Project
Expand All @@ -14,6 +15,8 @@ import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import javax.inject.Inject

/**
Expand All @@ -32,6 +35,12 @@ abstract class DefaultReporter @Inject constructor(
.also { fileProperty ->
fileProperty.convention(project.defaultReportLocation(extension = type.extension))
}

override fun toCreationArguments(sourceRootDir: Path): DiktatReporterCreationArguments = DiktatReporterCreationArguments(
reporterType = type,
outputStream = output.map { file -> file.asFile.also { Files.createDirectories(it.parentFile.toPath()) }.outputStream() }.orNull,

Check warning on line 41 in diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extension/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extension/DefaultReporter.kt#L39-L41

Added lines #L39 - L41 were not covered by tests
sourceRootDir = sourceRootDir.takeIf { type == DiktatReporterType.SARIF },
)
}

Check warning on line 44 in diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extension/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extension/DefaultReporter.kt#L43-L44

Added lines #L43 - L44 were not covered by tests

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.saveourtool.diktat.plugin.gradle.extension

import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.OutputFile
import java.nio.file.Path

/**
* A base interface for reporter
Expand All @@ -12,4 +14,10 @@ interface Reporter {
*/
@get:OutputFile
val output: RegularFileProperty

/**
* @param sourceRootDir
* @return [DiktatReporterCreationArguments] to create this reporter
*/
fun toCreationArguments(sourceRootDir: Path): DiktatReporterCreationArguments
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import com.saveourtool.diktat.DiktatRunnerArguments
import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import com.saveourtool.diktat.api.DiktatReporterType
import com.saveourtool.diktat.diktatRunnerFactory
import com.saveourtool.diktat.plugin.maven.reporters.DefaultReporter
import com.saveourtool.diktat.plugin.maven.reporters.GitHubActionsReporter
import com.saveourtool.diktat.plugin.maven.reporters.PlainReporter
import com.saveourtool.diktat.plugin.maven.reporters.Reporter
import com.saveourtool.diktat.plugin.maven.reporters.Reporters
import com.saveourtool.diktat.util.isKotlinCodeOrScript

import org.apache.maven.plugin.AbstractMojo
Expand Down Expand Up @@ -33,17 +38,10 @@ abstract class DiktatBaseMojo : AbstractMojo() {
var githubActions = false

/**
* Type of the reporter to use
* The reporters to use
*/
@Parameter(property = "diktat.reporter")
var reporter = "plain"

/**
* Type of output
* Default: System.out
*/
@Parameter(property = "diktat.output")
var output = ""
@Parameter
var reporters: Reporters? = null

/**
* Baseline file, containing a list of errors that will be ignored.
Expand Down Expand Up @@ -100,19 +98,23 @@ abstract class DiktatBaseMojo : AbstractMojo() {
if (excludes.isNotEmpty()) " and excluding $excludes" else ""
)

val sourceRootDir = mavenProject.basedir.parentFile.toPath()
val reporterType = getReporterType()
val reporterArgs = DiktatReporterCreationArguments(
reporterType = reporterType,
outputStream = getReporterOutput(),
sourceRootDir = sourceRootDir.takeIf { reporterType == DiktatReporterType.SARIF },
)
val sourceRootDir = generateSequence(mavenProject) { it.parent }.last().basedir.toPath()
val reporters: List<Reporter> = (reporters?.all ?: listOf(PlainReporter()))
.let { all ->

Check warning on line 103 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L103

Added line #L103 was not covered by tests
if (githubActions && all.filterIsInstance<GitHubActionsReporter>().isEmpty()) {
all + GitHubActionsReporter()

Check warning on line 105 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L105

Added line #L105 was not covered by tests
} else {
all

Check warning on line 107 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L107

Added line #L107 was not covered by tests
}
}

val reporterArgsList = reporters.map { it.toCreationArguments(mavenProject, sourceRootDir) }
val args = DiktatRunnerArguments(
configInputStream = configFile.inputStream(),
sourceRootDir = sourceRootDir,
files = files(),
baselineFile = baseline?.toPath(),
reporterArgsList = listOf(reporterArgs),
reporterArgsList = reporterArgsList,

Check warning on line 117 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L117

Added line #L117 was not covered by tests
)
val diktatRunner = diktatRunnerFactory(args)
val errorCounter = runAction(
Expand All @@ -124,23 +126,6 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}

private fun getReporterType(): DiktatReporterType = if (githubActions) {
DiktatReporterType.SARIF
} else {
DiktatReporterType.entries.firstOrNull { it.id.equals(reporter, ignoreCase = true) } ?: run {
log.warn("Reporter name ${this.reporter} was not specified or is invalid. Falling to 'plain' reporter")
DiktatReporterType.PLAIN
}
}

private fun getReporterOutput(): OutputStream? = if (output.isNotBlank()) {
FileOutputStream(this.output, false)
} else if (githubActions) {
FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false)
} else {
null
}

/**
* Function that searches diktat config file in maven project hierarchy.
* If [diktatConfigFile] is absolute, it's path is used. If [diktatConfigFile] is relative, this method looks for it in all maven parent projects.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Utilities for diktat maven plugin
*/

package com.saveourtool.diktat.plugin.maven

import com.saveourtool.diktat.api.DiktatReporterType
import org.apache.maven.project.MavenProject
import java.io.File

/**
* @param reporterType
* @return default location of report with provided [reporterType]
*/
internal fun MavenProject.defaultReportLocation(
reporterType: DiktatReporterType,
): File = basedir
.resolve(build.directory)
.resolve("reports")
.resolve("diktat")
.resolve("diktat.${reporterType.extension}")

Check warning on line 21 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/Utils.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/Utils.kt#L17-L21

Added lines #L17 - L21 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* All default reporters
*/

package com.saveourtool.diktat.plugin.maven.reporters

import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import com.saveourtool.diktat.api.DiktatReporterType
import com.saveourtool.diktat.plugin.maven.defaultReportLocation
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import java.io.File
import java.nio.file.Path

/**
* A base interface for a default reporter
*
* @property type type of reporter
*/
abstract class DefaultReporter(

Check warning

Code scanning / detekt

An abstract class is unnecessary. May be refactored to an interface or to a concrete class. Warning

An abstract class without an abstract member can be refactored to a concrete class.
private val type: DiktatReporterType,

Check failure

Code scanning / ktlint

[KDOC_NO_CONSTRUCTOR_PROPERTY] all properties from the primary constructor should be documented in a @Property tag in KDoc: change @property tag to @param tag for to KDoc Error

[KDOC_NO_CONSTRUCTOR_PROPERTY] all properties from the primary constructor should be documented in a @property tag in KDoc: change @property tag to @param tag for to KDoc

Check warning on line 21 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L20-L21

Added lines #L20 - L21 were not covered by tests
) : Reporter {
/**
* Location for output
*/
@Parameter
var output: File? = null

Check warning on line 27 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L27

Added line #L27 was not covered by tests

override fun getOutput(project: MavenProject): File? = output ?: project.defaultReportLocation(type)

override fun toCreationArguments(
project: MavenProject,
sourceRootDir: Path,
): DiktatReporterCreationArguments = DiktatReporterCreationArguments(
reporterType = type,
outputStream = getOutputStream(project),

Check warning on line 36 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L34-L36

Added lines #L34 - L36 were not covered by tests
sourceRootDir = sourceRootDir.takeIf { type == DiktatReporterType.SARIF },
)

Check warning on line 38 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L38

Added line #L38 was not covered by tests
}

/**
* Plain reporter
*/
class PlainReporter : DefaultReporter(
type = DiktatReporterType.PLAIN,

Check warning on line 45 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L44-L45

Added lines #L44 - L45 were not covered by tests
) {
/**
* Plain reporter prints to stdout by default
*/
override fun getOutput(project: MavenProject): File? = output

Check warning on line 50 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L50

Added line #L50 was not covered by tests
}

/**
* JSON reporter
*/
class JsonReporter : DefaultReporter(
type = DiktatReporterType.JSON,

Check warning on line 57 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L56-L57

Added lines #L56 - L57 were not covered by tests
)

/**
* SARIF reporter
*/
class SarifReporter : DefaultReporter(
type = DiktatReporterType.SARIF,

Check warning on line 64 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L63-L64

Added lines #L63 - L64 were not covered by tests
)

/**
* Checkstyle reporter
*/
class CheckstyleReporter : DefaultReporter(
type = DiktatReporterType.CHECKSTYLE,

Check warning on line 71 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L70-L71

Added lines #L70 - L71 were not covered by tests
)

/**
* HTML reporter
*/
class HtmlReporter : DefaultReporter(
type = DiktatReporterType.HTML,

Check warning on line 78 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/DefaultReporter.kt#L77-L78

Added lines #L77 - L78 were not covered by tests
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.saveourtool.diktat.plugin.maven.reporters

import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import com.saveourtool.diktat.api.DiktatReporterType
import com.saveourtool.diktat.plugin.maven.defaultReportLocation
import org.apache.maven.project.MavenProject
import java.io.File
import java.nio.file.Path

/**
* GitHub actions reporter
*/
class GitHubActionsReporter : Reporter {
override fun getOutput(project: MavenProject): File = project.defaultReportLocation(DiktatReporterType.SARIF)

Check warning on line 14 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/GitHubActionsReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/GitHubActionsReporter.kt#L13-L14

Added lines #L13 - L14 were not covered by tests
override fun toCreationArguments(project: MavenProject, sourceRootDir: Path): DiktatReporterCreationArguments =
DiktatReporterCreationArguments(
reporterType = DiktatReporterType.SARIF,
outputStream = getOutputStream(project),
sourceRootDir = sourceRootDir,
)

Check warning on line 20 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/GitHubActionsReporter.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/GitHubActionsReporter.kt#L16-L20

Added lines #L16 - L20 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.saveourtool.diktat.plugin.maven.reporters

import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import org.apache.maven.project.MavenProject
import java.io.File
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.Path

/**
* A base interface for reporter
*/
interface Reporter {
/**
* @param project
* @return location as a [File] for output or default value resolved by [project]
*/
fun getOutput(project: MavenProject): File?

/**
* @param project
* @return location as an [OutputStream] for output or default value resolved by [project]
*/
fun getOutputStream(project: MavenProject): OutputStream? = getOutput(project)?.also { Files.createDirectories(it.parentFile.toPath()) }?.outputStream()

/**
* @param project
* @param sourceRootDir
* @return [DiktatReporterCreationArguments] to create this reporter
*/
fun toCreationArguments(project: MavenProject, sourceRootDir: Path): DiktatReporterCreationArguments
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.saveourtool.diktat.plugin.maven.reporters

import org.apache.maven.plugins.annotations.Parameter

/**

Check failure

Code scanning / ktlint

[USE_DATA_CLASS] this class can be converted to a data class: Reporters (cannot be auto-corrected) Error

[USE_DATA_CLASS] this class can be converted to a data class: Reporters (cannot be auto-corrected)
* Configuration for reporters
*/
class Reporters {

Check warning on line 8 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L8

Added line #L8 was not covered by tests
/**
* Configure *plain* reporter
*/
@Parameter
var plain: PlainReporter? = null

Check warning on line 13 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L13

Added line #L13 was not covered by tests

/**
* Configure *json* reporter
*/
@Parameter
var json: JsonReporter? = null

Check warning on line 19 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L19

Added line #L19 was not covered by tests

/**
* Configure *sarif* reporter
*/
@Parameter
var sarif: SarifReporter? = null

Check warning on line 25 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L25

Added line #L25 was not covered by tests

/**
* Configure *sarif* reporter for GitHub actions
*/
@Parameter
var gitHubActions: GitHubActionsReporter? = null

Check warning on line 31 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L31

Added line #L31 was not covered by tests

/**
* Configure *checkstyle* reporter
*/
@Parameter
var checkstyle: CheckstyleReporter? = null

Check warning on line 37 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L37

Added line #L37 was not covered by tests

/**
* Configure *html* reporter
*/
@Parameter
var html: HtmlReporter? = null

Check warning on line 43 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L43

Added line #L43 was not covered by tests

/**
* @return all configured reporters
*/
val all: List<Reporter>
get() = listOfNotNull(

Check failure

Code scanning / ktlint

[CUSTOM_GETTERS_SETTERS] custom getters and setters are not recommended, use class methods instead: get (cannot be auto-corrected) Error

[CUSTOM_GETTERS_SETTERS] custom getters and setters are not recommended, use class methods instead: get (cannot be auto-corrected)
plain,
json,
sarif,
gitHubActions,
checkstyle,
html,
)

Check warning on line 56 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/reporters/Reporters.kt#L49-L56

Added lines #L49 - L56 were not covered by tests
}

0 comments on commit e857f3d

Please sign in to comment.