7 namespace Fossology\UI\Api\Controllers;
17 use Symfony\Component\HttpFoundation\Request;
37 if (empty($SysConf[
'SYSCONFIG'][
'EnableOsselotReuse']) ||
38 !$SysConf[
'SYSCONFIG'][
'EnableOsselotReuse']) {
39 $error =
new Info(501,
"OSSelot integration is disabled", InfoType::ERROR);
40 return $response->withJson($error->getArray(), $error->getCode());
43 $package = $args[
'package'] ??
"";
44 if (strlen(
trim($package)) == 0) {
50 $versions = $helper->getVersions($package);
52 if (empty($versions)) {
53 $error =
new Info(404,
"No curated versions found for '$package'", InfoType::ERROR);
54 return $response->withJson($error->getArray(), $error->getCode());
57 rsort($versions, SORT_NATURAL);
59 return $response->withJson($versions, 200);
62 $error =
new Info(502,
"OSSelot unavailable, try later", InfoType::ERROR);
63 return $response->withJson($error->getArray(), $error->getCode());
79 if (empty($SysConf[
'SYSCONFIG'][
'EnableOsselotReuse']) ||
80 !$SysConf[
'SYSCONFIG'][
'EnableOsselotReuse']) {
81 $err =
new Info(501,
"OSSelot integration is disabled", InfoType::ERROR);
82 return $response->withJson($err->getArray(), $err->getCode());
85 $uploadId = (int)($args[
'id'] ?? 0);
89 $pkg = $body[
'package'] ??
null;
90 $ver = $body[
'version'] ??
null;
91 $options = $body[
'options'] ??
null;
93 if (!$pkg || !$ver || !is_array($options)) {
97 'addLicenseInfoFromInfoInFile',
98 'addLicenseInfoFromConcluded',
99 'addConcludedAsDecisions',
103 foreach ($requiredOptions as $optKey) {
104 if (!array_key_exists($optKey, $options)) {
109 $jobDao = $this->restHelper->getJobDao();
110 if (method_exists($jobDao,
'hasPendingReportImport') &&
111 $jobDao->hasPendingReportImport($uploadId)) {
112 $err =
new Info(409,
"An import job is already in progress for this upload", InfoType::ERROR);
113 return $response->withJson($err->getArray(), $err->getCode());
118 $cachedPath = $helper->fetchSpdxFile($pkg, $ver);
120 if (!$cachedPath || !is_file($cachedPath) || !is_readable($cachedPath)) {
121 $err =
new Info(404,
"No curated SPDX report found for '$pkg' version '$ver'", InfoType::ERROR);
122 return $response->withJson($err->getArray(), $err->getCode());
125 $fileBase = $SysConf[
'FOSSOLOGY'][
'path'] .
"/ReportImport/";
126 if (!is_dir($fileBase) && !mkdir($fileBase, 0755,
true)) {
127 throw new \RuntimeException(
'Failed to create ReportImport directory');
130 $originalName = basename($cachedPath);
131 if (!str_ends_with($originalName,
'.rdf.xml')) {
132 $baseName = pathinfo($originalName, PATHINFO_FILENAME);
133 $originalName = str_ends_with($originalName,
'.rdf') ?
134 $baseName .
'.rdf.xml' : $originalName .
'.rdf.xml';
137 $targetFile = time() .
'_' . random_int(0, getrandmax()) .
'_osselot_' . $originalName;
138 $targetPath = $fileBase . $targetFile;
140 if (!copy($cachedPath, $targetPath)) {
141 throw new \RuntimeException(
'Failed to copy SPDX file to target location');
144 $reportImportAgent =
plugin_find(
'agent_reportImport');
145 if (!$reportImportAgent || !method_exists($reportImportAgent,
'addReport') ||
146 !method_exists($reportImportAgent,
'setAdditionalJqCmdArgs') ||
147 !method_exists($reportImportAgent,
'AgentAdd')) {
148 throw new \RuntimeException(
'ReportImport agent not available or missing required methods');
151 $importRequest =
new Request();
153 $addNewLicensesAs = $options[
'addNewLicensesAs'] ??
'candidate';
154 if (!in_array($addNewLicensesAs, [
'candidate',
'approved',
'rejected'],
true)) {
155 $addNewLicensesAs =
'candidate';
157 $importRequest->request->set(
'addNewLicensesAs', $addNewLicensesAs);
160 'addLicenseInfoFromInfoInFile' => $options[
'addLicenseInfoFromInfoInFile'],
161 'addLicenseInfoFromConcluded' => $options[
'addLicenseInfoFromConcluded'],
162 'addConcludedAsDecisions' => $options[
'addConcludedAsDecisions'],
163 'addConcludedAsDecisionsOverwrite' => $options[
'addConcludedAsDecisionsOverwrite'] ??
false,
164 'addConcludedAsDecisionsTBD' => $options[
'addConcludedAsDecisionsTBD'] ??
false,
165 'addCopyrights' => $options[
'addCopyrights']
168 foreach ($booleanOptions as $key => $value) {
169 $importRequest->request->set($key, $value ?
'true' :
'false');
172 $licenseMatch = $options[
'licenseMatch'] ??
'spdxid';
173 if (!in_array($licenseMatch, [
'spdxid',
'name',
'text'],
true)) {
174 $licenseMatch =
'spdxid';
176 $importRequest->request->set(
'licenseMatch', $licenseMatch);
178 $jqCmdArgs = $reportImportAgent->addReport($targetFile);
179 $additionalArgs = $reportImportAgent->setAdditionalJqCmdArgs($importRequest);
180 $jqCmdArgs .= $additionalArgs;
185 $jobId = JobAddJob($userId, $groupId,
"OSSelot Import", $uploadId);
188 $dependencies = array();
189 $jobQueueId = $reportImportAgent->AgentAdd($jobId, $uploadId, $error, $dependencies, $jqCmdArgs);
191 if ($jobQueueId < 0) {
192 if (file_exists($targetPath)) {
195 throw new \RuntimeException(
"Cannot schedule import job: " . $error);
198 $info =
new Info(202,
"Import job scheduled successfully", InfoType::INFO);
199 $responseData = $info->getArray();
200 $responseData[
'jobId'] = intval($jobQueueId);
202 return $response->withJson($responseData, $info->getCode());
204 }
catch (\InvalidArgumentException $e) {
205 $err =
new Info(400, $e->getMessage(), InfoType::ERROR);
206 return $response->withJson($err->getArray(), $err->getCode());
207 }
catch (\RuntimeException $e) {
208 if (isset($targetPath) && file_exists($targetPath)) {
212 if (strpos($e->getMessage(),
'Could not fetch') !==
false ||
213 strpos($e->getMessage(),
'No curated') !==
false) {
214 $err =
new Info(404,
"No curated SPDX report found for '$pkg' version '$ver'", InfoType::ERROR);
216 $err =
new Info(502,
"OSSelot service unavailable, try later", InfoType::ERROR);
218 return $response->withJson($err->getArray(), $err->getCode());
220 if (isset($targetPath) && file_exists($targetPath)) {
224 error_log(
"OSSelot import error: " . $e->getMessage());
225 $err =
new Info(502,
"OSSelot service unavailable, try later", InfoType::ERROR);
226 return $response->withJson($err->getArray(), $err->getCode());
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
Controller for OSSelot REST API endpoints.
importOsselotReport($request, $response, $args)
getPackageVersions($request, $response, $args)
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
Different type of infos provided by REST.
Info model to contain general error and return values.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
char * trim(char *ptext)
Trimming whitespace.