forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdviceProvider.kt
85 lines (76 loc) · 2.94 KB
/
AdviceProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright (C) 2020-2021 Bosch.IO GmbH
* Copyright (C) 2021 HERE Europe B.V.
*
* 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.advisor
import java.time.Instant
import org.ossreviewtoolkit.model.AdvisorDetails
import org.ossreviewtoolkit.model.AdvisorResult
import org.ossreviewtoolkit.model.AdvisorSummary
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.utils.common.collectMessagesAsString
import org.ossreviewtoolkit.utils.core.showStackTrace
/**
* An abstract class that represents a service that can retrieve any kind of advice information
* for a list of given [Package]s. Examples of such information can be security vulnerabilities, known defects,
* or code analysis results.
*/
abstract class AdviceProvider(val providerName: String) {
/**
* For a given list of [Package]s, retrieve findings and return a map that associates each package with a list
* of [AdvisorResult]s. Needs to be implemented by child classes.
*/
abstract suspend fun retrievePackageFindings(
packages: List<Package>
): Map<Package, List<AdvisorResult>>
/**
* An object with detail information about this [AdviceProvider].
*/
abstract val details: AdvisorDetails
/**
* A generic method that creates a failed [AdvisorResult] for [Package]s if there was an issue
* constructing the provider-specific information.
*/
protected fun createFailedResults(
startTime: Instant,
packages: List<Package>,
t: Throwable
): Map<Package, List<AdvisorResult>> {
val endTime = Instant.now()
t.showStackTrace()
val failedResults = listOf(
AdvisorResult(
vulnerabilities = emptyList(),
advisor = details,
summary = AdvisorSummary(
startTime = startTime,
endTime = endTime,
issues = listOf(
createAndLogIssue(
source = providerName,
message = "Failed to retrieve findings from $providerName: " +
t.collectMessagesAsString()
)
)
)
)
)
return packages.associateWith { failedResults }
}
}