Skip to content

Commit

Permalink
chore(project): refactor properties (refs #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Apr 2, 2024
1 parent fb98e25 commit c6abe8e
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .ci/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ cd $PROJECT_DIR
export APP_ENV=test

# reset database
rm -rf var/data/git-manager-test.db
bin/console cache:clear
bin/console doctrine:schema:update --force


# prepare output dir for test
mkdir -p var/output
rm -rf var/output/*
rm -rf var/output/*

# run test
export XDEBUG_MODE=coverage
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ bin/console git:fetch-all --users=_me_ https://github.com $GITHUB_TOKEN
* From gogs or gitea :

```bash
bin/console git:fetch-all --type gogs-v1 https://codes.quadtreeworld.net $QTW_TOKEN
bin/console git:fetch-all -vv --type gogs-v1 https://codes.quadtreeworld.net $QTW_TOKEN
```

### Compute stats about repositories

```bash
bin/console git:stats -O stats.json
bin/console git:stats -vv
```

## Usage with docker
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"symfony/phpunit-bridge": "^6.4",
"phpunit/phpunit": "^9",
"php-coveralls/php-coveralls": "^2.5",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-doctrine": "^1.3"
},
"scripts": {
"auto-scripts": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ parameters:
- public/
- src/
- tests/
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
13 changes: 6 additions & 7 deletions public/js/git-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function getLastActivity(repository) {


function renderTrivy(trivy){
console.log(trivy);
if ( ! trivy.success ){
return `<span class="text-danger">FAILURE</span>`
}
Expand All @@ -31,15 +30,15 @@ function loadRepositories() {
}).then(function (items) {
let dataSet = items.map(function (item) {
const name = item.name;
const metadata = item.metadata;
const sizeMo = (metadata.size / (1024 * 1024)).toFixed(1);
const sizeMo = (item.size / (1024 * 1024)).toFixed(1);
const checks = item.checks;
return [
`<a href="https://${name}">${name}</a>`,
`<span class="${metadata.readme ? "text-success" : "text-danger"}">${metadata.readme ? "FOUND" : "MISSING"}</span>`,
`<span class="${metadata.license ? "text-success" : "text-danger"}">${metadata.license ? metadata.license : "MISSING"}</span>`,
getLastActivity(metadata),
`<span class="${checks.readme ? "text-success" : "text-danger"}">${checks.readme ? "FOUND" : "MISSING"}</span>`,
`<span class="${checks.license ? "text-success" : "text-danger"}">${checks.license ? checks.license : "MISSING"}</span>`,
getLastActivity(item),
sizeMo,
metadata.trivy
checks.trivy
];
});
$('#repositories').DataTable({
Expand Down
10 changes: 4 additions & 6 deletions src/Controller/Api/ProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@

namespace MBO\GitManager\Controller\Api;

use Doctrine\ORM\EntityManagerInterface;
use MBO\GitManager\Entity\Project;
use MBO\GitManager\Repository\ProjectRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProjectsController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager
private ProjectRepository $projectRepository
) {
}

#[Route('/api/projects', name: 'app_projects_list')]
public function list(): Response
{
$projectRepository = $this->entityManager->getRepository(Project::class);
/** @var Project[] $projects */
$projects = $projectRepository->findAll();
$projects = $this->projectRepository->findAll();

return $this->json($projects);
}

#[Route('/api/projects/{id}', name: 'app_projects_get')]
public function get(string $id): Response
{
$projectRepository = $this->entityManager->getRepository(Project::class);
/** @var Project $project */
$project = $projectRepository->find($id);
$project = $this->projectRepository->find($id);

return $this->json($project);
}
Expand Down
146 changes: 138 additions & 8 deletions src/Entity/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,71 @@
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Symfony\Component\Serializer\Annotation\Ignore;
use MBO\GitManager\Repository\ProjectRepository;
use Symfony\Component\Uid\Uuid;

#[Entity]
#[Entity(repositoryClass: ProjectRepository::class)]
class Project
{
/**
* Generated UID.
*/
#[Id, Column]
private string $id;

/**
* Full name (ex : github.com/mborne/ansible-docker-ce).
*/
#[Column(unique: true)]
private string $name;

/**
* Metadata update date.
*/
#[Column(type: 'datetime')]
private \DateTime $updatedAt;

/**
* Size of repository (ko).
*/
#[Column]
private int $size;

/**
* Tag names.
*
* @var string[]
*/
#[Column(type: 'json')]
private array $tags;

/**
* Branch names.
*
* @var string[]
*/
#[Column(type: 'json')]
private array $branches;

/**
* Number of commit per days.
*
* @var array<string,int>
*/
#[Column(type: 'json')]
// #[Ignore]
private mixed $metadata;
private mixed $activity;

/**
* @var array<string,mixed>
*/
#[Column(type: 'json')]
private array $checks;

public function __construct()
{
$this->id = Uuid::v4();
$this->updatedAt = new \DateTime('now');
$this->checks = [];
}

public function getId(): string
Expand All @@ -43,14 +89,98 @@ public function setName(string $name): self
return $this;
}

public function getMetadata(): mixed
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}

public function setUpdatedAt(\DateTime $updatedAt): self
{
$this->updatedAt = $updatedAt;

return $this;
}

public function getSize(): int
{
return $this->size;
}

public function setSize(int $size): self
{
$this->size = $size;

return $this;
}

/**
* @return string[]
*/
public function getTags(): array
{
return $this->tags;
}

/**
* @param string[] $tags
*/
public function setTags(array $tags): self
{
$this->tags = $tags;

return $this;
}

/**
* @return string[]
*/
public function getBranches(): array
{
return $this->branches;
}

/**
* @param string[] $branches
*/
public function setBranches(array $branches): self
{
$this->branches = $branches;

return $this;
}

/**
* @return array<string,int>
*/
public function getActivity(): array
{
return $this->activity;
}

/**
* @param array<string,int> $activity
*/
public function setActivity(array $activity): self
{
$this->activity = $activity;

return $this;
}

/**
* @return array<string,mixed>
*/
public function getChecks(): array
{
return $this->metadata;
return $this->checks;
}

public function setMetadata(mixed $metadata): self
/**
* @param array<string,mixed> $checks
*/
public function setChecks(array $checks): self
{
$this->metadata = $metadata;
$this->checks = $checks;

return $this;
}
Expand Down
28 changes: 11 additions & 17 deletions src/Git/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,26 @@ public function update(Project $project): void
$gitRepository = new GitRepository(
$this->localFilesystem->getRootPath().'/'.$project->getName()
);
$project->setMetadata($this->getMetadata($gitRepository));
$project->setUpdatedAt(new \DateTime('now'));
$project->setSize($gitRepository->getSize() * 1024);
$project->setTags($this->getTagNames($gitRepository));
$project->setBranches($this->getBranchNames($gitRepository));
$project->setActivity($this->getCommitDates($gitRepository));

$project->setChecks($this->getChecks($gitRepository));
}

/**
* Get metadata for a given repository.
*
* @return array<string,mixed>
*/
private function getMetadata(GitRepository $gitRepository): array
private function getChecks(GitRepository $gitRepository): array
{
$this->logger->debug('[Analyser] retrieve git metadata...', [
'repository' => $gitRepository->getWorkingDir(),
]);
$metadata = [
'updatedAt' => new \DateTime('now'),
'size' => $gitRepository->getSize() * 1024,
];

$metadata['tags'] = $this->getTagNames($gitRepository);
$metadata['branch'] = $this->getBranchNames($gitRepository);
$metadata['activity'] = $this->getCommitDates($gitRepository);
$results = [];
foreach ($this->checkers as $checker) {
$metadata[$checker->getName()] = $checker->check($gitRepository);
$results[$checker->getName()] = $checker->check($gitRepository);
}

return $metadata;
return $results;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Repository/ProjectRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MBO\GitManager\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use MBO\GitManager\Entity\Project;

/**
* @extends ServiceEntityRepository<ProjectRepository>
*/
class ProjectRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $managerRegistry)
{
parent::__construct($managerRegistry, Project::class);
}
}

0 comments on commit c6abe8e

Please sign in to comment.