Skip to content

Commit

Permalink
feat(helper-cli): Add a command to show insights into scan issues
Browse files Browse the repository at this point in the history
Assign scan issues to categories and output for each category the amount
of dependency affected by a scan issue of the respective category. Also
show a secondary count which disregards the version.

These statistics can be useful as a basis to make a rough estimate of
the effort needed to fix the scan issues.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Jul 11, 2024
1 parent 799acd1 commit 5cd3c4f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions helper-cli/src/main/kotlin/HelperMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ internal class HelperMain : CliktCommand(
ExtractRepositoryConfigurationCommand(),
GenerateTimeoutErrorResolutionsCommand(),
GetPackageLicensesCommand(),
GroupScanIssuesCommand(),
DownloadResultsFromPostgresCommand(),
ImportCopyrightGarbageCommand(),
ImportScanResultsCommand(),
Expand Down
113 changes: 113 additions & 0 deletions helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.helper.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file

import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.utils.common.expandTilde

class GroupScanIssuesCommand : CliktCommand(
help = "Shows the amount of affected dependencies for each scan issue category."
) {
private val ortFile by option(
"--ort-file", "-i",
help = "The ORT result file to read as input."
).convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }
.required()

override fun run() {
val ortResult = ortFile.readValue<OrtResult>()

Check warning on line 46 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L46

Added line #L46 was not covered by tests

val issues = ortResult.getScannerIssues(
omitExcluded = true,
omitResolved = true,
minSeverity = Severity.ERROR
).filter { (id, _) -> ortResult.isPackage(id) }

Check warning on line 52 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L48-L52

Added lines #L48 - L52 were not covered by tests

val issueCategoriesForId = issues.mapValues { issue ->
issue.value.mapTo(mutableSetOf()) { it.category }

Check warning on line 55 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L54-L55

Added lines #L54 - L55 were not covered by tests
}

val issueCategoriesForIdWithoutVersion = issueCategoriesForId.entries.groupBy(
{ it.key.copy(version = "") },
{ it.value }
).mapValues { it.value.flatten().toSet() }

Check warning on line 61 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L58-L61

Added lines #L58 - L61 were not covered by tests

val pkgCountsForIssueCategory = ScanIssueCategory.entries.associateWith { category ->
val numPackages = issueCategoriesForId.count { (_, categories) ->
category in categories

Check warning on line 65 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L63-L65

Added lines #L63 - L65 were not covered by tests
}

val numPackagesWithoutVersion = issueCategoriesForIdWithoutVersion.count { (_, categories) ->
category in categories

Check warning on line 69 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L68-L69

Added lines #L68 - L69 were not covered by tests
}

numPackages to numPackagesWithoutVersion

Check warning on line 72 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L72

Added line #L72 was not covered by tests
}

val stats = buildString {
pkgCountsForIssueCategory.entries.sortedByDescending { it.value.first }.forEach { (category, counts) ->
appendLine("$category: ${counts.first} / ${counts.second}")

Check warning on line 77 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L75-L77

Added lines #L75 - L77 were not covered by tests
}
}

print(stats)

Check warning on line 81 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L81

Added line #L81 was not covered by tests
}
}

private enum class ScanIssueCategory(
val regex: Regex

Check warning on line 86 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L85-L86

Added lines #L85 - L86 were not covered by tests
) {
PROVENANCE_INFO_MISSING(
"IOException: Could not resolve provenance for package '.*' for source code origins \\[.*\\]\\."

Check warning on line 89 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L88-L89

Added lines #L88 - L89 were not covered by tests
),
SCAN_TIMED_OUT(
"ERROR: Timeout after .* seconds while scanning file '.*'\\."

Check warning on line 92 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L91-L92

Added lines #L91 - L92 were not covered by tests
),
VCS_REVISION_NOT_FOUND(
".*Could not resolve revision.*Could not find any revision candidates.*"

Check warning on line 95 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L94-L95

Added lines #L94 - L95 were not covered by tests
),
VCS_PATH_NOT_EXISTENT(
".*Could not resolve provenance.*because the requested VCS path.*does not exist."

Check warning on line 98 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L97-L98

Added lines #L97 - L98 were not covered by tests
),
VCS_TYPE_UNKNOWN(
".*Could not determine VCS for type.*"

Check warning on line 101 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L100-L101

Added lines #L100 - L101 were not covered by tests
),
OTHER("");

Check warning on line 103 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L103

Added line #L103 was not covered by tests

constructor(pattern: String) : this(pattern.toRegex(setOf(RegexOption.DOT_MATCHES_ALL, RegexOption.MULTILINE)))

Check warning on line 105 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L105

Added line #L105 was not covered by tests

companion object {
fun forIssue(issue: Issue) = entries.find { it.regex.matches(issue.message) } ?: OTHER
}
}

private val Issue.category: ScanIssueCategory
get() = ScanIssueCategory.forIssue(this)

Check warning on line 113 in helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt

View check run for this annotation

Codecov / codecov/patch

helper-cli/src/main/kotlin/commands/GroupScanIssuesCommand.kt#L113

Added line #L113 was not covered by tests

0 comments on commit 5cd3c4f

Please sign in to comment.