Skip to content

Commit f5fe277

Browse files
committed
feat: expand createCheckRun params, add getCheckRun, improve test assertions
1 parent 83b83d6 commit f5fe277

3 files changed

Lines changed: 111 additions & 25 deletions

File tree

src/VCS/Adapter.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,40 @@ abstract public function listBranches(string $owner, string $repositoryName): ar
236236
abstract public function updateCommitStatus(string $repositoryName, string $SHA, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void;
237237

238238
/**
239-
* Creates a completed check run for a commit.
240-
* conclusion can be one of: action_required, cancelled, failure, neutral, success, skipped, timed_out
239+
* Creates a check run for a commit.
240+
* status can be one of: queued, in_progress, completed
241+
* conclusion (required when status=completed) can be one of: action_required, cancelled, failure, neutral, success, skipped, timed_out
242+
*
243+
* @return array<mixed>
241244
*/
242-
public function createCheckRun(string $owner, string $repositoryName, string $headSha, string $name, string $conclusion, string $title, string $summary): void
243-
{
245+
public function createCheckRun(
246+
string $owner,
247+
string $repositoryName,
248+
string $headSha,
249+
string $name,
250+
string $status = 'queued',
251+
string $conclusion = '',
252+
string $title = '',
253+
string $summary = '',
254+
string $text = '',
255+
string $detailsUrl = '',
256+
string $externalId = '',
257+
string $startedAt = '',
258+
string $completedAt = '',
259+
): array {
244260
throw new \Exception('createCheckRun() is not implemented for ' . $this->getName());
245261
}
246262

263+
/**
264+
* Gets a check run by ID.
265+
*
266+
* @return array<mixed>
267+
*/
268+
public function getCheckRun(string $owner, string $repositoryName, int $checkRunId): array
269+
{
270+
throw new \Exception('getCheckRun() is not implemented for ' . $this->getName());
271+
}
272+
247273
/**
248274
* Get repository tree
249275
*

src/VCS/Adapter/Git/GitHub.php

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -873,26 +873,77 @@ public function updateCommitStatus(string $repositoryName, string $commitHash, s
873873
}
874874

875875
/**
876-
* Creates a completed check run for a commit.
877-
* conclusion can be one of: action_required, cancelled, failure, neutral, success, skipped, timed_out
876+
* Creates a check run for a commit.
877+
* status can be one of: queued, in_progress, completed
878+
* conclusion (required when status=completed) can be one of: action_required, cancelled, failure, neutral, success, skipped, timed_out
879+
*
880+
* @return array<mixed>
878881
*/
879-
public function createCheckRun(string $owner, string $repositoryName, string $headSha, string $name, string $conclusion, string $title, string $summary): void
880-
{
882+
public function createCheckRun(
883+
string $owner,
884+
string $repositoryName,
885+
string $headSha,
886+
string $name,
887+
string $status = 'queued',
888+
string $conclusion = '',
889+
string $title = '',
890+
string $summary = '',
891+
string $text = '',
892+
string $detailsUrl = '',
893+
string $externalId = '',
894+
string $startedAt = '',
895+
string $completedAt = '',
896+
): array {
881897
$url = "/repos/$owner/$repositoryName/check-runs";
882898

883899
$body = [
884900
'name' => $name,
885901
'head_sha' => $headSha,
886-
'status' => 'completed',
887-
'conclusion' => $conclusion,
888-
'completed_at' => gmdate('Y-m-d\TH:i:s\Z'),
889-
'output' => [
902+
'status' => $status,
903+
];
904+
905+
if (!empty($detailsUrl)) {
906+
$body['details_url'] = $detailsUrl;
907+
}
908+
if (!empty($externalId)) {
909+
$body['external_id'] = $externalId;
910+
}
911+
if (!empty($startedAt)) {
912+
$body['started_at'] = $startedAt;
913+
}
914+
if (!empty($conclusion)) {
915+
$body['conclusion'] = $conclusion;
916+
}
917+
if (!empty($completedAt)) {
918+
$body['completed_at'] = $completedAt;
919+
}
920+
if (!empty($title) || !empty($summary)) {
921+
$body['output'] = [
890922
'title' => $title,
891923
'summary' => $summary,
892-
],
893-
];
924+
];
925+
if (!empty($text)) {
926+
$body['output']['text'] = $text;
927+
}
928+
}
894929

895-
$this->call(self::METHOD_POST, $url, ['Authorization' => "Bearer $this->accessToken"], $body);
930+
$response = $this->call(self::METHOD_POST, $url, ['Authorization' => "Bearer $this->accessToken"], $body);
931+
932+
return $response['body'] ?? [];
933+
}
934+
935+
/**
936+
* Gets a check run by ID.
937+
*
938+
* @return array<mixed>
939+
*/
940+
public function getCheckRun(string $owner, string $repositoryName, int $checkRunId): array
941+
{
942+
$url = "/repos/$owner/$repositoryName/check-runs/$checkRunId";
943+
944+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"]);
945+
946+
return $response['body'] ?? [];
896947
}
897948

898949
/**

tests/VCS/Adapter/GitHubTest.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -659,18 +659,27 @@ public function testCreateCheckRun(): void
659659
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
660660
$commitHash = $commit['commitHash'];
661661

662-
// Should not throw
663-
$this->vcsAdapter->createCheckRun(
664-
static::$owner,
665-
$repositoryName,
666-
$commitHash,
667-
'ci/build',
668-
'neutral',
669-
'Deployment skipped',
670-
'Deployment skipped because the commit message contains a skip pattern.'
662+
$checkRun = $this->vcsAdapter->createCheckRun(
663+
owner: static::$owner,
664+
repositoryName: $repositoryName,
665+
headSha: $commitHash,
666+
name: 'ci/build',
667+
status: 'completed',
668+
conclusion: 'neutral',
669+
title: 'Deployment skipped',
670+
summary: 'Deployment skipped because the commit message contains a skip pattern.',
671+
completedAt: gmdate('Y-m-d\TH:i:s\Z'),
671672
);
672673

673-
$this->assertTrue(true);
674+
$this->assertArrayHasKey('id', $checkRun);
675+
$this->assertEquals('completed', $checkRun['status']);
676+
$this->assertEquals('neutral', $checkRun['conclusion']);
677+
$this->assertEquals('ci/build', $checkRun['name']);
678+
679+
$fetched = $this->vcsAdapter->getCheckRun(static::$owner, $repositoryName, $checkRun['id']);
680+
$this->assertEquals($checkRun['id'], $fetched['id']);
681+
$this->assertEquals('neutral', $fetched['conclusion']);
682+
$this->assertEquals('completed', $fetched['status']);
674683
} finally {
675684
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
676685
}

0 commit comments

Comments
 (0)