|
| 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.pipelines import Pipeline |
| 24 | +from scanpipe.pipes import repo_health |
| 25 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_ECOSYSTEM |
| 26 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_METRICS_EXECUTABLE |
| 27 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_OPENSEARCH_INDEX |
| 28 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_OPENSEARCH_PASSWORD |
| 29 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_OPENSEARCH_URL |
| 30 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_OPENSEARCH_USERNAME |
| 31 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_PASSWORD |
| 32 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_PROJECT |
| 33 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_URL |
| 34 | +from scanpipe.pipes.repo_health import GRIMOIRELAB_USERNAME |
| 35 | + |
| 36 | + |
| 37 | +class ScanRepoHealth(Pipeline): |
| 38 | + """Run a Repo Health scan to extract repository metrics and health score.""" |
| 39 | + |
| 40 | + results_url = "/project/{slug}/resources/?extra_data=grimoire_data" |
| 41 | + download_inputs = False |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def steps(cls): |
| 45 | + return ( |
| 46 | + cls.get_repo_url_input, |
| 47 | + cls.collect_and_store_grimoire_metric, |
| 48 | + cls.format_metrics_output, |
| 49 | + ) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def get_availability(cls): |
| 53 | + if not ( |
| 54 | + GRIMOIRELAB_METRICS_EXECUTABLE |
| 55 | + and GRIMOIRELAB_OPENSEARCH_INDEX |
| 56 | + and GRIMOIRELAB_OPENSEARCH_PASSWORD |
| 57 | + and GRIMOIRELAB_OPENSEARCH_URL |
| 58 | + and GRIMOIRELAB_OPENSEARCH_USERNAME |
| 59 | + and GRIMOIRELAB_PASSWORD |
| 60 | + and GRIMOIRELAB_URL |
| 61 | + and GRIMOIRELAB_USERNAME |
| 62 | + and GRIMOIRELAB_ECOSYSTEM |
| 63 | + and GRIMOIRELAB_PROJECT |
| 64 | + ): |
| 65 | + return "Grimoirelab is not configured." |
| 66 | + |
| 67 | + def get_repo_url_input(self): |
| 68 | + """Validate and extract the repository URL from the project's input sources""" |
| 69 | + self.repo_url = repo_health.get_repo_url_input(project=self.project) |
| 70 | + |
| 71 | + def collect_and_store_grimoire_metric(self): |
| 72 | + """ |
| 73 | + Run the grimoirelab-metrics command against the input source. |
| 74 | + Save the generated metrics JSON to the project output directory. |
| 75 | + """ |
| 76 | + self.metrics_output_path = repo_health.collect_and_store_grimoire_metric( |
| 77 | + project=self.project, repo_url=self.repo_url, logger=self.log |
| 78 | + ) |
| 79 | + |
| 80 | + def format_metrics_output(self): |
| 81 | + """ |
| 82 | + Format the GrimoireLab metrics output by extracting the repository URL, |
| 83 | + score, and metrics from the generated JSON and overwriting it with a |
| 84 | + simplified structure, and updating the project's extra data. |
| 85 | + """ |
| 86 | + repo_health.format_metrics_output( |
| 87 | + project=self.project, metrics_output_path=self.metrics_output_path |
| 88 | + ) |
0 commit comments