|
| 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 | +import json |
| 24 | +import subprocess |
| 25 | + |
| 26 | +from scanpipe.pipelines import Pipeline |
| 27 | +from scanpipe.pipelines.metrics_model import npmModel |
| 28 | + |
| 29 | + |
| 30 | +class ScanGrimoirelab(Pipeline): |
| 31 | + results_url = "/project/{slug}/resources/?extra_data=grimoire_data" |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def steps(cls): |
| 35 | + return ( |
| 36 | + cls.collect_grimoire_metric, |
| 37 | + cls.compute_and_store_metric_score, |
| 38 | + ) |
| 39 | + |
| 40 | + def collect_grimoire_metric(self): |
| 41 | + metrics_output_path = self.project.get_output_file_path("metrics", "json") |
| 42 | + grimoire_config_path = self.project.get_output_file_path( |
| 43 | + "grimoire_config", "json" |
| 44 | + ) |
| 45 | + repo_url = "https://github.com/aboutcode-org/fetchcode.git" |
| 46 | + |
| 47 | + grimoirelab_config = { |
| 48 | + "spdxVersion": "SPDX-2.3", |
| 49 | + "dataLicense": "CC0-1.0", |
| 50 | + "SPDXID": "SPDXRef-DOCUMENT", |
| 51 | + "name": "GrimoireLab Analysis", |
| 52 | + "documentNamespace": "http://spdx.org/spdxdocs/grimoirelab-metrics", |
| 53 | + "creationInfo": { |
| 54 | + "creators": ["Tool: manual-conversion"], |
| 55 | + "created": "2026-07-31T21:41:21Z", |
| 56 | + }, |
| 57 | + "packages": [ |
| 58 | + { |
| 59 | + "name": "grimoirelab", |
| 60 | + "SPDXID": "SPDXRef-Package-grimoirelab", |
| 61 | + "downloadLocation": repo_url, |
| 62 | + } |
| 63 | + ], |
| 64 | + } |
| 65 | + |
| 66 | + with open(grimoire_config_path, "w") as f: |
| 67 | + json.dump(grimoirelab_config, f, indent=2) |
| 68 | + |
| 69 | + GRIMOIRELAB_METRICS_EXECUTABLE = ( |
| 70 | + "/home/ziad-hany/PycharmProjects/healthycode/venv/bin/grimoirelab-metrics" |
| 71 | + ) |
| 72 | + |
| 73 | + GRIMOIRELAB_URL = "http://localhost:8000" |
| 74 | + GRIMOIRELAB_USERNAME = "admin" |
| 75 | + GRIMOIRELAB_PASSWORD = "admin" |
| 76 | + |
| 77 | + OPENSEARCH_URL = "https://localhost:9200" |
| 78 | + OPENSEARCH_INDEX = "events" |
| 79 | + OPENSEARCH_USERNAME = "admin" |
| 80 | + OPENSEARCH_PASSWORD = "GrimoireLab.1" |
| 81 | + |
| 82 | + FROM_DATE = "2023-01-01" |
| 83 | + TO_DATE = "2026-06-19" |
| 84 | + |
| 85 | + REPOSITORY_TIMEOUT = "3600" |
| 86 | + |
| 87 | + CODE_FILE_PATTERN = r"\.py$|\.js$" |
| 88 | + BINARY_FILE_PATTERN = r"\.exe$|\.tar$" |
| 89 | + |
| 90 | + PONY_THRESHOLD = "0.5" |
| 91 | + ELEPHANT_THRESHOLD = "0.5" |
| 92 | + DEVELOPER_CATEGORIES_THRESHOLDS = ["0.8", "0.95"] |
| 93 | + |
| 94 | + cmd = [ |
| 95 | + GRIMOIRELAB_METRICS_EXECUTABLE, |
| 96 | + str(grimoire_config_path), |
| 97 | + "--grimoirelab-url", |
| 98 | + GRIMOIRELAB_URL, |
| 99 | + "--grimoirelab-user", |
| 100 | + GRIMOIRELAB_USERNAME, |
| 101 | + "--grimoirelab-password", |
| 102 | + GRIMOIRELAB_PASSWORD, |
| 103 | + "--opensearch-url", |
| 104 | + OPENSEARCH_URL, |
| 105 | + "--opensearch-index", |
| 106 | + OPENSEARCH_INDEX, |
| 107 | + "--opensearch-user", |
| 108 | + OPENSEARCH_USERNAME, |
| 109 | + "--opensearch-password", |
| 110 | + OPENSEARCH_PASSWORD, |
| 111 | + "--from-date", |
| 112 | + FROM_DATE, |
| 113 | + "--to-date", |
| 114 | + TO_DATE, |
| 115 | + "--repository-timeout", |
| 116 | + REPOSITORY_TIMEOUT, |
| 117 | + "--code-file-pattern", |
| 118 | + CODE_FILE_PATTERN, |
| 119 | + "--binary-file-pattern", |
| 120 | + BINARY_FILE_PATTERN, |
| 121 | + "--pony-threshold", |
| 122 | + PONY_THRESHOLD, |
| 123 | + "--elephant-threshold", |
| 124 | + ELEPHANT_THRESHOLD, |
| 125 | + "--dev-categories-thresholds", |
| 126 | + *DEVELOPER_CATEGORIES_THRESHOLDS, |
| 127 | + "--output", |
| 128 | + str(metrics_output_path), |
| 129 | + ] |
| 130 | + |
| 131 | + try: |
| 132 | + subprocess.run( |
| 133 | + cmd, |
| 134 | + capture_output=True, |
| 135 | + text=True, |
| 136 | + check=True, |
| 137 | + ) |
| 138 | + self.log(f"Metrics successfully saved to {metrics_output_path}") |
| 139 | + with open(metrics_output_path) as f: |
| 140 | + self.metrics = json.load(f) |
| 141 | + |
| 142 | + except subprocess.CalledProcessError as e: |
| 143 | + self.log(f"failed with exit code {e.returncode}") |
| 144 | + except FileNotFoundError: |
| 145 | + self.log( |
| 146 | + "Error: 'grimoirelab-metrics' command not found. Is it installed and on your PATH?" |
| 147 | + ) |
| 148 | + raise |
| 149 | + |
| 150 | + def compute_and_store_metric_score(self): |
| 151 | + model = npmModel() |
| 152 | + probability = model.calculate_score(self.metrics) |
| 153 | + status = "Healthy" if probability >= 0.5 else "Unhealthy" |
| 154 | + |
| 155 | + self.log(f"Repository Health: {status}, Probability: {probability:.2%}") |
| 156 | + score_data = { |
| 157 | + "status": status, |
| 158 | + "probability": probability, |
| 159 | + "metrics": self.metrics, |
| 160 | + } |
| 161 | + |
| 162 | + score_output_path = self.project.get_output_file_path("results", "json") |
| 163 | + with open(score_output_path, "w") as f: |
| 164 | + json.dump(score_data, f, indent=2) |
| 165 | + |
| 166 | + return score_data |
0 commit comments