Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported multiple reporters in maven #1824

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@
.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 43 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

Added line #L43 was 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 @@ -2,9 +2,11 @@

import com.saveourtool.diktat.DiktatRunner
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.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 All @@ -15,8 +17,6 @@
import org.apache.maven.project.MavenProject

import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.inputStream
Expand All @@ -33,17 +33,10 @@
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

Check warning on line 39 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#L39

Added line #L39 was not covered by tests

/**
* Baseline file, containing a list of errors that will be ignored.
Expand Down Expand Up @@ -100,19 +93,23 @@
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?.getAll() ?: listOf(PlainReporter()))
.let { all ->

Check warning on line 98 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#L98

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

Check warning on line 100 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#L100

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

Check warning on line 102 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#L102

Added line #L102 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 112 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#L112

Added line #L112 was not covered by tests
)
val diktatRunner = diktatRunnerFactory(args)
val errorCounter = runAction(
Expand All @@ -124,23 +121,6 @@
}
}

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
*
* @param type type of reporter
*/
open class DefaultReporter(
private val type: DiktatReporterType,

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
Fixed Show fixed Hide fixed
) : 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,56 @@
package com.saveourtool.diktat.plugin.maven.reporters

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

/**
Fixed Show fixed Hide fixed
* 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
*/
fun getAll(): List<Reporter> = listOfNotNull(
plain,
json,
sarif,
gitHubActions,
checkstyle,
html,
)

Check warning on line 55 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#L48-L55

Added lines #L48 - L55 were not covered by tests
}
Loading