Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/DataTransferObjects/Commit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace ConduitUI\Pr\DataTransferObjects;

class Commit
{
public function __construct(
public readonly string $sha,
public readonly string $message,
public readonly CommitAuthor $author,
public readonly CommitAuthor $committer,
public readonly string $htmlUrl,
public readonly ?User $githubAuthor,
public readonly ?User $githubCommitter,
) {}

public static function fromArray(array $data): self
{
return new self(
sha: $data['sha'],
message: $data['commit']['message'],
author: CommitAuthor::fromArray($data['commit']['author']),
committer: CommitAuthor::fromArray($data['commit']['committer']),
htmlUrl: $data['html_url'],
githubAuthor: isset($data['author']) ? User::fromArray($data['author']) : null,
githubCommitter: isset($data['committer']) ? User::fromArray($data['committer']) : null,
);
}

public function toArray(): array
{
return [
'sha' => $this->sha,
'commit' => [
'message' => $this->message,
'author' => $this->author->toArray(),
'committer' => $this->committer->toArray(),
],
'html_url' => $this->htmlUrl,
'author' => $this->githubAuthor?->toArray(),
'committer' => $this->githubCommitter?->toArray(),
];
}
}
34 changes: 34 additions & 0 deletions src/DataTransferObjects/CommitAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace ConduitUI\Pr\DataTransferObjects;

use DateTimeImmutable;

class CommitAuthor
{
public function __construct(
public readonly string $name,
public readonly string $email,
public readonly DateTimeImmutable $date,
) {}

public static function fromArray(array $data): self
{
return new self(
name: $data['name'],
email: $data['email'],
date: new DateTimeImmutable($data['date']),
);
}

public function toArray(): array
{
return [
'name' => $this->name,
'email' => $this->email,
'date' => $this->date->format('c'),
];
}
}
76 changes: 76 additions & 0 deletions src/DataTransferObjects/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace ConduitUI\Pr\DataTransferObjects;

class File
{
public function __construct(
public readonly string $sha,
public readonly string $filename,
public readonly string $status,
public readonly int $additions,
public readonly int $deletions,
public readonly int $changes,
public readonly string $blobUrl,
public readonly string $rawUrl,
public readonly string $contentsUrl,
public readonly ?string $patch,
public readonly ?string $previousFilename,
) {}

public static function fromArray(array $data): self
{
return new self(
sha: $data['sha'],
filename: $data['filename'],
status: $data['status'],
additions: $data['additions'],
deletions: $data['deletions'],
changes: $data['changes'],
blobUrl: $data['blob_url'],
rawUrl: $data['raw_url'],
contentsUrl: $data['contents_url'],
patch: $data['patch'] ?? null,
previousFilename: $data['previous_filename'] ?? null,
);
}

public function isAdded(): bool
{
return $this->status === 'added';
}

public function isRemoved(): bool
{
return $this->status === 'removed';
}

public function isModified(): bool
{
return $this->status === 'modified';
}

public function isRenamed(): bool
{
return $this->status === 'renamed';
}

public function toArray(): array
{
return [
'sha' => $this->sha,
'filename' => $this->filename,
'status' => $this->status,
'additions' => $this->additions,
'deletions' => $this->deletions,
'changes' => $this->changes,
'blob_url' => $this->blobUrl,
'raw_url' => $this->rawUrl,
'contents_url' => $this->contentsUrl,
'patch' => $this->patch,
'previous_filename' => $this->previousFilename,
];
}
}
27 changes: 21 additions & 6 deletions src/PullRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace ConduitUI\Pr;

use ConduitUi\GitHubConnector\Connector;
use ConduitUI\Pr\DataTransferObjects\CheckRun;
use ConduitUI\Pr\DataTransferObjects\Comment;
use ConduitUI\Pr\DataTransferObjects\Commit;
use ConduitUI\Pr\DataTransferObjects\File;
use ConduitUI\Pr\DataTransferObjects\PullRequest as PullRequestData;
use ConduitUI\Pr\DataTransferObjects\Review;
use ConduitUI\Pr\Requests\AddIssueLabels;
Expand Down Expand Up @@ -182,7 +185,7 @@ public function comments(): array
}

/**
* @return array<int, array<string, mixed>>
* @return array<int, File>
*/
public function files(): array
{
Expand All @@ -192,10 +195,14 @@ public function files(): array
$this->data->number
));

return array_values($response->json());
return array_values(array_map(
fn (array $data) => File::fromArray($data),
$response->json()
));
}

/**
/**
* Get the raw diff text for this pull request.
*/
public function diff(): string
Expand All @@ -210,7 +217,7 @@ public function diff(): string
}

/**
* @return array<int, array<string, mixed>>
* @return array<int, CheckRun>
*/
public function checks(): array
{
Expand All @@ -220,13 +227,18 @@ public function checks(): array
$this->data->head->sha
));

return $response->json()['check_runs'] ?? [];
$checkRuns = $response->json()['check_runs'] ?? [];

return array_values(array_map(
fn (array $data) => CheckRun::fromArray($data),
$checkRuns
));
}

/**
* Get all commits in this pull request (paginated, fetches all pages).
*
* @return array<int, array<string, mixed>>
* @return array<int, Commit>
*/
public function commits(): array
{
Expand All @@ -253,7 +265,10 @@ public function commits(): array
$page++;
} while (count($commits) === $perPage);

return array_values($allCommits);
return array_values(array_map(
fn (array $data) => Commit::fromArray($data),
$allCommits
));
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/DataTransferObjects/CommitAuthorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use ConduitUI\Pr\DataTransferObjects\CommitAuthor;

it('can create commit author from array', function () {
$data = [
'name' => 'John Doe',
'email' => 'john@example.com',
'date' => '2025-01-01T10:00:00Z',
];

$author = CommitAuthor::fromArray($data);

expect($author->name)->toBe('John Doe')
->and($author->email)->toBe('john@example.com')
->and($author->date)->toBeInstanceOf(DateTimeImmutable::class)
->and($author->date->format('Y-m-d'))->toBe('2025-01-01');
});

it('can convert commit author to array', function () {
$data = [
'name' => 'Jane Smith',
'email' => 'jane@example.com',
'date' => '2025-01-15T14:30:00Z',
];

$author = CommitAuthor::fromArray($data);
$result = $author->toArray();

expect($result['name'])->toBe('Jane Smith')
->and($result['email'])->toBe('jane@example.com')
->and($result['date'])->toBe('2025-01-15T14:30:00+00:00');
});
Loading