Skip to content

Commit 8c9dc8f

Browse files
committed
create a new pipe for compliancing
Signed-off-by: NucleonGodX <[email protected]>
1 parent 5ab30e7 commit 8c9dc8f

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

scanpipe/pipelines/fetch_scores.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from scanpipe.models import DiscoveredPackageScore
2727
from scanpipe.pipelines import Pipeline
28-
from scanpipe.pipes.compliance_thresholds import get_project_scorecard_thresholds
28+
from scanpipe.pipes import scorecard_compliance
2929

3030

3131
class FetchScores(Pipeline):
@@ -50,6 +50,7 @@ def steps(cls):
5050
return (
5151
cls.check_scorecode_service_availability,
5252
cls.fetch_packages_scorecode_info,
53+
cls.evaluate_compliance_alerts,
5354
)
5455

5556
def check_scorecode_service_availability(self):
@@ -59,26 +60,13 @@ def check_scorecode_service_availability(self):
5960

6061
def fetch_packages_scorecode_info(self):
6162
"""Fetch ScoreCode information for each of the project's discovered packages."""
62-
scorecard_policy = get_project_scorecard_thresholds(self.project)
63-
worst_alert = None
64-
6563
for package in self.project.discoveredpackages.all():
6664
if scorecard_data := ossf_scorecard.fetch_scorecard_info(package=package):
6765
DiscoveredPackageScore.create_from_package_and_scorecard(
6866
scorecard_data=scorecard_data,
6967
package=package,
7068
)
7169

72-
if scorecard_policy and scorecard_data.score is not None:
73-
try:
74-
score = float(scorecard_data.score)
75-
alert = scorecard_policy.get_alert_for_score(score)
76-
except Exception:
77-
alert = "error"
78-
79-
order = {"ok": 0, "warning": 1, "error": 2}
80-
if worst_alert is None or order[alert] > order.get(worst_alert, -1):
81-
worst_alert = alert
82-
83-
if worst_alert is not None:
84-
self.project.update_extra_data({"scorecard_compliance_alert": worst_alert})
70+
def evaluate_compliance_alerts(self):
71+
"""Evaluate scorecard compliance alerts for the project."""
72+
scorecard_compliance.evaluate_scorecard_compliance(self.project)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/aboutcode-org/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
22+
23+
from scanpipe.pipes.compliance_thresholds import get_project_scorecard_thresholds
24+
25+
26+
def evaluate_scorecard_compliance(project):
27+
"""
28+
Evaluate scorecard compliance for all discovered packages in the project.
29+
30+
This function checks OpenSSF Scorecard scores against project-defined
31+
thresholds and determines the worst compliance alert level across all packages.
32+
Updates the project's extra_data with the overall compliance status.
33+
"""
34+
scorecard_policy = get_project_scorecard_thresholds(project)
35+
if not scorecard_policy:
36+
return
37+
38+
worst_alert = None
39+
packages_with_scores = project.discoveredpackages.filter(
40+
scores__scoring_tool="ossf-scorecard"
41+
).distinct()
42+
43+
for package in packages_with_scores:
44+
latest_score = package.scores.filter(
45+
scoring_tool="ossf-scorecard"
46+
).order_by("-score_date").first()
47+
48+
if not latest_score or latest_score.score is None:
49+
continue
50+
51+
try:
52+
score = float(latest_score.score)
53+
alert = scorecard_policy.get_alert_for_score(score)
54+
except Exception:
55+
alert = "error"
56+
57+
order = {"ok": 0, "warning": 1, "error": 2}
58+
if worst_alert is None or order[alert] > order.get(worst_alert, -1):
59+
worst_alert = alert
60+
61+
if worst_alert is not None:
62+
project.update_extra_data({"scorecard_compliance_alert": worst_alert})

0 commit comments

Comments
 (0)