|
16 | 16 | from django.core import management |
17 | 17 | from django.core.exceptions import ObjectDoesNotExist |
18 | 18 | from django.core.mail import send_mail |
19 | | -from django.db import transaction |
20 | | -from django.template.defaultfilters import pluralize |
21 | 19 |
|
22 | 20 | from django_rq import job |
23 | 21 |
|
@@ -118,122 +116,6 @@ def scancodeio_submit_scan(uris, user_uuid, dataspace_uuid): |
118 | 116 | logger.info(f'uri="{uri}" is not reachable.') |
119 | 117 |
|
120 | 118 |
|
121 | | -@job |
122 | | -def scancodeio_submit_project(scancodeproject_uuid, user_uuid, pipeline_name): |
123 | | - """Submit the provided SBOM file to ScanCode.io as an asynchronous task.""" |
124 | | - logger.info( |
125 | | - f"Entering scancodeio_submit_project task with " |
126 | | - f"scancodeproject_uuid={scancodeproject_uuid} user_uuid={user_uuid} " |
127 | | - f"pipeline_name={pipeline_name}" |
128 | | - ) |
129 | | - |
130 | | - DejacodeUser = apps.get_model("dje", "DejacodeUser") |
131 | | - ScanCodeProject = apps.get_model("product_portfolio", "scancodeproject") |
132 | | - scancode_project = ScanCodeProject.objects.get(uuid=scancodeproject_uuid) |
133 | | - |
134 | | - try: |
135 | | - user = DejacodeUser.objects.get(uuid=user_uuid) |
136 | | - except ObjectDoesNotExist: |
137 | | - logger.error(f"[scancodeio_submit_project]: User uuid={user_uuid} does not exists.") |
138 | | - return |
139 | | - |
140 | | - scancodeio = ScanCodeIO(user.dataspace) |
141 | | - |
142 | | - # Create a Project instance on ScanCode.io without immediate execution of the |
143 | | - # pipeline. This allows to get instant feedback from ScanCode.io about the Project |
144 | | - # creation status and its related data, even in SYNC mode. |
145 | | - response = scancodeio.submit_project( |
146 | | - project_name=scancodeproject_uuid, |
147 | | - pipeline_name=pipeline_name, |
148 | | - file_location=scancode_project.input_file.path, |
149 | | - user_uuid=user_uuid, |
150 | | - execute_now=False, |
151 | | - ) |
152 | | - |
153 | | - if not response: |
154 | | - logger.info("Error submitting the file to ScanCode.io server") |
155 | | - scancode_project.status = ScanCodeProject.Status.FAILURE |
156 | | - msg = "- Error: File could not be submitted to ScanCode.io" |
157 | | - scancode_project.append_to_log(msg, save=True) |
158 | | - return |
159 | | - |
160 | | - logger.info("Update the ScanCodeProject instance") |
161 | | - scancode_project.status = ScanCodeProject.Status.SUBMITTED |
162 | | - scancode_project.project_uuid = response.get("uuid") |
163 | | - msg = "- File submitted to ScanCode.io for inspection" |
164 | | - scancode_project.append_to_log(msg, save=True) |
165 | | - |
166 | | - # Delay the execution of the pipeline after the ScancodeProject instance was |
167 | | - # properly saved and committed in order to avoid any race conditions. |
168 | | - if runs := response.get("runs"): |
169 | | - logger.info("Start the pipeline run") |
170 | | - transaction.on_commit(lambda: scancodeio.start_pipeline(run_url=runs[0]["url"])) |
171 | | - |
172 | | - |
173 | | -@job("default", timeout=1200) |
174 | | -def pull_project_data_from_scancodeio(scancodeproject_uuid): |
175 | | - """ |
176 | | - Pull Project data from ScanCode.io as an asynchronous task for the provided |
177 | | - `scancodeproject_uuid`. |
178 | | - """ |
179 | | - logger.info( |
180 | | - f"Entering pull_project_data_from_scancodeio task with " |
181 | | - f"scancodeproject_uuid={scancodeproject_uuid}" |
182 | | - ) |
183 | | - |
184 | | - ScanCodeProject = apps.get_model("product_portfolio", "scancodeproject") |
185 | | - scancode_project = ScanCodeProject.objects.get(uuid=scancodeproject_uuid) |
186 | | - |
187 | | - # Make sure the import is not already in progress, |
188 | | - # or that the import has not completed yet. |
189 | | - if not scancode_project.can_start_import: |
190 | | - logger.error("Cannot start import") |
191 | | - return |
192 | | - |
193 | | - # Update the status to prevent from starting the task again |
194 | | - ScanCodeProject.objects.filter(uuid=scancode_project.uuid).update( |
195 | | - status=ScanCodeProject.Status.IMPORT_STARTED |
196 | | - ) |
197 | | - |
198 | | - if scancode_project.type == scancode_project.ProjectType.LOAD_SBOMS: |
199 | | - notification_verb = "Import SBOM" |
200 | | - else: |
201 | | - notification_verb = "Import packages from ScanCode.io" |
202 | | - |
203 | | - try: |
204 | | - created, existing, errors = scancode_project.import_data_from_scancodeio() |
205 | | - except Exception as e: |
206 | | - scancode_project.status = ScanCodeProject.Status.FAILURE |
207 | | - scancode_project.append_to_log(message=str(e), save=True) |
208 | | - scancode_project.notify(verb=notification_verb, description="Import failed.") |
209 | | - return |
210 | | - |
211 | | - scancode_project.status = ScanCodeProject.Status.SUCCESS |
212 | | - |
213 | | - for object_type, values in created.items(): |
214 | | - object_type_plural = f"{object_type}{pluralize(values)}" |
215 | | - object_type_plural = object_type_plural.replace("dependencys", "dependencies") |
216 | | - msg = f"- Imported {len(values)} {object_type_plural}." |
217 | | - scancode_project.append_to_log(msg) |
218 | | - |
219 | | - for object_type, values in existing.items(): |
220 | | - object_type_plural = f"{object_type}{pluralize(values)}" |
221 | | - object_type_plural = object_type_plural.replace("dependencys", "dependencies") |
222 | | - reason = "already available in the dataspace" |
223 | | - if object_type == "dependency": |
224 | | - reason = "already defined on the product" |
225 | | - msg = f"- {len(values)} {object_type_plural} skipped: {reason}." |
226 | | - scancode_project.append_to_log(msg) |
227 | | - |
228 | | - for object_type, values in errors.items(): |
229 | | - msg = f"- {len(values)} {object_type} error{pluralize(values)} occurred during import." |
230 | | - scancode_project.append_to_log(msg) |
231 | | - |
232 | | - scancode_project.save() |
233 | | - description = "\n".join(scancode_project.import_log) |
234 | | - scancode_project.notify(verb=notification_verb, description=description) |
235 | | - |
236 | | - |
237 | 119 | @job("default", timeout="3h") |
238 | 120 | def update_vulnerabilities(): |
239 | 121 | """Fetch vulnerabilities for all Dataspaces that enable vulnerablecodedb access.""" |
|
0 commit comments