diktat-demo-gradle

A template Gradle project (Groovy DSL flavour) which uses DiKTat to check the style of your code

MIT License

Stars
4

This is a template Gradle project (Groovy DSL flavour) which uses https://github.com/saveourtool/diktat[_DiKTat_] to check the style of your code.

If your project has multiple modules, be sure to pass the https://docs.gradle.org/current/userguide/command_line_interface.html#sec:continue_build_on_failure[`--continue`] flag to Gradle:

[source,shell]

./gradlew --continue diktatCheck

Here's a sample DiKTat configuration. Depending on whether you pass the -Pdiktat.githubActions=true property to Gradle, the output will be written either in plain text format to the standard output, or in the https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning#about-sarif-support[SARIF] format to a report file:

[source,groovy]

buildscript { repositories { mavenCentral() }

dependencies {
    classpath group: 'org.cqfn.diktat', name: 'diktat-gradle-plugin', version: 1.2.3
}

}

apply plugin: 'org.cqfn.diktat.diktat-gradle-plugin'

diktat { inputs { it.include("src//.kt", ".kts", "src//*.kts") it.exclude("build/**") }

def sarifOutput = Boolean.parseBoolean(project.findProperty("diktat.githubActions"))

reporter = sarifOutput ? "sarif" : "plain"

output = sarifOutput ? "build/reports/diktat/diktat.sarif" : ""

debug = false

}

reporter can be one of:

  • html,
  • json,
  • plain (the default), and
  • sarif.

If the output field is empty, reports will be written to the standard output.

While it's possible to configure reporter and output independently, you may consider using the githubActions flag instead. This configuration is effectively equivalent to the one above:

[source,groovy]

buildscript { repositories { mavenCentral() }

dependencies {
    classpath group: 'org.cqfn.diktat', name: 'diktat-gradle-plugin', version: 1.2.3
}

}

apply plugin: 'org.cqfn.diktat.diktat-gradle-plugin'

diktat { inputs { it.include("src//.kt", ".kts", "src//*.kts") it.exclude("build/**") }

githubActions = Boolean.parseBoolean(project.findProperty("diktat.githubActions"))

debug = false

}

You can integrate with https://docs.github.com/en/actions[_GitHub Actions_] and make code scanning results (e.g.: after a pull request) immediately available by adding a YAML file of the following content under .github/workflows (uses https://github.com/github/codeql-action[github/codeql-action/upload-sarif@v2]):

[source,yaml]

name: Run diKTat

on: push: branches: [ master ] pull_request: branches: [ master ]

env: GRADLE_OPTS: -Dorg.gradle.daemon=false

jobs: diktat_check: runs-on: ubuntu-20.04

permissions:
  security-events: write

steps:
  - uses: actions/checkout@v3
  - uses: actions/setup-java@v3
    with:
      java-version: '8'
      distribution: 'zulu'
      java-package: jdk+fx
      cache: gradle
  - uses: gradle/gradle-build-action@v2
    with:
      gradle-version: wrapper
      arguments: |
        --continue
        diktatCheck
        -Pdiktat.githubActions=true
  - name: Copy SARIF reports into a single directory
    if: ${{ always() }}
    run: |
      mkdir -p build/diktat-sarif-reports
      i=0
      find -path '*/build/reports/diktat/*.sarif' | while read -r f; do cp "$f" "build/diktat-sarif-reports/diktat-$((i++)).sarif"; done
  - name: Upload SARIF reports to GitHub
    if: ${{ always() }}
    uses: github/codeql-action/upload-sarif@v2
    with:
      sarif_file: build/diktat-sarif-reports

The shell script fragment:

[source,shell]

mkdir -p build/diktat-sarif-reports
i=0
find -path '/build/reports/diktat/.sarif' | while read -r f;
do
cp "$f" "build/diktat-sarif-reports/diktat-$((i++)).sarif";
done

-- merely copies all the generated SARIF reports into a single directory, so that they can be read by GitHub.

Related Projects