-
-
Notifications
You must be signed in to change notification settings - Fork 144
Sarif reporting #1660
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
Open
matejdro
wants to merge
4
commits into
autonomousapps:main
Choose a base branch
from
matejdro:sarif-reporting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sarif reporting #1660
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/main/kotlin/com/autonomousapps/internal/advice/ProjectHealthSarifReportBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // Copyright (c) 2025. Tony Robalik. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.autonomousapps.internal.advice | ||
|
|
||
| import com.autonomousapps.model.SourcedProjectAdvice | ||
| import io.github.detekt.sarif4k.* | ||
|
|
||
| internal class ProjectHealthSarifReportBuilder( | ||
| projectAdvices: Collection<SourcedProjectAdvice>, | ||
| dslKind: DslKind, | ||
| /** Customize how dependencies are printed. */ | ||
| dependencyMap: ((String) -> String?)? = null, | ||
| useTypesafeProjectAccessors: Boolean, | ||
| ) { | ||
|
|
||
| val sarif: SarifSchema210 | ||
|
|
||
| private val advicePrinter = AdvicePrinter(dslKind, dependencyMap, useTypesafeProjectAccessors) | ||
|
|
||
| init { | ||
| val pluginResults = projectAdvices.flatMap { projectAdvice -> | ||
| projectAdvice.pluginAdvice.map { advice -> | ||
| val location = projectAdvice.projectBuildFile?.let { buildFile -> | ||
| Location( | ||
| physicalLocation = PhysicalLocation( | ||
| artifactLocation = ArtifactLocation(uri = buildFile), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| Result( | ||
| locations = listOfNotNull(location), | ||
| message = Message(text = "Pluigin ${advice.redundantPlugin} should be removed: ${advice.reason}"), | ||
| ruleID = "dependencyAnalysis.Plugin" | ||
| ) | ||
| } | ||
|
|
||
| } | ||
| val dependencyResults = projectAdvices.flatMap { projectAdvice -> | ||
| projectAdvice.dependencyAdvice.map { sourcedAdvice -> | ||
| val message: String | ||
| val ruleId: String | ||
|
|
||
| val advice = sourcedAdvice.advice | ||
| when { | ||
| advice.isAdd() -> { | ||
| message = "Transitive dependency ${advicePrinter.toDeclaration(advice).trim()} should be declared directly" | ||
| ruleId = "dependencyanalysis.Add" | ||
| } | ||
|
|
||
| advice.isRemove() -> { | ||
| message = "Unused dependency ${advicePrinter.fromDeclaration(advice).trim()} should be removed" | ||
| ruleId = "dependencyanalysis.Remove" | ||
| } | ||
|
|
||
| advice.isChange() -> { | ||
| message = | ||
| "Dependency ${ | ||
| advicePrinter.fromDeclaration(advice).trim() | ||
| } should be modified to ${advice.toConfiguration} from ${advice.fromConfiguration}" | ||
| ruleId = "dependencyanalysis.Change" | ||
| } | ||
|
|
||
| advice.isChangeToRuntimeOnly() -> { | ||
| message = | ||
| "Dependency ${advicePrinter.fromDeclaration(advice).trim()} should be removed or changed to runtime-only" | ||
| ruleId = "dependencyanalysis.ChangeRuntimeOnly" | ||
| } | ||
|
|
||
| advice.isCompileOnly() -> { | ||
| message = "Dependency ${advicePrinter.fromDeclaration(advice).trim()} should be changed to compile-only" | ||
| ruleId = "dependencyanalysis.ChangeCompileOnly" | ||
| } | ||
|
|
||
| advice.isProcessor() -> { | ||
| message = "Unused annotation processor ${advicePrinter.fromDeclaration(advice).trim()} should be removed" | ||
| ruleId = "dependencyanalysis.Processpr" | ||
| } | ||
|
|
||
| else -> { | ||
| error("Unknown advice type: $advice") | ||
| } | ||
| } | ||
|
|
||
| val location = projectAdvice.projectBuildFile?.let { buildFile -> | ||
| Location( | ||
| physicalLocation = PhysicalLocation( | ||
| artifactLocation = ArtifactLocation(uri = buildFile), | ||
| region = | ||
| Region( | ||
| startLine = sourcedAdvice.buildFileDeclarationLineNumber?.toLong(), | ||
| endLine = sourcedAdvice.buildFileDeclarationLineNumber?.toLong(), | ||
| ) | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| Result( | ||
| locations = listOfNotNull(location), | ||
| message = Message(text = message), | ||
| ruleID = ruleId | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| sarif = SarifSchema210( | ||
| schema = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json", | ||
| version = Version.The210, | ||
| runs = listOf( | ||
| Run( | ||
| results = dependencyResults + pluginResults, | ||
| tool = SARIF_TOOL, | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private val SARIF_TOOL = Tool( | ||
| driver = ToolComponent( | ||
| guid = "f9137358-f4fb-44f2-8300-39ca0b85fb77", | ||
| informationURI = "https://github.com/autonomousapps/dependency-analysis-gradle-plugin", | ||
| language = "en", | ||
| name = "dependency-analysis-gradle-plugin", | ||
| rules = listOf( | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.Add", | ||
| name = "Add", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "These transitive dependencies should be declared directly" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.Remove", | ||
| name = "Remove", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Unused dependencies which should be removed" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.Change", | ||
| name = "Change", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Existing dependencies which should be modified to be as indicated" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.ChangeRuntimeOnly", | ||
| name = "ChangeRuntimeOnly", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Dependencies which should be removed or changed to runtime-only" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.ChangeCompileOnly", | ||
| name = "ChangeCompileOnly", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Dependencies which could be compile-only" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.Processor", | ||
| name = "Processor", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Unused annotation processors that should be removed" | ||
| ) | ||
| ), | ||
| ReportingDescriptor( | ||
| id = "dependencyanalysis.Plugin", | ||
| name = "Plugin", | ||
| shortDescription = MultiformatMessageString( | ||
| text = "Unused plugins that can be removed" | ||
| ) | ||
| ), | ||
| ), | ||
| ) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.