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

Gradle and Maven Plugins: reporter types are fixed and updated #1180

Merged
merged 13 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Main features of diktat are the following:
4) **Strict detailed coding convention** that you can use in your project.

## Run as CLI-application
<details>
<summary>Download and install binaries:</summary>
1. Install KTlint manually: [here](https://github.com/pinterest/ktlint/releases)

**OR** use curl:
Expand All @@ -68,12 +70,34 @@ Main features of diktat are the following:
```

To **autofix** all code style violations use `-F` option.
</details>

## GitHub Native Integration
We suggest everyone to use common ["sarif"](https://docs.oasis-open.org/sarif/sarif/v2.0/sarif-v2.0.html) format as a `reporterType` in CI/CD.
GitHub has an [integration](https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning)
with SARIF format and provides you a native reporting of diktat issues in Pull Requests.

```text
petertrr marked this conversation as resolved.
Show resolved Hide resolved
reporterType = "sarif"
output = "diktat-report.sarif"
```

Add the following code to your GitHub Action to upload diktat sarif report (after it was generated).

```yml
- name: Upload SARIF to Github using the upload-sarif action
uses: github/codeql-action/upload-sarif@v1
if: ${{ always() }}
with:
sarif_file: build/diktat-report.sarif
```

## Run with Maven using diktat-maven-plugin
This plugin is available since version 0.1.3. You can see how it is configured in our project for self-checks: [pom.xml](pom.xml).
If you use it and encounter any problems, feel free to open issues on [github](https://github.com/cqfn/diktat/issues).

Add this plugin to your pom.xml:
<details>
<summary>Add this plugin to your pom.xml:</summary>
```xml
<plugin>
<groupId>org.cqfn.diktat</groupId>
Expand Down Expand Up @@ -101,6 +125,7 @@ Add this plugin to your pom.xml:
</executions>
</plugin>
```
</details>

To run diktat in **only-check** mode use command `$ mvn diktat:check@diktat`.
To run diktat in **autocorrect** mode use command `$ mvn diktat:fix@diktat`.
Expand All @@ -109,7 +134,9 @@ To run diktat in **autocorrect** mode use command `$ mvn diktat:fix@diktat`.
Requires a gradle version no lower than 5.3.

This plugin is available since version 0.1.5. You can see how the plugin is configured in our examples: [build.gradle.kts](examples/gradle-kotlin-dsl/build.gradle.kts).
Add this plugin to your `build.gradle.kts`:

<details>
<summary>Add this plugin to your `build.gradle.kts`:</summary>
```kotlin
plugins {
id("org.cqfn.diktat.diktat-gradle-plugin") version "1.0.2"
Expand Down Expand Up @@ -141,29 +168,21 @@ diktat {
}
```

Also `diktat` extension has different reporters. You can specify `json`, `html`, `checkstyle`, `plain` (default) or your own custom reporter:
Also `diktat` extension has different reporters. You can specify `json`, `html`, `sarif`, `plain` (default) or your own custom reporter (it should be added as a dependency into `diktat` configuration):
```kotlin
diktat {
reporter = "json" // "html", "checkstyle", "plain"
reporterType = "json" // "html", "json", "plain" (default), "sarif"
}
```

Example of your custom reporter:
```kotlin
diktat {
reporter = "custom:name:pathToJar"
}
```
Name parameter is the name of your reporter and as the last parameter you should specify path to jar, which contains your reporter.
[Example of the junit custom reporter.](https://github.com/kryanod/ktlint-junit-reporter)

You can also specify an output.
```kotlin
diktat {
reporter = "json"
reporterType = "json"
output = "someFile.json"
}
```
</details>

You can run diktat checks using task `diktatCheck` and automatically fix errors with tasks `diktatFix`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ open class DiktatJavaExecTaskBase @Inject constructor(
main = "com.pinterest.ktlint.Main"
}

// Plain, checkstyle and json reporter are provided out of the box in ktlint
if (diktatExtension.reporterType == "html") {
petertrr marked this conversation as resolved.
Show resolved Hide resolved
diktatConfiguration.dependencies.add(project.dependencies.create("com.pinterest.ktlint:ktlint-reporter-html:$KTLINT_VERSION"))
}
classpath = diktatConfiguration
project.logger.debug("Setting diktatCheck classpath to ${diktatConfiguration.dependencies.toSet()}")
if (diktatExtension.debug) {
Expand Down Expand Up @@ -155,13 +151,8 @@ open class DiktatJavaExecTaskBase @Inject constructor(
private fun createReporterFlag(diktatExtension: DiktatExtension): String {
val flag: StringBuilder = StringBuilder()

// Plain, checkstyle and json reporter are provided out of the box in ktlint
when (diktatExtension.reporterType) {
"json" -> flag.append("--reporter=json")
"html" -> flag.append("--reporter=html")
"checkstyle" -> flag.append("--reporter=checkstyle")
else -> customReporter(diktatExtension, flag)
}
// appending the flag with the reporter
setReporterType(diktatExtension, flag)

if (diktatExtension.output.isNotEmpty()) {
flag.append(",output=${diktatExtension.output}")
Expand All @@ -170,19 +161,14 @@ open class DiktatJavaExecTaskBase @Inject constructor(
return flag.toString()
}

private fun customReporter(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) {
if (diktatExtension.reporterType.startsWith("custom")) {
val name = diktatExtension.reporterType.split(":")[1]
val jarPath = diktatExtension.reporterType.split(":")[2]
if (name.isEmpty() || jarPath.isEmpty()) {
project.logger.warn("Either name or path to jar is not specified. Falling to plain reporter")
flag.append("--reporter=plain")
} else {
flag.append("--reporter=$name,artifact=$jarPath")
}
} else {
private fun setReporterType(diktatExtension: DiktatExtension, flag: java.lang.StringBuilder) {
val name = diktatExtension.reporterType
val validReporterTypes = listOf("sarif", "plain", "json", "html")
if (name.isEmpty() || !validReporterTypes.contains(name.trim())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about custom reporters here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we talked about that, no? I think that we should not give people any liberty and a chance to use their own formats. We are already fucked up with with custom formats. It's time to stop it. SARIF - is the solution

project.logger.warn("Reporter name $name was not specified or is invalid. Falling to 'plain' reporter")
flag.append("--reporter=plain")
project.logger.debug("Unknown reporter was specified. Falling back to plain reporter.")
} else {
flag.append("--reporter=$name")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,6 @@ class DiktatJavaExecTaskTest {
}
}

@Test
fun `check command line has custom reporter type with output`() {
assertCommandLineEquals(
listOf(null, "--reporter=customName,artifact=customPath")
) {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
reporterType = "custom:customName:customPath"
}
}

@Test
fun `check that project has html dependency`() {
val task = project.registerDiktatTask {
inputs { exclude("*") }
diktatConfigFile = project.file("../diktat-analysis.yml")
reporterType = "html"
}

Assertions.assertTrue(
project
.configurations
.getByName("diktat")
.dependencies
.any { it.name == "ktlint-reporter-html" })
Assertions.assertEquals(File(project.projectDir.parentFile, "diktat-analysis.yml").absolutePath, task.systemProperties[DIKTAT_CONF_PROPERTY])
}

@Test
fun `check system property with multiproject build with default config`() {
setupMultiProject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DiktatMavenPluginIntegrationTest {
Assertions.assertTrue(result.isFailure)

val mavenLog = result.mavenLog.stdout.readText()

Assertions.assertTrue(
mavenLog.contains(Regex("""Original and formatted content differ, writing to [:\w/\\]+Test\.kt\.\.\."""))
)
Expand Down