Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Merge abf53a8 into 0a5a945
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Dec 2, 2017
2 parents 0a5a945 + abf53a8 commit d368afe
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

class AddBuildNumberToInspections extends AbstractMigration
{
public function up()
{
$inspectionsTable = $this->table('inspection', ['id' => false, 'primary_key' => ['id']]);
$inspectionsTable->addColumn('build_number', 'integer', ['default' => 1]);
$inspectionsTable->save();

$inspections = $this->fetchAll('SELECT id, repository_id FROM inspection ORDER BY created_at ASC');

$buildNumbersMap = [];
foreach ($inspections as $inspection) {
$buildNumbersMap[$inspection['repository_id']]++;
$buildNumber = $buildNumbersMap[$inspection['repository_id']];

$this->execute(sprintf("UPDATE inspection SET build_number = %d WHERE id = '%s'", $buildNumber, $inspection['id']));
}

$inspectionsTable
->addIndex(['repository_id', 'build_number'], [
'unique' => true,
'name' => 'idx_repository_build_number',
])
->save();
}

public function down()
{
$inspections = $this->table('inspection', ['id' => false, 'primary_key' => ['id']]);
$inspections->removeColumn('build_number');
$inspections->save();
}
}
1 change: 1 addition & 0 deletions phpmd-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/cleancode.xml">
<exclude name="ElseExpression" />
<exclude name="StaticAccess" />
</rule>
</ruleset>
6 changes: 6 additions & 0 deletions src/AppContext/Domain/Entity/Inspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Inspection
private $report;
private $status;
private $repository;
private $number;
private $createdAt;
private $startedAt;
private $finishedAt;
Expand All @@ -51,6 +52,11 @@ public function getId(): string
return $this->id;
}

public function getNumber(): int
{
return $this->number;
}

// TODO
public function getDiff(): Diff
{
Expand Down
3 changes: 3 additions & 0 deletions src/AppContext/Domain/Entity/config/Inspection.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Regis\AppContext\Domain\Entity\Inspection:
generator: { strategy: NONE }

fields:
number:
type: integer
column: build_number
createdAt:
type: datetimetz
startedAt:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% import '::utils.html.twig' as utils %}

{% block content %}
<h1 class="page-header"><span class="label label-primary">{{ utils.inspection_type(inspection) }}</span> Inspection « {{ inspection.id }} »</h1>
<h1 class="page-header"><span class="label label-primary">{{ utils.inspection_type(inspection) }}</span> Inspection #{{ inspection.number }}</h1>

<h2>Summary</h2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<tr class="{{ utils.class_for_integration_status(inspection.status) }}">
<td>
<span class="label label-primary">{{ utils.inspection_type(inspection) }}</span>
<a href="{{ path('inspection_detail', {'id': inspection.id}) }}">{{ inspection.id }}</a>
<a href="{{ path('inspection_detail', {'id': inspection.id}) }}">#{{ inspection.number }}</a>
</td>
<td>{{ inspection.createdAt|date }}</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function handle(Command\Inspection\SchedulePullRequest $command): void
return;
}

$number = $this->inspectionsRepo->nextBuildNumber($repository);

// create the inspection
$inspection = Entity\PullRequestInspection::create($repository, $pullRequest);
$inspection = Entity\PullRequestInspection::create($repository, $pullRequest, $number);
$this->inspectionsRepo->save($inspection);

// FIXME probably broken for forked repositories
Expand Down
4 changes: 3 additions & 1 deletion src/BitbucketContext/Domain/Entity/Inspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class Inspection
private $id;
private $report;
private $repository;
private $number;
private $status;
private $createdAt;
private $startedAt;
Expand All @@ -45,10 +46,11 @@ abstract class Inspection
/**
* @return static
*/
protected static function createForRevisions(Repository $repository, string $head, string $base): self
protected static function createForRevisions(Repository $repository, string $head, string $base, int $number): self
{
$inspection = new static();
$inspection->repository = $repository;
$inspection->number = $number;
$inspection->createdAt = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$inspection->status = self::STATUS_SCHEDULED;
$inspection->base = $base;
Expand Down
4 changes: 2 additions & 2 deletions src/BitbucketContext/Domain/Entity/PullRequestInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class PullRequestInspection extends Inspection
{
private $pullRequestNumber;

public static function create(Repository $repository, Model\PullRequest $pullRequest): self
public static function create(Repository $repository, Model\PullRequest $pullRequest, int $number): self
{
/** @var PullRequestInspection $inspection */
$inspection = parent::createForRevisions($repository, $pullRequest->getHead(), $pullRequest->getBase());
$inspection = parent::createForRevisions($repository, $pullRequest->getHead(), $pullRequest->getBase(), $number);
$inspection->pullRequestNumber = $pullRequest->getNumber();

return $inspection;
Expand Down
3 changes: 3 additions & 0 deletions src/BitbucketContext/Domain/Entity/config/Inspection.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Regis\BitbucketContext\Domain\Entity\Inspection:
generator: { strategy: NONE }

fields:
number:
type: integer
column: build_number
createdAt:
type: datetimetz
startedAt:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ interface PullRequestInspections
public function save(Entity\PullRequestInspection $inspection): void;

public function find(string $id): Entity\PullRequestInspection;

public function nextBuildNumber(Entity\Repository $repository): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,17 @@ public function find(string $id): Entity\PullRequestInspection

return $inspection;
}

public function nextBuildNumber(Entity\Repository $repository): int
{
$qb = $this->entityManager()->createQueryBuilder();

$qb
->select('MAX(inspection.number)')
->from(Entity\PullRequestInspection::class, 'inspection')
->where('inspection.repository = :repository')
->setParameter('repository', $repository);

return $qb->getQuery()->getSingleScalarResult() + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public function handle(Command\Inspection\SchedulePullRequest $command): void
return;
}

$number = $this->inspectionsRepo->nextBuildNumber($repository);

// create the inspection
$inspection = Entity\PullRequestInspection::create($repository, $pullRequest);
$inspection = Entity\PullRequestInspection::create($repository, $pullRequest, $number);
$this->inspectionsRepo->save($inspection);

// and schedule it
Expand Down
4 changes: 3 additions & 1 deletion src/GithubContext/Domain/Entity/Inspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class Inspection
private $id;
private $report;
private $repository;
private $number;
private $status;
private $createdAt;
private $startedAt;
Expand All @@ -49,10 +50,11 @@ abstract public function getType(): string;
/**
* @return static
*/
protected static function createForRevisions(Repository $repository, string $head, string $base): self
protected static function createForRevisions(Repository $repository, string $head, string $base, int $number): self
{
$inspection = new static();
$inspection->repository = $repository;
$inspection->number = $number;
$inspection->createdAt = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$inspection->status = self::STATUS_SCHEDULED;
$inspection->base = $base;
Expand Down
4 changes: 2 additions & 2 deletions src/GithubContext/Domain/Entity/PullRequestInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class PullRequestInspection extends Inspection
{
private $pullRequestNumber;

public static function create(Repository $repository, Model\PullRequest $pullRequest): self
public static function create(Repository $repository, Model\PullRequest $pullRequest, int $number): self
{
/** @var PullRequestInspection $inspection */
$inspection = parent::createForRevisions($repository, $pullRequest->getHead(), $pullRequest->getBase());
$inspection = parent::createForRevisions($repository, $pullRequest->getHead(), $pullRequest->getBase(), $number);
$inspection->pullRequestNumber = $pullRequest->getNumber();

return $inspection;
Expand Down
3 changes: 3 additions & 0 deletions src/GithubContext/Domain/Entity/config/Inspection.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Regis\GithubContext\Domain\Entity\Inspection:
generator: { strategy: NONE }

fields:
number:
type: integer
column: build_number
createdAt:
type: datetimetz
startedAt:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

interface PullRequestInspections
{
public function nextBuildNumber(Entity\Repository $repository): int;

public function save(Entity\PullRequestInspection $inspection): void;

public function find(string $id): Entity\PullRequestInspection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class DoctrinePullRequestInspections implements Repository\PullRequestInspection
{
use RepositoryHelper;

public function save(Entity\PullRequestInspection $inspections): void
public function save(Entity\PullRequestInspection $inspection): void
{
$this->entityManager()->persist($inspections);
$this->entityManager()->persist($inspection);
$this->entityManager()->flush();
}

Expand All @@ -48,4 +48,17 @@ public function find(string $id): Entity\PullRequestInspection

return $inspection;
}

public function nextBuildNumber(Entity\Repository $repository): int
{
$qb = $this->entityManager()->createQueryBuilder();

$qb
->select('MAX(inspection.number)')
->from(Entity\PullRequestInspection::class, 'inspection')
->where('inspection.repository = :repository')
->setParameter('repository', $repository);

return $qb->getQuery()->getSingleScalarResult() + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public function testItSavesTheInspectionAndSchedulesIt()

$bitbucketClient->method('getCloneUrl')->with($this->repositoryIdentifier)->willReturn('clone-url');

$this->inspectionsRepo->expects($this->once())
->method('nextBuildNumber')
->with($this->repository)
->willReturn(2);

$this->inspectionsRepo->expects($this->once())
->method('save')
->with($this->callback(function (Entity\PullRequestInspection $inspection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public function testItSavesTheInspectionAndSchedulesIt()
],
]);

$this->inspectionsRepo->expects($this->once())
->method('nextBuildNumber')
->with($this->repository)
->willReturn(2);

$this->inspectionsRepo->expects($this->once())
->method('save')
->with($this->callback(function (Entity\PullRequestInspection $inspection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function setUp()

public function testItHasAType()
{
$inspection = PullRequestInspection::create($this->repository, $this->pullRequest);
$inspection = PullRequestInspection::create($this->repository, $this->pullRequest, 4);

$this->assertSame(PullRequestInspection::TYPE_GITHUB_PR, $inspection->getType());
}
Expand All @@ -50,14 +50,14 @@ public function testItExposesThePrNumber()
{
$this->pullRequest->method('getNumber')->willReturn(42);

$inspection = PullRequestInspection::create($this->repository, $this->pullRequest);
$inspection = PullRequestInspection::create($this->repository, $this->pullRequest, 4);

$this->assertSame(42, $inspection->getPullRequestNumber());
}

public function testItIsInitializedCorrectly()
{
$inspection = PullRequestInspection::create($this->repository, $this->pullRequest);
$inspection = PullRequestInspection::create($this->repository, $this->pullRequest, 4);

$this->assertNotEmpty($inspection->getId());
$this->assertEmpty($inspection->getFailureTrace());
Expand Down

0 comments on commit d368afe

Please sign in to comment.