diff --git a/src/Platform/Github/Github.php b/src/Platform/Github/Github.php index dd29c5c..7a28ee8 100644 --- a/src/Platform/Github/Github.php +++ b/src/Platform/Github/Github.php @@ -6,10 +6,11 @@ use Danger\Config; use Danger\Platform\AbstractPlatform; use Danger\Struct\Github\PullRequest as GithubPullRequest; +use Danger\Struct\PullRequest; use Github\Client; /** - * @property array{'title': string, 'body': ?string, 'labels': array{'name': string}[], 'assignees': array{'login': string}[], 'requested_reviewers': array{'login': string}[], 'created_at': string, 'updated_at': string} $raw + * @property array{'title': string, 'body': ?string, 'state': string, 'merged': bool, 'labels': array{'name': string}[], 'assignees': array{'login': string}[], 'requested_reviewers': array{'login': string}[], 'created_at': string, 'updated_at': string} $raw */ class Github extends AbstractPlatform { @@ -27,7 +28,7 @@ public function load(string $projectIdentifier, string $id): void $this->githubOwner = $owner; $this->githubRepository = $repository; - /** @var array{'title': string, 'body': ?string, 'labels': array{'name': string}[], 'assignees': array{'login': string}[], 'requested_reviewers': array{'login': string}[], 'created_at': string, 'updated_at': string, head: array{sha: string}} $raw */ + /** @var array{'title': string, 'body': ?string, 'state': string, 'merged': bool, 'labels': array{'name': string}[], 'assignees': array{'login': string}[], 'requested_reviewers': array{'login': string}[], 'created_at': string, 'updated_at': string, head: array{sha: string}} $raw */ $raw = $this->client->pullRequest()->show($owner, $repository, (int) $id); $this->raw = $raw; @@ -41,6 +42,9 @@ public function load(string $projectIdentifier, string $id): void $this->pullRequest->assignees = array_map(static fn (array $assignee): string => $assignee['login'], $this->raw['assignees'] ); $this->pullRequest->reviewers = $this->getReviews($owner, $repository, $id); + $this->pullRequest->state = $this->raw['merged'] + ? PullRequest::STATE_MERGED + : ($this->raw['state'] === 'open' ? PullRequest::STATE_OPEN : PullRequest::STATE_CLOSED); $this->pullRequest->createdAt = new \DateTime($this->raw['created_at']); $this->pullRequest->updatedAt = new \DateTime($this->raw['updated_at']); } diff --git a/src/Platform/Gitlab/Gitlab.php b/src/Platform/Gitlab/Gitlab.php index 197ba4b..e72f422 100644 --- a/src/Platform/Gitlab/Gitlab.php +++ b/src/Platform/Gitlab/Gitlab.php @@ -6,10 +6,11 @@ use Danger\Config; use Danger\Platform\AbstractPlatform; use Danger\Struct\Gitlab\PullRequest; +use Danger\Struct\PullRequest as BasePullRequest; use Gitlab\Client; /** - * @property array{'sha': string, 'title': string, 'web_url': string, 'description': string|null, 'labels': string[], 'assignees': array{'username': string}[], 'reviewers': array{'username': string}[], 'created_at': string, 'updated_at': string} $raw + * @property array{'sha': string, 'title': string, 'web_url': string, 'description': string|null, 'state': string, 'labels': string[], 'assignees': array{'username': string}[], 'reviewers': array{'username': string}[], 'created_at': string, 'updated_at': string} $raw */ class Gitlab extends AbstractPlatform { @@ -23,7 +24,7 @@ public function load(string $projectIdentifier, string $id): void { $this->projectIdentifier = $projectIdentifier; - /** @var array{'sha': string, 'title': string, 'web_url': string, 'description': string|null, 'labels': string[], 'assignees': array{'username': string}[], 'reviewers': array{'username': string}[], 'created_at': string, 'updated_at': string} $res */ + /** @var array{'sha': string, 'title': string, 'web_url': string, 'description': string|null, 'state': string, 'labels': string[], 'assignees': array{'username': string}[], 'reviewers': array{'username': string}[], 'created_at': string, 'updated_at': string} $res */ $res = $this->client->mergeRequests()->show($projectIdentifier, (int) $id); $this->raw = $res; @@ -35,6 +36,11 @@ public function load(string $projectIdentifier, string $id): void $this->pullRequest->labels = $this->raw['labels']; $this->pullRequest->assignees = array_map(static fn (array $assignee) => $assignee['username'], $this->raw['assignees']); $this->pullRequest->reviewers = array_map(static fn (array $reviewer) => $reviewer['username'], $this->raw['reviewers']); + $this->pullRequest->state = match ($this->raw['state']) { + 'merged' => BasePullRequest::STATE_MERGED, + 'closed' => BasePullRequest::STATE_CLOSED, + default => BasePullRequest::STATE_OPEN, + }; $this->pullRequest->createdAt = new \DateTime($this->raw['created_at']); $this->pullRequest->updatedAt = new \DateTime($this->raw['updated_at']); } diff --git a/src/Struct/PullRequest.php b/src/Struct/PullRequest.php index 2bf9c40..bd0922a 100644 --- a/src/Struct/PullRequest.php +++ b/src/Struct/PullRequest.php @@ -5,6 +5,10 @@ abstract class PullRequest { + public const STATE_OPEN = 'open'; + public const STATE_CLOSED = 'closed'; + public const STATE_MERGED = 'merged'; + public string $id; public string $projectIdentifier; @@ -13,6 +17,8 @@ abstract class PullRequest public string $body; + public string $state = self::STATE_OPEN; + public \DateTimeInterface $createdAt; public \DateTimeInterface $updatedAt; diff --git a/tests/Platform/Github/GithubTest.php b/tests/Platform/Github/GithubTest.php index b32cff2..e7d60c3 100644 --- a/tests/Platform/Github/GithubTest.php +++ b/tests/Platform/Github/GithubTest.php @@ -10,6 +10,7 @@ use Danger\Struct\Comment; use Danger\Struct\Commit; use Danger\Struct\File; +use Danger\Struct\PullRequest; use Github\Client; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpClient\MockHttpClient; @@ -46,6 +47,7 @@ public function testLoad(): void static::assertSame(['Dependencies'], $github->pullRequest->labels); static::assertSame(['shyim'], $github->pullRequest->assignees); static::assertSame(['dangertestuser', 'dangertestuser2'], $github->pullRequest->reviewers); + static::assertSame(PullRequest::STATE_MERGED, $github->pullRequest->state); static::assertSame(1_621_542_059, $github->pullRequest->createdAt->getTimestamp()); static::assertSame(1_621_547_349, $github->pullRequest->updatedAt->getTimestamp()); diff --git a/tests/Platform/Gitlab/GitlabTest.php b/tests/Platform/Gitlab/GitlabTest.php index 601a10d..f781667 100644 --- a/tests/Platform/Gitlab/GitlabTest.php +++ b/tests/Platform/Gitlab/GitlabTest.php @@ -10,6 +10,7 @@ use Danger\Struct\Comment; use Danger\Struct\Commit; use Danger\Struct\File; +use Danger\Struct\PullRequest; use Gitlab\Client; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpClient\MockHttpClient; @@ -42,6 +43,7 @@ public function testLoad(): void static::assertSame(['Test'], $gitlab->pullRequest->labels); static::assertSame(['shyim'], $gitlab->pullRequest->assignees); static::assertSame(['dangertestuser', 'dangertestuser2'], $gitlab->pullRequest->reviewers); + static::assertSame(PullRequest::STATE_OPEN, $gitlab->pullRequest->state); static::assertSame(1_621_638_766, $gitlab->pullRequest->createdAt->getTimestamp()); static::assertSame(1_621_672_778, $gitlab->pullRequest->updatedAt->getTimestamp());