Skip to content

Commit

Permalink
CPC-49: Add Jmleroux\CircleCi\Api\Pipeline\ListPipelines endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux committed Mar 8, 2023
1 parent 69e6cd4 commit 32a8139
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/Api/Pipeline/ListPipelines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Jmleroux\CircleCi\Api\Pipeline;

use Jmleroux\CircleCi\Client;
use Jmleroux\CircleCi\Model\Pipeline;
use Jmleroux\CircleCi\ValidateClientVersionTrait;

/**
* Returns all pipelines for the most recently built projects (max 250) you follow in an organization.
*
* @author jmleroux <[email protected]>
* @link https://circleci.com/docs/api/v2/index.html#operation/listPipelines
*/
class ListPipelines
{
use ValidateClientVersionTrait;

protected const endpointPattern = 'pipeline';

public function __construct(private readonly Client $client)
{
$this->validateClientVersion($client, ['v2']);
}

/**
* @return Pipeline[]
*/
public function execute(string $orgSlug, ?bool $mine = false, ?int $maxPipelineCount = 250): array
{
$pipelines = [];

$nextPageToken = null;
$params = [
'org-slug' => $orgSlug,
'mine' => $mine,
];

do {
if (null !== $nextPageToken) {
$params['page-token'] = $nextPageToken;
}

$response = json_decode($this->client->get(static::endpointPattern, $params)->getContent());
$nextPageToken = $response->next_page_token;

foreach ($response->items as $item) {
$pipelines[] = Pipeline::createFromApi($item);
if (count($pipelines) >= $maxPipelineCount) {
break;
}
}
} while (null !== $nextPageToken && count($pipelines) < $maxPipelineCount);

return $pipelines;
}
}
66 changes: 66 additions & 0 deletions tests/Integration/Api/Pipeline/ListPipelinesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Jmleroux\CircleCi\Tests\Integration\Api\Pipeline;

use Jmleroux\CircleCi\Api\Pipeline\ListPipelines;
use Jmleroux\CircleCi\Client;
use Jmleroux\CircleCi\Model\Pipeline;
use Jmleroux\CircleCi\Tests\Integration\ExecuteWithRetryTrait;
use PHPUnit\Framework\TestCase;

/**
* @author JM Leroux <[email protected]>
*/
class ListPipelinesTest extends TestCase
{
use ExecuteWithRetryTrait;

private Client $client;

public function setUp(): void
{
$personaltoken = $_ENV['CIRCLECI_PERSONNAL_TOKEN'];
$this->client = new Client($personaltoken, 'v2');
}

public function testQuery()
{
$query = new ListPipelines($this->client);

$result = $this->executeWithRetry($query, ['gh/jmleroux']);

$this->assertIsArray($result);
$this->assertInstanceOf(Pipeline::class, $result[0]);
}

public function testMaxResults()
{
$query = new ListPipelines($this->client);

$result = $this->executeWithRetry($query, ['gh/jmleroux', false, 50]);

$this->assertIsArray($result);
$this->assertCount(50, $result);
}

public function testPipelineModel()
{
$query = new ListPipelines($this->client);

$result = $this->executeWithRetry($query, ['gh/jmleroux', true, 2]);

/** @var Pipeline $pipeline */
$pipeline = $result[0];

$this->assertInstanceOf(Pipeline::class, $pipeline);
$this->assertIsString($pipeline->id());
$this->assertIsArray($pipeline->errors());
$this->assertSame('gh/jmleroux/circleci-php-client', $pipeline->projectSlug());
$this->assertInstanceOf(\DateTimeImmutable::class, $pipeline->updatedAt());
$this->assertInstanceOf(\DateTimeImmutable::class, $pipeline->createdAt());
$this->assertIsInt($pipeline->number());
$this->assertIsString($pipeline->state());
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Api/Project/ProjectPipelinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testPipelineModel()
$pipeline = $result[0];

$this->assertInstanceOf(Pipeline::class, $pipeline);
$this->assertIsString('7a89bb9e-565a-4964-9788-07fac5ae1355', $pipeline->id());
$this->assertIsString($pipeline->id());
$this->assertIsArray($pipeline->errors());
$this->assertSame('gh/jmleroux/my_project', $pipeline->projectSlug());
$this->assertInstanceOf(\DateTimeImmutable::class, $pipeline->updatedAt());
Expand Down

0 comments on commit 32a8139

Please sign in to comment.