abosch
abosch

Reputation: 21

Why are my added NewIssues not transfered to sonar? (Custom Sonar Plugin)

Heyho,

I have my sonar-kotlin-plugin up and running. My RuleDefinitions are dislayed on the Rules-page, but when analyzing some kotlin projects the found issues are not saved.

My sensor-issue-adding-code looks like this:

private fun projectIssues(detektion: Detektion, context: SensorContext) {
    val fileSystem = context.fileSystem()
    val baseDir = fileSystem.baseDir()
    val predicates = fileSystem.predicates()
    detektion.findings.forEach { _, findings ->
        findings.forEach { issue ->
            val inputFile = fileSystem.inputFile(predicates.`is`(baseDir.resolve(issue.location.file)))
            if (inputFile != null) {
                val newIssue = context.newIssue()
                        .forRule(findKey(issue.id))
                        .gap(2.0) // TODO what to write here?
                        .primaryLocation(issue, inputFile)
                println(newIssue)
                newIssue.save()
            } else {
                println("No file found for ${issue.location.file}")
            }
        }
    }
}

private fun NewIssue.primaryLocation(issue: Finding, inputFile: InputFile): NewIssue {
    val (line, _) = issue.startPosition
    val newIssueLocation = newLocation()
            .on(inputFile)
            .at(inputFile.selectLine(line))
            .message("TODO needs PR!")
    return this.at(newIssueLocation)
}

Even the println(newIssue) shows that it was created.

I use sonar-plugin-api version 5.6, org.sonar-gradle-plugin in version 2.5, my sonar distribution is in version 5.6.6 and I use the embedded database.

My whole code is in this PR https://github.com/arturbosch/detekt/pull/81.

Thx for your help in advance.

Upvotes: 1

Views: 173

Answers (1)

abosch
abosch

Reputation: 21

I've solved my issue by activating my rules in the RulesProfile:

override fun createProfile(validation: ValidationMessages): RulesProfile {
    val profile = RulesProfile.create(DETEKT_WAY, KOTLIN_KEY)
    RULE_KEYS.forEach {
        profile.activateRule(Rule.create(it.repository(), it.rule()), null)
    }
    return profile
} 

Upvotes: 1

Related Questions