Skip to content

Commit

Permalink
Enhancement package API
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Aug 31, 2023
1 parent 1e95294 commit 2b878bf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ Installation
php bin/console packagist:user:manager username [email protected] --password=123456 --admin
```

6. Enable cron tabs and background jobs.
6. (optional) If you changed the configuration files, then you need to clear the cache `rm -rf var/cache/*` or `php bin/console cache:clear`

7. Enable cron tabs and background jobs.
Enable crontab `crontab -e -u www-data` or use Docker friendly build-in cron demand runner.

```
Expand Down Expand Up @@ -183,13 +185,13 @@ priority=1
user=www-data
```

7. **IMPORTANT** Make sure that web-server, cron and supervisor run under the same user, that should have an ssh key
8. **IMPORTANT** Make sure that web-server, cron and supervisor run under the same user, that should have an ssh key
that gives it read (clone) access to your git/svn/hg repositories. If you run application under `www-data`
you can add your ssh keys to /var/www/.ssh/

You should now be able to access the site, create a user, etc.

8. Make a VirtualHost with DocumentRoot pointing to public/
9. Make a VirtualHost with DocumentRoot pointing to public/

Ssh key access and composer oauth token.
-----------------------
Expand Down
78 changes: 41 additions & 37 deletions src/Controller/Api/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Persistence\ManagerRegistry;
use Packeton\Attribute\Vars;
use Packeton\Controller\ControllerTrait;
use Packeton\Entity\Job;
use Packeton\Entity\OAuthIntegration;
use Packeton\Entity\Package;
Expand All @@ -17,6 +16,7 @@
use Packeton\Model\AutoHookUser;
use Packeton\Model\DownloadManager;
use Packeton\Model\PackageManager;
use Packeton\Package\RepTypes;
use Packeton\Security\Provider\AuditSessionProvider;
use Packeton\Service\JobPersister;
use Packeton\Service\Scheduler;
Expand All @@ -27,15 +27,14 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[Route(defaults: ['_format' => 'json'])]
class ApiController extends AbstractController
{
use ControllerTrait;
use ApiControllerTrait;

public function __construct(
protected ManagerRegistry $registry,
Expand All @@ -52,39 +51,44 @@ public function createPackageAction(Request $request): Response
{
$payload = $this->getJsonPayload($request);

if (!$payload || empty($url = $payload['repository']['url'] ?? null)) {
if (!$payload) {
return new JsonResponse(['status' => 'error', 'message' => 'Missing payload repository->url parameter'], 406);
}

$package = new Package;
// For BC layer
if (isset($payload['repository']['url'])) {
$payload['repository'] = $payload['repository']['url'];
}

$package = new Package();
if ($this->getUser() instanceof User) {
$package->addMaintainer($this->getUser());
}

$package->setRepository($url);
$this->container->get(PackageManager::class)->updatePackageUrl($package);
$errors = $this->validator->validate($package, null, ['Create']);
if (\count($errors) > 0) {
$errorArray = [];
foreach ($errors as $error) {
$errorArray[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(['status' => 'error', 'message' => $errorArray], 406);
}
try {
$type = $payload['repoType'] ?? null;
$formType = RepTypes::getFormType($type);

$form = $this->createForm($formType, $package, [
'csrf_protection' => false,
'validation_groups' => ['Create', 'Default'],
'is_created' => true
]);

$form->submit($payload);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->registry->getManager();
$em->persist($package);
$em->flush();

$this->container->get(PackageManager::class)->insetPackage($package);
} catch (\Exception $e) {
$this->logger->critical($e->getMessage(), ['exception', $e]);
return new JsonResponse(['status' => 'error', 'message' => 'Error saving package'], 500);
}
$job = $this->container->get(Scheduler::class)->scheduleUpdate($package);

$job = $this->container->get(Scheduler::class)->scheduleUpdate($package);
return new JsonResponse(['status' => 'success', 'name' => $package->getName(), 'job' => $job->getId()], 202);
}

return new JsonResponse(['status' => 'success', 'job' => $job->getId()], 202);
$errorArray = $this->getErrors($form);
return new JsonResponse(['status' => 'error', 'message' => $errorArray], 406);
}

#[Route('/api/hooks/{alias}/{id}', name: 'api_integration_postreceive')]
Expand Down Expand Up @@ -161,24 +165,24 @@ public function editPackageAction(Request $request, #[Vars] Package $package): R

$payload = $this->getJsonPayload($request);

$package->setRepository($payload['repository']);
$this->container->get(PackageManager::class)->updatePackageUrl($package);
$errors = $this->validator->validate($package, null, ["Update"]);
if (\count($errors) > 0) {
$errorArray = [];
foreach ($errors as $error) {
$errorArray[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(['status' => 'error', 'message' => $errorArray], 406);
}
$formType = RepTypes::getFormType($package->getRepoType());
$form = $this->createForm($formType, $package, [
'csrf_protection' => false,
'validation_groups' => ['Update', 'Default'],
'is_created' => false
]);

$package->setCrawledAt(null);
$form->submit($payload, false);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->registry->getManager();
$em->flush();

$em = $this->registry->getManager();
$em->persist($package);
$em->flush();
$job = $this->container->get(Scheduler::class)->scheduleUpdate($package);
return new JsonResponse(['status' => 'success', 'name' => $package->getName(), 'job' => $job->getId()], 200);
}

return new JsonResponse(['status' => 'success'], 200);
$errorArray = $this->getErrors($form);
return new JsonResponse(['status' => 'error', 'message' => $errorArray], 406);
}


Expand Down
16 changes: 9 additions & 7 deletions swagger/packages-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ x-pkg-param: &pkg-param
required: true
type: "string"


paths:
'/api/webhook-invoke/{name}':
post:
Expand Down Expand Up @@ -47,7 +46,7 @@ paths:
post:
tags: [ Packages ]
summary: 'Create a package'
example: $Package
example: $PackageCreate

'/api/packages':
get:
Expand Down Expand Up @@ -78,7 +77,7 @@ paths:
put:
tags: [ Packages ]
summary: 'Update a package'
example: $Package
example: $PackageUpdate
<<: *pkg-param

'/api/packages/{name}/changelog':
Expand All @@ -98,9 +97,12 @@ paths:
in: query

examples:
Package: |
PackageCreate: |
{
"repository": "[email protected]:sonata-project/SonataSeoBundle.git",
"credentials": 1
}
PackageUpdate: |
{
"repository": {
"url": "[email protected]:sonata-project/SonataSeoBundle.git"
}
"repository": "[email protected]:sonata-project/SonataSeoBundle.git"
}

0 comments on commit 2b878bf

Please sign in to comment.