-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CPC-49: Add
Jmleroux\CircleCi\Api\Pipeline\ListPipelines
endpoint
- Loading branch information
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters