|
| 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 = ( |
| 45 | + package.scores.filter(scoring_tool="ossf-scorecard") |
| 46 | + .order_by("-score_date") |
| 47 | + .first() |
| 48 | + ) |
| 49 | + |
| 50 | + if not latest_score or latest_score.score is None: |
| 51 | + continue |
| 52 | + |
| 53 | + try: |
| 54 | + score = float(latest_score.score) |
| 55 | + alert = scorecard_policy.get_alert_for_score(score) |
| 56 | + except Exception: |
| 57 | + alert = "error" |
| 58 | + |
| 59 | + order = {"ok": 0, "warning": 1, "error": 2} |
| 60 | + if worst_alert is None or order[alert] > order.get(worst_alert, -1): |
| 61 | + worst_alert = alert |
| 62 | + |
| 63 | + if worst_alert is not None: |
| 64 | + project.update_extra_data({"scorecard_compliance_alert": worst_alert}) |
0 commit comments