Skip to content

Commit

Permalink
updated githubEventService
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarai Misidjan authored and Sarai Misidjan committed Nov 9, 2023
1 parent 12742a0 commit 4014fba
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 202 deletions.
75 changes: 3 additions & 72 deletions src/Service/EnrichPubliccodeFromGithubUrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,6 @@ public function setConfiguration(array $configuration): void
}//end setConfiguration()


/**
* This function gets the publiccode file from the github user content.
*
* @param string $repositoryUrl The url of the repository
*
* @throws GuzzleException
*
* @return array|null
*/
public function getPubliccodeFromRawUserContent(string $repositoryUrl): ?array
{
$source = $this->resourceService->getSource($this->configuration['usercontentSource'], 'open-catalogi/open-catalogi-bundle');
if ($source === null) {
return $this->data;
}

// Get the path from the url to make the call.
$endpoint = \Safe\parse_url($repositoryUrl)['path'];
try {
$response = $this->callService->call($source, $endpoint);
} catch (Exception $e) {
$this->pluginLogger->error('Error found trying to fetch '.$repositoryUrl.' '.$e->getMessage());
}

return $this->callService->decodeResponse($source, $response, 'text/yaml');

}//end getPubliccodeFromRawUserContent()


/**
* This function gets and maps the publiccode file
*
Expand All @@ -148,50 +119,10 @@ public function enrichRepositoryWithPubliccode(ObjectEntity $repository, string
if ($this->githubApiService->checkGithubAuth($source) === false) {
return null;
}//end if

$repositories = $this->githubApiService->getPubliccodesFromRepo($repositoryUrl, $source);

$url = trim(\Safe\parse_url($repositoryUrl, PHP_URL_PATH), '/');

// Call the search/code endpoint for publiccode files in this repository.
$queryConfig['query'] = ['q' => "filename:publiccode extension:yaml extension:yml repo:{$url}"];

// Find the publiccode.yaml file(s).
try {
$response = $this->callService->call($source, '/search/code', 'GET', $queryConfig);
} catch (Exception $exception) {
$this->pluginLogger->error('Error found trying to fetch '.$source->getLocation().'/search/code'.' '.$exception->getMessage());
}

if (isset($response) === false) {
return null;
}

$repositories = $this->callService->decodeResponse($source, $response);

$this->pluginLogger->debug('Found '.count($repositories).' publiccode file(s).', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

$publiccodeUrls = [];
foreach ($repositories['items'] as $githubRepository) {
// Get the ref query from the url. This way we can get the publiccode file with the raw.gitgubusercontent
$publiccodeUrlQuery = \Safe\parse_url($githubRepository['url'])['query'];
$urlReference = explode('ref=', $publiccodeUrlQuery)[1];

$publiccodeUrls[] = $publiccodeUrl = "https://raw.githubusercontent.com/{$githubRepository['repository']['full_name']}/{$urlReference}/{$githubRepository['path']}";

// Get the publiccode through the raw.githubusercontent source
$publiccode = $this->getPubliccodeFromRawUserContent($publiccodeUrl);

$this->pluginLogger->notice('Got publiccode from GitHub', $publiccode);

if ($publiccode !== null) {
$repository = $this->githubService->mapPubliccode($repository, $publiccode, $this->configuration, $publiccodeUrl);
}
}

$repository->setValue('publiccode_urls', $publiccodeUrls);
$this->entityManager->persist($repository);
$this->entityManager->flush();

return $repository;
return $this->githubService->mappPubliccodesFromRepo($repositories, $repository);

}//end enrichRepositoryWithPubliccode()

Expand Down
4 changes: 2 additions & 2 deletions src/Service/GetResourcesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getComponents(Source $source, string $endpoint, array $configura

$result = [];
foreach ($components as $component) {
$result[] = $this->importResourceService->importComponent($component, $configuration);
$result[] = $this->importResourceService->importCatalogusComponent($component, $configuration);
}

$this->entityManager->flush();
Expand Down Expand Up @@ -125,7 +125,7 @@ public function getComponent(Source $source, string $endpoint, string $component
return null;
}

$component = $this->importResourceService->importComponent($component, $configuration);
$component = $this->importResourceService->importCatalogusComponent($component, $configuration);
if ($component === null) {
return null;
}
Expand Down
144 changes: 118 additions & 26 deletions src/Service/GithubApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GithubApiService
*/
private MappingService $mappingService;


/**
* @var array
*/
Expand Down Expand Up @@ -107,6 +108,34 @@ public function checkGithubAuth(Source $source): ?bool

}//end checkGithubAuth()

/**
* This function gets the publiccode file from the github user content.
*
* @param string $repositoryUrl The url of the repository
*
* @throws GuzzleException
*
* @return array|null
*/
public function getPubliccodeFromRawUserContent(string $repositoryUrl): ?array
{
$source = $this->resourceService->getSource('https://opencatalogi.nl/source/oc.GitHubusercontent.source.json', 'open-catalogi/open-catalogi-bundle');
if ($source === null) {
return $this->data;
}

// Get the path from the url to make the call.
$endpoint = \Safe\parse_url($repositoryUrl)['path'];
try {
$response = $this->callService->call($source, $endpoint);
} catch (Exception $e) {
$this->pluginLogger->error('Error found trying to fetch '.$repositoryUrl.' '.$e->getMessage());
}

return $this->callService->decodeResponse($source, $response, 'text/yaml');

}//end getPubliccodeFromRawUserContent()


/**
* Get a repository through the repositories of the given source
Expand Down Expand Up @@ -142,44 +171,107 @@ public function getRepository(string $name, Source $source): ?array

}//end getRepository()

/**
* Get a repository through the repositories of the given source
*
* @param string $repositoryUrl The url of the repository.
* @param Source $source The source to sync from.
*
* @return array|null The imported repository as array.
*/
public function getPubliccodesFromRepo(string $repositoryUrl, Source $source): ?array
{
$url = trim(\Safe\parse_url($repositoryUrl, PHP_URL_PATH), '/');

// Call the search/code endpoint for publiccode files in this repository.
$queryConfig['query'] = ['q' => "filename:publiccode extension:yaml extension:yml repo:{$url}"];

// Find the publiccode.yaml file(s).
try {
$response = $this->callService->call($source, '/search/code', 'GET', $queryConfig);
} catch (Exception $exception) {
$this->pluginLogger->error('Error found trying to fetch '.$source->getLocation().'/search/code'.' '.$exception->getMessage());
}

if (isset($response) === false) {
return null;
}

$repositories = $this->callService->decodeResponse($source, $response);

$this->pluginLogger->debug('Found '.count($repositories).' publiccode file(s).', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

return $repositories;

}//end getRepository()

/**
* This function create or get the component of the repository.
* Get a repository through the repositories of the given source
*
* @param ObjectEntity $repository The repository object.
* @param string|null $publiccodeUrl The publiccode url.
* @param string $name The name of the repository.
* @param Source $source The source to sync from.
*
* @return ObjectEntity|null
* @return array|null The imported repository as array.
*/
public function connectComponent(ObjectEntity $repository, ?string $publiccodeUrl=null): ?ObjectEntity
public function getOrganisation(string $name, Source $source): ?array
{
$componentEntity = $this->resourceService->getSchema('https://opencatalogi.nl/oc.component.schema.json', 'open-catalogi/open-catalogi-bundle');
$cacheComponents = $this->cacheService->searchObjects(null, ['url' => $repository->getSelf()], [$componentEntity->getId()->toString()])['results'];

if ($cacheComponents === []) {
$component = new ObjectEntity($componentEntity);
$component->hydrate(
[
'name' => $repository->getValue('name'),
'url' => $repository,
'publiccodeUrl' => $publiccodeUrl,
]
);
$this->entityManager->persist($component);
$this->entityManager->flush();
}//end if
$this->pluginLogger->debug('Getting repository '.$name.'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

if (count($cacheComponents) === 1) {
$component = $this->entityManager->find('App:ObjectEntity', $cacheComponents[0]['_self']['id']);
try {
$response = $this->callService->call($source, '/repos/'.$name);
} catch (ClientException $exception) {
$this->pluginLogger->error($exception->getMessage(), ['plugin' => 'open-catalogi/open-catalogi-bundle']);
}

if (isset($response) === false) {
return null;
}

$repository = \Safe\json_decode($response->getBody()->getContents(), true);

if ($repository === null) {
$this->pluginLogger->error('Could not find a repository with name: '.$name.' and with source: '.$source->getName().'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

return null;
}//end if

if (isset($component) === true) {
return $component;
return $repository;

}//end getRepository()

/**
* Get a repository through the repositories of the given source
*
* @param string $name The name of the repository.
* @param Source $source The source to sync from.
*
* @return array|null The imported repository as array.
*/
public function getOrganisationRepos(string $name, Source $source): ?array
{
$this->pluginLogger->debug('Getting repository '.$name.'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

try {
$response = $this->callService->call($source, '/repos/'.$name);
} catch (ClientException $exception) {
$this->pluginLogger->error($exception->getMessage(), ['plugin' => 'open-catalogi/open-catalogi-bundle']);
}

if (isset($response) === false) {
return null;
}

$repository = \Safe\json_decode($response->getBody()->getContents(), true);

if ($repository === null) {
$this->pluginLogger->error('Could not find a repository with name: '.$name.' and with source: '.$source->getName().'.', ['plugin' => 'open-catalogi/open-catalogi-bundle']);

return null;
}//end if

return null;
return $repository;

}//end connectComponent()
}//end getRepository()


/**
Expand Down
Loading

0 comments on commit 4014fba

Please sign in to comment.