Skip to content

Commit

Permalink
adding support for creating detekt baseline file (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadkahelghi-grabtaxi authored Jun 23, 2024
1 parent 32a00ac commit 82945b7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
66 changes: 62 additions & 4 deletions detekt/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ _ATTRS = {
default = False,
doc = "See [Detekt `--fail-fast` option](https://detetk.github.io/detekt/cli.html).",
),
"all_rules": attr.bool(
default = False,
doc = "See [Detekt `--all-rules` option](https://detekt.dev/docs/gettingstarted/cli/).",
),
"disable_default_rule_sets": attr.bool(
default = False,
doc = "See [Detekt `--disable-default-rulesets` option](https://detekt.dev/docs/gettingstarted/cli/).",
),
"auto_correct": attr.bool(
default = False,
doc = "See [Detekt `--auto-correct` option](https://detekt.dev/docs/gettingstarted/cli/).",
),
"parallel": attr.bool(
default = False,
doc = "See [Detekt `--parallel` option](https://detekt.github.io/detekt/cli.html).",
Expand All @@ -57,7 +69,10 @@ _ATTRS = {

TOOLCHAIN_TYPE = Label("//detekt:toolchain_type")

def _impl(ctx, run_as_test_target):
def _impl(
ctx,
run_as_test_target = False,
create_baseline = False):
action_inputs = []
action_outputs = []

Expand All @@ -83,7 +98,27 @@ def _impl(ctx, run_as_test_target):
action_inputs.extend(ctx.files.cfgs)
detekt_arguments.add_joined("--config", ctx.files.cfgs, join_with = ",")

if ctx.attr.baseline != None:
internal_baseline = None
baseline_script = ""
run_files = []
default_baseline = "default_baseline.xml"
if create_baseline:
detekt_arguments.add("--create-baseline")
internal_baseline = ctx.actions.declare_file("{}_baseline.xml".format(ctx.label.name))
run_files.append(internal_baseline)
action_outputs.append(internal_baseline)
detekt_arguments.add("--baseline", internal_baseline)
final_baseline = ctx.files.baseline[0].short_path if len(ctx.files.baseline) != 0 else "%s/%s" % (ctx.label.package, default_baseline)

baseline_script = """
#!/bin/bash
cp -rf {source} $BUILD_WORKING_DIRECTORY/{target}
echo "$(tput setaf 2)Updated {target} $(tput sgr0)"
""".format(
source = internal_baseline.short_path,
target = final_baseline,
)
elif ctx.attr.baseline != None:
action_inputs.append(ctx.file.baseline)
detekt_arguments.add("--baseline", ctx.file.baseline)

Expand Down Expand Up @@ -113,6 +148,15 @@ def _impl(ctx, run_as_test_target):
if ctx.attr.fail_fast:
detekt_arguments.add("--fail-fast")

if ctx.attr.all_rules:
detekt_arguments.add("--all-rules")

if ctx.attr.disable_default_rule_sets:
detekt_arguments.add("--disable-default-rulesets")

if ctx.attr.auto_correct:
detekt_arguments.add("--auto-correct")

if ctx.attr.parallel:
detekt_arguments.add("--parallel")

Expand All @@ -123,6 +167,7 @@ def _impl(ctx, run_as_test_target):
detekt_arguments.add_joined("--plugins", ctx.files.plugins, join_with = ",")

execution_result = ctx.actions.declare_file("{}_exit_code.txt".format(ctx.label.name))
run_files.append(execution_result)
action_outputs.append(execution_result)
detekt_arguments.add("--execution-result", "{}".format(execution_result.path))

Expand All @@ -138,6 +183,7 @@ def _impl(ctx, run_as_test_target):
},
arguments = [java_arguments, detekt_arguments],
)
run_files.append(txt_report)

# Note: this is not compatible with Windows, feel free to submit PR!
# text report-contents are always printed to shell
Expand All @@ -152,22 +198,26 @@ report=$(cat {text_report})
if [ ! -z "$report" ]; then
echo "$report"
fi
{baseline_script}
exit "$exit_code"
""".format(execution_result = execution_result.short_path, text_report = txt_report.short_path),
""".format(execution_result = execution_result.short_path, text_report = txt_report.short_path, baseline_script = baseline_script),
is_executable = True,
)

return [
DefaultInfo(
files = depset(action_outputs),
executable = final_result,
runfiles = ctx.runfiles(files = [execution_result, txt_report]),
runfiles = ctx.runfiles(files = run_files),
),
]

def _detekt_impl(ctx):
return _impl(ctx = ctx, run_as_test_target = False)

def _detekt_create_baseline_impl(ctx):
return _impl(ctx = ctx, create_baseline = True)

def _detekt_test_impl(ctx):
return _impl(ctx = ctx, run_as_test_target = True)

Expand All @@ -178,6 +228,14 @@ detekt = rule(
toolchains = ["//detekt:toolchain_type"],
)

detekt_create_baseline = rule(
implementation = _detekt_create_baseline_impl,
attrs = _ATTRS,
provides = [DefaultInfo],
toolchains = ["//detekt:toolchain_type"],
executable = True,
)

detekt_test = rule(
implementation = _detekt_test_impl,
attrs = _ATTRS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import io.gitlab.arturbosch.detekt.cli.CliRunner;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

public interface Detekt {

Expand All @@ -18,6 +23,35 @@ public void execute(String[] args, PrintStream output, PrintStream error) {

if (resultError != null) {
throw resultError;
} else {
List<String> argsList = Arrays.asList(args);
if (argsList.contains("--create-baseline")) {
String baseline = argsList.get(argsList.indexOf("--baseline") + 1);
File baselineFile = new File(baseline);
if (!baselineFile.exists()) {
try {
createEmptyBaseline(baselineFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}

String emptyBaseLineContent = "<?xml version=\"1.0\" ?>\n" +
"<SmellBaseline>\n" +
" <ManuallySuppressedIssues></ManuallySuppressedIssues>\n" +
" <CurrentIssues>\n" +
" </CurrentIssues>\n" +
"</SmellBaseline>\n";

private void createEmptyBaseline(File baselineFile) throws IOException {
baselineFile.createNewFile();
try (FileWriter writer = new FileWriter(baselineFile)) {
writer.write(emptyBaseLineContent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions docs/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ Name | Type | Default | Descr
`name` | [`name`](https://docs.bazel.build/versions/master/build-ref.html#name) | — | A unique name for this target.
`baseline` | [`Label`](https://docs.bazel.build/versions/master/skylark/lib/Label.html) | `None` | [Detekt baseline file](https://detekt.github.io/detekt/baseline.html).
`build_upon_default_config` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `--build-upon-default-config` option](https://detekt.github.io/detekt/cli.html).
`auto_correct` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `auto_correct` option](https://detekt.github.io/detekt/cli.html).
`disable_default_rule_sets` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `disable_default_rule_sets` option](https://detekt.github.io/detekt/cli.html).
`all_rules` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `all_rules` option](https://detekt.github.io/detekt/cli.html).
`cfgs` | [`[Label]`](https://docs.bazel.build/versions/master/skylark/lib/list.html) | `[]` | [Detekt configuration files](https://detekt.github.io/detekt/configurations.html). Otherwise [the default configuration](https://github.com/detekt/detekt/blob/master/detekt-core/src/main/resources/default-detekt-config.yml) is used.
`disable_default_rulesets` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `--disable-default-rulesets` option](https://detekt.github.io/detekt/cli.html).
`fail_fast` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `--fail-fast` option](https://detetk.github.io/detekt/cli.html).
`html_report` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | Enables / disables the HTML report generation. The report file name is `{target_name}_detekt_report.html`.
`parallel` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | See [Detekt `--parallel` option](https://detekt.github.io/detekt/cli.html).
`plugins` | [`[Label]`](https://docs.bazel.build/versions/master/skylark/lib/list.html) | `[]` | [Detekt plugins](https://detekt.github.io/detekt/extensions.html). For example, [the formatting rule set](https://detekt.github.io/detekt/formatting.html). Labels should be JVM artifacts (generated via [`rules_jvm_external`](https://github.com/bazelbuild/rules_jvm_external) work).
`srcs` | [`[Label]`](https://docs.bazel.build/versions/master/skylark/lib/list.html) | — | Kotlin source code files.
`xml_report` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | Enables / disables the XML report generation. The report file name is `{target_name}_detekt_report.xml`. FYI Detekt uses the Checkstyle XML reporting format which makes it compatible with tools like SonarQube.

`xml_report` | [`bool`](https://docs.bazel.build/versions/master/skylark/lib/bool.html) | `False` | Enables / disables the XML report generation. The report file name is `{target_name}_detekt_report.xml`. FYI Detekt uses the Checkstyle XML reporting format which makes it compatible with tools like SonarQube.

0 comments on commit 82945b7

Please sign in to comment.