From 8ab74fc1cc462531827b3328d90090f486b4278a Mon Sep 17 00:00:00 2001 From: Sarai Misidjan Date: Mon, 20 Nov 2023 19:25:55 +0100 Subject: [PATCH 1/4] opencatalogi fix --- Installation/Schema/Organisation.schema.json | 6 +- src/Service/EnrichOrganizationService.php | 33 ++-- src/Service/GithubApiService.php | 171 ++++++++++++++----- 3 files changed, 149 insertions(+), 61 deletions(-) diff --git a/Installation/Schema/Organisation.schema.json b/Installation/Schema/Organisation.schema.json index 287b5f19..cdc9e218 100644 --- a/Installation/Schema/Organisation.schema.json +++ b/Installation/Schema/Organisation.schema.json @@ -2,7 +2,7 @@ "title": "Organisation", "$id": "https://opencatalogi.nl/oc.organisation.schema.json", "$schema": "https://docs.commongateway.nl/schemas/Entity.schema.json", - "version": "0.1.5", + "version": "0.1.6", "type": "object", "description": "extension of the publiccode standard ", "properties": { @@ -85,6 +85,10 @@ }, "catalogus": { "$ref": "https://opencatalogi.nl/oc.catalogus.schema.json" + }, + "opencatalogiRepo": { + "type": "string", + "description": "The url of the opencatalogi file." } } } diff --git a/src/Service/EnrichOrganizationService.php b/src/Service/EnrichOrganizationService.php index d788a9bd..a6c7eee9 100644 --- a/src/Service/EnrichOrganizationService.php +++ b/src/Service/EnrichOrganizationService.php @@ -105,23 +105,30 @@ public function enrichOrganization(ObjectEntity $organization): ObjectEntity $organizationArray = $this->githubApiService->getOrganization(trim($githubPath, '/'), $source); } - if (isset($organizationArray) === false - || isset($organizationArray) === true - && $organizationArray === null - ) { - return $organization; + if ($organization->getValue('type') === 'User') { + // Get the organization from the github api. + $organizationArray = $this->githubApiService->getUser(trim($githubPath, '/'), $source); } - // If we get an empty string we set the description from the github api. - if ($organization->getValue('description') !== false - || $organization->getValue('description') !== null - ) { - $organization->hydrate(['description' => $organizationArray['description']]); - $this->entityManager->persist($organization); - $this->entityManager->flush(); + $opencatalogiUrl = $organization->getValue('opencatalogiRepo'); + $path = trim(\Safe\parse_url($opencatalogiUrl)['path'], '/'); + + // Call the search/code endpoint for publiccode files in this repository. + $queryConfig['query'] = ['q' => "filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; + $opencatalogiFiles = $this->githubApiService->getFilesFromRepo($source, $queryConfig); + + $opencatalogiNames = [ + 'opencatalogi.yaml', + 'opencatalogi.yml', + ]; + + foreach ($opencatalogiFiles as $item) { + if (in_array($item['name'], $opencatalogiNames) === true) { + $organization = $this->githubApiService->handleOpencatalogiFile($organizationArray, $source, $item); + } } - $this->pluginLogger->debug($organization->getName().' succesfully updated the organization with a description.'); + $this->pluginLogger->debug($organization->getName().' succesfully updated the organization with the opencatalogi file.'); return $organization; diff --git a/src/Service/GithubApiService.php b/src/Service/GithubApiService.php index 518d985f..c97e4413 100644 --- a/src/Service/GithubApiService.php +++ b/src/Service/GithubApiService.php @@ -157,6 +157,7 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr return $this->data; }//end if + $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); // Do we have the api key set of the source. if ($this->checkGithubAuth($source) === false @@ -187,7 +188,10 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr $repository = $repositorySync->getObject(); // Get the publiccode/opencatalogi files of the given repository. - $dataArray = $this->getFilesFromRepo($repositoryUrl, $source); + $path = trim(\Safe\parse_url($repositoryUrl)['path'], '/'); + // Call the search/code endpoint for publiccode files in this repository. + $queryConfig['query'] = ['q' => "filename:publiccode filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; + $dataArray = $this->getFilesFromRepo($source, $queryConfig); if ($dataArray !== null) { // Import the publiccode/opencatalogi files and connect it to the repository. $repository = $this->importRepoFiles($dataArray, $source, $repository); @@ -466,8 +470,9 @@ public function getConnectedComponents(ObjectEntity $organization, array $openca * @return ObjectEntity * @throws Exception */ - public function enrichOpencatalogiOrg(array $opencatalogiArray, array $opencatalogi, ObjectEntity $organization, Source $source): ObjectEntity + public function enrichOpencatalogiOrg(array $organizationArray, array $opencatalogi, ObjectEntity $organization, Source $source): ObjectEntity { + // If the opencatalogi logo is set to null or false we set the organization logo to null. if (key_exists('logo', $opencatalogi) === true && $opencatalogi['logo'] === false @@ -481,12 +486,33 @@ public function enrichOpencatalogiOrg(array $opencatalogiArray, array $opencatal if (key_exists('logo', $opencatalogi) === true && $opencatalogi['logo'] === '' ) { - $organization->hydrate(['logo' => $opencatalogiArray['repository']['owner']['avatar_url']]); + $organization->hydrate(['logo' => $organizationArray['avatar_url']]); } // If we don't get a opencatalogi logo we set the logo from the github api. if (key_exists('logo', $opencatalogi) === false) { - $organization->hydrate(['logo' => $opencatalogiArray['repository']['owner']['avatar_url']]); + $organization->hydrate(['logo' => $organizationArray['avatar_url']]); + } + + // If the opencatalogi description is set to null or false we set the organization description to null. + if (key_exists('description', $opencatalogi) === true + && $opencatalogi['description'] === false + || key_exists('description', $opencatalogi) === true + && $opencatalogi['description'] === null + ) { + $organization->hydrate(['description' => null]); + } + + // If we get an empty string we set the description from the github api. + if (key_exists('description', $opencatalogi) === true + && $opencatalogi['description'] === '' + ) { + $organization->hydrate(['description' => $organizationArray['description']]); + } + + // If we don't get a opencatalogi description we set the description from the github api. + if (key_exists('description', $opencatalogi) === false) { + $organization->hydrate(['description' => $organizationArray['description']]); } return $organization; @@ -504,20 +530,20 @@ public function enrichOpencatalogiOrg(array $opencatalogiArray, array $opencatal * @return ObjectEntity|null * @throws Exception */ - public function handleOpencatalogiFile(array $opencatalogiArray, Source $source, ObjectEntity $repository): ?ObjectEntity + public function handleOpencatalogiFile(array $organizationArray, Source $source, array $opencatalogiArray): ?ObjectEntity { $opencatalogiMapping = $this->resourceService->getMapping('https://api.github.com/oc.githubOpenCatalogiYamlToOrg.mapping.json', 'open-catalogi/open-catalogi-bundle'); $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); if ($opencatalogiMapping instanceof Mapping === false || $organizationSchema instanceof Entity === false ) { - return $repository; + return null; } // Get the ref query from the url. This way we can get the publiccode file with the raw.gitgubusercontent. - $publiccodeUrlQuery = \Safe\parse_url($opencatalogiArray['url'])['query']; + $opencatalogiUrlQuery = \Safe\parse_url($opencatalogiArray['url'])['query']; // Remove the ref= part of the query. - $urlReference = explode('ref=', $publiccodeUrlQuery)[1]; + $urlReference = explode('ref=', $opencatalogiUrlQuery)[1]; // Create the publiccode/opencatalogi url $opencatalogiUrl = "https://raw.githubusercontent.com/{$opencatalogiArray['repository']['full_name']}/{$urlReference}/{$opencatalogiArray['path']}"; @@ -526,45 +552,39 @@ public function handleOpencatalogiFile(array $opencatalogiArray, Source $source, // Check if the publiccodeYmlVersion is set otherwise this is not a valid file. if (key_exists('publiccodeYmlVersion', $opencatalogi) === false) { - return $repository; + return null; } - $opencatalogi['github'] = $opencatalogiArray['repository']['owner']['html_url']; - $opencatalogi['type'] = $opencatalogiArray['repository']['owner']['type']; - - // Find the sync with the source and opencatalogi url. - $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $opencatalogiArray['repository']['owner']['html_url']); + // Find the sync with the source and organization html_url. + $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $organizationArray['html_url']); // Check the sha of the sync with the url reference in the array. if ($this->syncService->doesShaMatch($organizationSync, $urlReference) === true) { - $repository->hydrate(['organisation' => $organizationSync->getObject()]); - - $this->entityManager->persist($repository); - $this->entityManager->flush(); - return $repository; + return null; } + $opencatalogi['github'] = $organizationArray['html_url']; + $opencatalogi['type'] = $organizationArray['type']; + $opencatalogi['opencatalogiRepo'] = $organizationSync->getObject()->getValue('opencatalogiRepo'); + $organizationSync->setMapping($opencatalogiMapping); // Synchronize the organization with the opencatalogi file. - $organizationSync = $this->syncService->synchronize($organizationSync, $opencatalogi, true); + $organizationSync = $this->syncService->synchronize($organizationSync, $opencatalogi); $this->entityManager->persist($organizationSync); $this->entityManager->flush(); // Get the softwareSupported/softwareOwned/softwareUsed repositories. - $organization = $this->getConnectedComponents($organizationSync->getObject(), $opencatalogi, $source, $opencatalogiArray); + $organization = $this->getConnectedComponents($organizationSync->getObject(), $opencatalogi, $source); // Enrich the opencatalogi organization with a logo and description. - $organization = $this->enrichOpencatalogiOrg($opencatalogiArray, $opencatalogi, $organization, $source); + $organization = $this->enrichOpencatalogiOrg($organizationArray, $opencatalogi, $organization, $source); - // Set the organization to the repository. - $repository->hydrate(['organisation' => $organization]); - - $this->entityManager->persist($repository); + $this->entityManager->persist($organization); $this->entityManager->flush(); - return $repository; + return $organization; }//end handleOpencatalogiFile() @@ -647,7 +667,11 @@ public function handlePubliccodeFile(array $publiccodeArray, Source $source, Obj // Create the publiccode/opencatalogi url $publiccodeUrl = "https://raw.githubusercontent.com/{$publiccodeArray['repository']['full_name']}/{$urlReference}/{$publiccodeArray['path']}"; - $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl); + // Create an unique sourceId for every publiccode that doesn't change. + // The urlReference and the git_url changes when the file changes. + $sourceId = "https://raw.githubusercontent.com/{$publiccodeArray['repository']['full_name']}/{$publiccodeArray['path']}"; + + $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl .' and source id: '.$sourceId); // Get the file from the usercontent or github api source $publiccode = $this->getFileFromRawUserContent($publiccodeUrl, $publiccodeArray['git_url']); @@ -669,7 +693,7 @@ public function handlePubliccodeFile(array $publiccodeArray, Source $source, Obj $publiccode['developmentStatus'] = 'obsolete'; } - $componentSync = $this->syncService->findSyncBySource($source, $componentSchema, $publiccodeArray['git_url']); + $componentSync = $this->syncService->findSyncBySource($source, $componentSchema, $sourceId); // Check the sha of the sync with the sha in the array. if ($this->syncService->doesShaMatch($componentSync, $urlReference) === true) { @@ -730,8 +754,25 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ // Check if the item name is the same as the openCatalogiNames array. // If so go the the function for the opencatalogi file. if (in_array($item['name'], $opencatalogiNames) === true) { + $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); $this->pluginLogger->info('The item is a opencatalogi file.'); - $repository = $this->handleOpencatalogiFile($item, $source, $repository); + + $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $item['repository']['owner']['html_url']); + $data = [ + 'name' => $item['repository']['full_name'], + 'type' => $item['repository']['owner']['type'], + 'github' => $item['repository']['owner']['html_url'], + 'opencatalogiRepo' => $item['repository']['html_url'] + ]; + $organizationSync = $this->syncService->synchronize($organizationSync, $data); + $this->entityManager->persist($organizationSync->getObject()); + $this->entityManager->persist($organizationSync); + $this->entityManager->flush(); + + $repository->hydrate(['organisation' => $organizationSync->getObject()]); + $this->entityManager->persist($repository); + $this->entityManager->flush(); + } // Check if the item name is the same as the publiccodeNames array. @@ -754,18 +795,18 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ * * @return array|null */ - public function getFileFromRawGithubApi(string $gitUrl): ?array + public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { - return $this->data; + return null; } // Get the path from the git url to make the call. $endpoint = \Safe\parse_url($gitUrl)['path']; try { - $response = $this->callService->call($source, $endpoint); + $response = $this->callService->call($source, $endpoint, 'GET', $query); } catch (Exception $e) { $this->pluginLogger->error('Error found trying to fetch '.$gitUrl.' '.$e->getMessage()); } @@ -790,23 +831,23 @@ public function getFileFromRawGithubApi(string $gitUrl): ?array // Decode the string. return $yamlEncoder->decode($content, 'yaml'); - }//end getFileFromRawGithubApi() + }//end getFileFromGithubApi() /** * This function gets the publiccode/opencatalogi file from the github user content. * * @param string $repositoryUrl The url of the repository - * @param string $gitUrl The git url of the repository + * @param string|null $gitUrl The git url of the repository * * @return array|null * @throws GuzzleException */ - public function getFileFromRawUserContent(string $repositoryUrl, string $gitUrl): ?array + public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl = null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubusercontent.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { - return $this->data; + return null; } // Get the path from the url to make the call. @@ -817,13 +858,18 @@ public function getFileFromRawUserContent(string $repositoryUrl, string $gitUrl) $this->pluginLogger->error('Error found trying to fetch '.$repositoryUrl.' '.$e->getMessage()); } - if (isset($response) === false) { + if (isset($response) === false + && $gitUrl !== null + ) { // Call the github api for the publiccode/opencatalogi files. - return $this->getFileFromRawGithubApi($gitUrl); + return $this->getFileFromGithubApi($gitUrl); } - return $this->callService->decodeResponse($source, $response, 'text/yaml'); + if (isset($response) === true) { + return $this->callService->decodeResponse($source, $response, 'text/yaml'); + } + return null; }//end getFileFromRawUserContent() @@ -871,22 +917,18 @@ public function getRepository(string $repositoryUrl, Source $source): ?array /** * Get the publiccode/opencatalogi files of the given repository * - * @param string $repositoryUrl The url of the repository. * @param Source $source The source to sync from. + * @param array $queryConfig The query config of the call. * * @return array|null The publiccode/opencatalogi files as array. */ - public function getFilesFromRepo(string $repositoryUrl, Source $source): ?array + public function getFilesFromRepo(Source $source, array $queryConfig): ?array { - $path = trim(\Safe\parse_url($repositoryUrl, PHP_URL_PATH), '/'); - - // Call the search/code endpoint for publiccode files in this repository. - $queryConfig['query'] = ['q' => "filename:publiccode filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; - // Find the publiccode.yaml file(s). try { $response = $this->callService->call($source, '/search/code', 'GET', $queryConfig); } catch (Exception $exception) { + var_dump($exception->getMessage()); $this->pluginLogger->error('Error found trying to fetch '.$source->getLocation().'/search/code'.' '.$exception->getMessage()); } @@ -1007,6 +1049,41 @@ public function getOrganizationRepos(string $name, Source $source): ?array }//end getOrganizationRepos() + /** + * Get a organization with type Organization from the github api. + * + * @param string $name The name of the organization. + * @param Source $source The source to sync from. + * + * @return array|null The imported organization as array. + */ + public function getUser(string $name, Source $source): ?array + { + $this->pluginLogger->debug('Getting user '.$name.'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']); + + try { + $response = $this->callService->call($source, '/users/'.$name); + } catch (ClientException $exception) { + $this->pluginLogger->error($exception->getMessage(), ['plugin' => 'open-catalogi/open-catalogi-bundle']); + } + + if (isset($response) === false) { + return null; + } + + $organization = \Safe\json_decode($response->getBody()->getContents(), true); + + if ($organization === null) { + $this->pluginLogger->error('Could not find a user with name: '.$name.' and with source: '.$source->getName().'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']); + + return null; + }//end if + + return $organization; + + }//end getOrganization() + + /** * Get a repositories of the organization with type user from the github api. From 2f3f07890fa767f60f1be80829432aaa541cc088 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Mon, 20 Nov 2023 18:26:22 +0000 Subject: [PATCH 2/4] Update src from PHP Codesniffer --- src/Service/EnrichOrganizationService.php | 4 +-- src/Service/GithubApiService.php | 42 +++++++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/Service/EnrichOrganizationService.php b/src/Service/EnrichOrganizationService.php index a6c7eee9..f1e315d9 100644 --- a/src/Service/EnrichOrganizationService.php +++ b/src/Service/EnrichOrganizationService.php @@ -111,11 +111,11 @@ public function enrichOrganization(ObjectEntity $organization): ObjectEntity } $opencatalogiUrl = $organization->getValue('opencatalogiRepo'); - $path = trim(\Safe\parse_url($opencatalogiUrl)['path'], '/'); + $path = trim(\Safe\parse_url($opencatalogiUrl)['path'], '/'); // Call the search/code endpoint for publiccode files in this repository. $queryConfig['query'] = ['q' => "filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; - $opencatalogiFiles = $this->githubApiService->getFilesFromRepo($source, $queryConfig); + $opencatalogiFiles = $this->githubApiService->getFilesFromRepo($source, $queryConfig); $opencatalogiNames = [ 'opencatalogi.yaml', diff --git a/src/Service/GithubApiService.php b/src/Service/GithubApiService.php index c97e4413..2afa5fb4 100644 --- a/src/Service/GithubApiService.php +++ b/src/Service/GithubApiService.php @@ -157,7 +157,6 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr return $this->data; }//end if - $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); // Do we have the api key set of the source. if ($this->checkGithubAuth($source) === false @@ -191,7 +190,7 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr $path = trim(\Safe\parse_url($repositoryUrl)['path'], '/'); // Call the search/code endpoint for publiccode files in this repository. $queryConfig['query'] = ['q' => "filename:publiccode filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; - $dataArray = $this->getFilesFromRepo($source, $queryConfig); + $dataArray = $this->getFilesFromRepo($source, $queryConfig); if ($dataArray !== null) { // Import the publiccode/opencatalogi files and connect it to the repository. $repository = $this->importRepoFiles($dataArray, $source, $repository); @@ -559,12 +558,11 @@ public function handleOpencatalogiFile(array $organizationArray, Source $source, $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $organizationArray['html_url']); // Check the sha of the sync with the url reference in the array. if ($this->syncService->doesShaMatch($organizationSync, $urlReference) === true) { - return null; } - $opencatalogi['github'] = $organizationArray['html_url']; - $opencatalogi['type'] = $organizationArray['type']; + $opencatalogi['github'] = $organizationArray['html_url']; + $opencatalogi['type'] = $organizationArray['type']; $opencatalogi['opencatalogiRepo'] = $organizationSync->getObject()->getValue('opencatalogiRepo'); $organizationSync->setMapping($opencatalogiMapping); @@ -671,7 +669,7 @@ public function handlePubliccodeFile(array $publiccodeArray, Source $source, Obj // The urlReference and the git_url changes when the file changes. $sourceId = "https://raw.githubusercontent.com/{$publiccodeArray['repository']['full_name']}/{$publiccodeArray['path']}"; - $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl .' and source id: '.$sourceId); + $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl.' and source id: '.$sourceId); // Get the file from the usercontent or github api source $publiccode = $this->getFileFromRawUserContent($publiccodeUrl, $publiccodeArray['git_url']); @@ -754,15 +752,15 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ // Check if the item name is the same as the openCatalogiNames array. // If so go the the function for the opencatalogi file. if (in_array($item['name'], $opencatalogiNames) === true) { - $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); + $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); $this->pluginLogger->info('The item is a opencatalogi file.'); $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $item['repository']['owner']['html_url']); - $data = [ - 'name' => $item['repository']['full_name'], - 'type' => $item['repository']['owner']['type'], - 'github' => $item['repository']['owner']['html_url'], - 'opencatalogiRepo' => $item['repository']['html_url'] + $data = [ + 'name' => $item['repository']['full_name'], + 'type' => $item['repository']['owner']['type'], + 'github' => $item['repository']['owner']['html_url'], + 'opencatalogiRepo' => $item['repository']['html_url'], ]; $organizationSync = $this->syncService->synchronize($organizationSync, $data); $this->entityManager->persist($organizationSync->getObject()); @@ -772,8 +770,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $repository->hydrate(['organisation' => $organizationSync->getObject()]); $this->entityManager->persist($repository); $this->entityManager->flush(); - - } + }//end if // Check if the item name is the same as the publiccodeNames array. // If so go the the function for the publiccode file. @@ -781,7 +778,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $this->pluginLogger->info('The item is a publiccode file.'); $repository = $this->handlePubliccodeFile($item, $source, $repository); } - } + }//end foreach return $repository; @@ -795,7 +792,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ * * @return array|null */ - public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?array + public function getFileFromGithubApi(string $gitUrl, ?string $query=null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -837,13 +834,13 @@ public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?ar /** * This function gets the publiccode/opencatalogi file from the github user content. * - * @param string $repositoryUrl The url of the repository + * @param string $repositoryUrl The url of the repository * @param string|null $gitUrl The git url of the repository * * @return array|null * @throws GuzzleException */ - public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl = null): ?array + public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl=null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubusercontent.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -870,6 +867,7 @@ public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl } return null; + }//end getFileFromRawUserContent() @@ -917,8 +915,8 @@ public function getRepository(string $repositoryUrl, Source $source): ?array /** * Get the publiccode/opencatalogi files of the given repository * - * @param Source $source The source to sync from. - * @param array $queryConfig The query config of the call. + * @param Source $source The source to sync from. + * @param array $queryConfig The query config of the call. * * @return array|null The publiccode/opencatalogi files as array. */ @@ -1049,6 +1047,7 @@ public function getOrganizationRepos(string $name, Source $source): ?array }//end getOrganizationRepos() + /** * Get a organization with type Organization from the github api. * @@ -1081,8 +1080,7 @@ public function getUser(string $name, Source $source): ?array return $organization; - }//end getOrganization() - + }//end getUser() /** From 876170b9c8f3c367a3cd8231f69dde5a6c203fec Mon Sep 17 00:00:00 2001 From: Sarai Misidjan Date: Mon, 20 Nov 2023 19:36:15 +0100 Subject: [PATCH 3/4] fix enrich component --- src/Service/GithubApiService.php | 69 ++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/src/Service/GithubApiService.php b/src/Service/GithubApiService.php index 2afa5fb4..ca75a8e4 100644 --- a/src/Service/GithubApiService.php +++ b/src/Service/GithubApiService.php @@ -157,6 +157,7 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr return $this->data; }//end if + $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); // Do we have the api key set of the source. if ($this->checkGithubAuth($source) === false @@ -190,7 +191,7 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr $path = trim(\Safe\parse_url($repositoryUrl)['path'], '/'); // Call the search/code endpoint for publiccode files in this repository. $queryConfig['query'] = ['q' => "filename:publiccode filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; - $dataArray = $this->getFilesFromRepo($source, $queryConfig); + $dataArray = $this->getFilesFromRepo($source, $queryConfig); if ($dataArray !== null) { // Import the publiccode/opencatalogi files and connect it to the repository. $repository = $this->importRepoFiles($dataArray, $source, $repository); @@ -288,8 +289,24 @@ public function enrichWithComponent(ObjectEntity $repository, array $repositoryA { $componentSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.component.schema.json', 'open-catalogi/open-catalogi-bundle'); + $forkedFrom = $repository->getValue('forked_from'); + // Set the isBasedOn. + if ($forkedFrom !== null) { + $data['isBasedOn'] = $forkedFrom; + } + + // Set developmentStatus obsolete when repository is archived. + if ($repository->getValue('archived') === true) { + $data['developmentStatus'] = 'obsolete'; + } + + $data = [ + 'name' => $repository->getValue('name'), + 'url' => $repository + ]; + $componentSync = $this->syncService->findSyncBySource($source, $componentSchema, $repositoryArray['html_url']); - $componentSync = $this->syncService->synchronize($componentSync, ['url' => $repository, 'name' => $repository->getValue('name')]); + $componentSync = $this->syncService->synchronize($componentSync, $data); $this->entityManager->persist($componentSync); $this->entityManager->flush(); @@ -558,11 +575,12 @@ public function handleOpencatalogiFile(array $organizationArray, Source $source, $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $organizationArray['html_url']); // Check the sha of the sync with the url reference in the array. if ($this->syncService->doesShaMatch($organizationSync, $urlReference) === true) { + return null; } - $opencatalogi['github'] = $organizationArray['html_url']; - $opencatalogi['type'] = $organizationArray['type']; + $opencatalogi['github'] = $organizationArray['html_url']; + $opencatalogi['type'] = $organizationArray['type']; $opencatalogi['opencatalogiRepo'] = $organizationSync->getObject()->getValue('opencatalogiRepo'); $organizationSync->setMapping($opencatalogiMapping); @@ -669,7 +687,7 @@ public function handlePubliccodeFile(array $publiccodeArray, Source $source, Obj // The urlReference and the git_url changes when the file changes. $sourceId = "https://raw.githubusercontent.com/{$publiccodeArray['repository']['full_name']}/{$publiccodeArray['path']}"; - $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl.' and source id: '.$sourceId); + $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl .' and source id: '.$sourceId); // Get the file from the usercontent or github api source $publiccode = $this->getFileFromRawUserContent($publiccodeUrl, $publiccodeArray['git_url']); @@ -752,15 +770,23 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ // Check if the item name is the same as the openCatalogiNames array. // If so go the the function for the opencatalogi file. if (in_array($item['name'], $opencatalogiNames) === true) { - $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); + $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); $this->pluginLogger->info('The item is a opencatalogi file.'); +// // Get the ref query from the url. This way we can get the publiccode file with the raw.gitgubusercontent. +// $opencatalogiUrlQuery = \Safe\parse_url($item['url'])['query']; +// // Remove the ref= part of the query. +// $urlReference = explode('ref=', $opencatalogiUrlQuery)[1]; + + $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $item['repository']['owner']['html_url']); - $data = [ - 'name' => $item['repository']['full_name'], - 'type' => $item['repository']['owner']['type'], - 'github' => $item['repository']['owner']['html_url'], - 'opencatalogiRepo' => $item['repository']['html_url'], +// $this->syncService->doesShaMatch($organizationSync, $urlReference); + + $data = [ + 'name' => $item['repository']['full_name'], + 'type' => $item['repository']['owner']['type'], + 'github' => $item['repository']['owner']['html_url'], + 'opencatalogiRepo' => $item['repository']['html_url'] ]; $organizationSync = $this->syncService->synchronize($organizationSync, $data); $this->entityManager->persist($organizationSync->getObject()); @@ -770,7 +796,8 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $repository->hydrate(['organisation' => $organizationSync->getObject()]); $this->entityManager->persist($repository); $this->entityManager->flush(); - }//end if + + } // Check if the item name is the same as the publiccodeNames array. // If so go the the function for the publiccode file. @@ -778,7 +805,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $this->pluginLogger->info('The item is a publiccode file.'); $repository = $this->handlePubliccodeFile($item, $source, $repository); } - }//end foreach + } return $repository; @@ -792,7 +819,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ * * @return array|null */ - public function getFileFromGithubApi(string $gitUrl, ?string $query=null): ?array + public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -834,13 +861,13 @@ public function getFileFromGithubApi(string $gitUrl, ?string $query=null): ?arra /** * This function gets the publiccode/opencatalogi file from the github user content. * - * @param string $repositoryUrl The url of the repository + * @param string $repositoryUrl The url of the repository * @param string|null $gitUrl The git url of the repository * * @return array|null * @throws GuzzleException */ - public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl=null): ?array + public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl = null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubusercontent.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -867,7 +894,6 @@ public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl } return null; - }//end getFileFromRawUserContent() @@ -915,8 +941,8 @@ public function getRepository(string $repositoryUrl, Source $source): ?array /** * Get the publiccode/opencatalogi files of the given repository * - * @param Source $source The source to sync from. - * @param array $queryConfig The query config of the call. + * @param Source $source The source to sync from. + * @param array $queryConfig The query config of the call. * * @return array|null The publiccode/opencatalogi files as array. */ @@ -926,7 +952,6 @@ public function getFilesFromRepo(Source $source, array $queryConfig): ?array try { $response = $this->callService->call($source, '/search/code', 'GET', $queryConfig); } catch (Exception $exception) { - var_dump($exception->getMessage()); $this->pluginLogger->error('Error found trying to fetch '.$source->getLocation().'/search/code'.' '.$exception->getMessage()); } @@ -1047,7 +1072,6 @@ public function getOrganizationRepos(string $name, Source $source): ?array }//end getOrganizationRepos() - /** * Get a organization with type Organization from the github api. * @@ -1080,7 +1104,8 @@ public function getUser(string $name, Source $source): ?array return $organization; - }//end getUser() + }//end getOrganization() + /** From 5ef71fc6fdbcc19c1f229490f83ced331986ece2 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Mon, 20 Nov 2023 18:36:39 +0000 Subject: [PATCH 4/4] Update src from PHP Codesniffer --- src/Service/GithubApiService.php | 57 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/Service/GithubApiService.php b/src/Service/GithubApiService.php index ca75a8e4..1dd942a2 100644 --- a/src/Service/GithubApiService.php +++ b/src/Service/GithubApiService.php @@ -157,7 +157,6 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr return $this->data; }//end if - $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); // Do we have the api key set of the source. if ($this->checkGithubAuth($source) === false @@ -191,7 +190,7 @@ public function getGithubRepository(string $repositoryUrl, ?array $repositoryArr $path = trim(\Safe\parse_url($repositoryUrl)['path'], '/'); // Call the search/code endpoint for publiccode files in this repository. $queryConfig['query'] = ['q' => "filename:publiccode filename:opencatalogi extension:yaml extension:yml repo:{$path}"]; - $dataArray = $this->getFilesFromRepo($source, $queryConfig); + $dataArray = $this->getFilesFromRepo($source, $queryConfig); if ($dataArray !== null) { // Import the publiccode/opencatalogi files and connect it to the repository. $repository = $this->importRepoFiles($dataArray, $source, $repository); @@ -302,7 +301,7 @@ public function enrichWithComponent(ObjectEntity $repository, array $repositoryA $data = [ 'name' => $repository->getValue('name'), - 'url' => $repository + 'url' => $repository, ]; $componentSync = $this->syncService->findSyncBySource($source, $componentSchema, $repositoryArray['html_url']); @@ -575,12 +574,11 @@ public function handleOpencatalogiFile(array $organizationArray, Source $source, $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $organizationArray['html_url']); // Check the sha of the sync with the url reference in the array. if ($this->syncService->doesShaMatch($organizationSync, $urlReference) === true) { - return null; } - $opencatalogi['github'] = $organizationArray['html_url']; - $opencatalogi['type'] = $organizationArray['type']; + $opencatalogi['github'] = $organizationArray['html_url']; + $opencatalogi['type'] = $organizationArray['type']; $opencatalogi['opencatalogiRepo'] = $organizationSync->getObject()->getValue('opencatalogiRepo'); $organizationSync->setMapping($opencatalogiMapping); @@ -687,7 +685,7 @@ public function handlePubliccodeFile(array $publiccodeArray, Source $source, Obj // The urlReference and the git_url changes when the file changes. $sourceId = "https://raw.githubusercontent.com/{$publiccodeArray['repository']['full_name']}/{$publiccodeArray['path']}"; - $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl .' and source id: '.$sourceId); + $this->pluginLogger->info('Map the publiccode file with url: '.$publiccodeUrl.' and source id: '.$sourceId); // Get the file from the usercontent or github api source $publiccode = $this->getFileFromRawUserContent($publiccodeUrl, $publiccodeArray['git_url']); @@ -770,23 +768,20 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ // Check if the item name is the same as the openCatalogiNames array. // If so go the the function for the opencatalogi file. if (in_array($item['name'], $opencatalogiNames) === true) { - $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); + $organizationSchema = $this->resourceService->getSchema('https://opencatalogi.nl/oc.organisation.schema.json', 'open-catalogi/open-catalogi-bundle'); $this->pluginLogger->info('The item is a opencatalogi file.'); -// // Get the ref query from the url. This way we can get the publiccode file with the raw.gitgubusercontent. -// $opencatalogiUrlQuery = \Safe\parse_url($item['url'])['query']; -// // Remove the ref= part of the query. -// $urlReference = explode('ref=', $opencatalogiUrlQuery)[1]; - - + // Get the ref query from the url. This way we can get the publiccode file with the raw.gitgubusercontent. + // $opencatalogiUrlQuery = \Safe\parse_url($item['url'])['query']; + // Remove the ref= part of the query. + // $urlReference = explode('ref=', $opencatalogiUrlQuery)[1]; $organizationSync = $this->syncService->findSyncBySource($source, $organizationSchema, $item['repository']['owner']['html_url']); -// $this->syncService->doesShaMatch($organizationSync, $urlReference); - - $data = [ - 'name' => $item['repository']['full_name'], - 'type' => $item['repository']['owner']['type'], - 'github' => $item['repository']['owner']['html_url'], - 'opencatalogiRepo' => $item['repository']['html_url'] + // $this->syncService->doesShaMatch($organizationSync, $urlReference); + $data = [ + 'name' => $item['repository']['full_name'], + 'type' => $item['repository']['owner']['type'], + 'github' => $item['repository']['owner']['html_url'], + 'opencatalogiRepo' => $item['repository']['html_url'], ]; $organizationSync = $this->syncService->synchronize($organizationSync, $data); $this->entityManager->persist($organizationSync->getObject()); @@ -796,8 +791,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $repository->hydrate(['organisation' => $organizationSync->getObject()]); $this->entityManager->persist($repository); $this->entityManager->flush(); - - } + }//end if // Check if the item name is the same as the publiccodeNames array. // If so go the the function for the publiccode file. @@ -805,7 +799,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ $this->pluginLogger->info('The item is a publiccode file.'); $repository = $this->handlePubliccodeFile($item, $source, $repository); } - } + }//end foreach return $repository; @@ -819,7 +813,7 @@ public function importRepoFiles(array $dataArray, Source $source, ObjectEntity $ * * @return array|null */ - public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?array + public function getFileFromGithubApi(string $gitUrl, ?string $query=null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubAPI.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -861,13 +855,13 @@ public function getFileFromGithubApi(string $gitUrl, ?string $query = null): ?ar /** * This function gets the publiccode/opencatalogi file from the github user content. * - * @param string $repositoryUrl The url of the repository + * @param string $repositoryUrl The url of the repository * @param string|null $gitUrl The git url of the repository * * @return array|null * @throws GuzzleException */ - public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl = null): ?array + public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl=null): ?array { $source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubusercontent.source.json', 'open-catalogi/open-catalogi-bundle'); if ($source === null) { @@ -894,6 +888,7 @@ public function getFileFromRawUserContent(string $repositoryUrl, ?string $gitUrl } return null; + }//end getFileFromRawUserContent() @@ -941,8 +936,8 @@ public function getRepository(string $repositoryUrl, Source $source): ?array /** * Get the publiccode/opencatalogi files of the given repository * - * @param Source $source The source to sync from. - * @param array $queryConfig The query config of the call. + * @param Source $source The source to sync from. + * @param array $queryConfig The query config of the call. * * @return array|null The publiccode/opencatalogi files as array. */ @@ -1072,6 +1067,7 @@ public function getOrganizationRepos(string $name, Source $source): ?array }//end getOrganizationRepos() + /** * Get a organization with type Organization from the github api. * @@ -1104,8 +1100,7 @@ public function getUser(string $name, Source $source): ?array return $organization; - }//end getOrganization() - + }//end getUser() /**