Skip to content

Commit 1ca158d

Browse files
committed
feat: add getCheckRunByName
1 parent 37f11ca commit 1ca158d

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

src/VCS/Adapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ public function createCheckRun(
266266
throw new \Exception('createCheckRun() is not implemented for ' . $this->getName());
267267
}
268268

269+
public function getCheckRunByName(string $owner, string $repositoryName, string $ref, string $checkName): int
270+
{
271+
throw new \Exception('getCheckRunByName() is not implemented for ' . $this->getName());
272+
}
273+
269274
/**
270275
* Finds the most recent check run on a commit by name.
271276
* Returns the check run ID, or 0 if none found.

src/VCS/Adapter/Git/GitHub.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,26 @@ public function createCheckRun(
955955
return $response['body'] ?? [];
956956
}
957957

958+
public function getCheckRunByName(string $owner, string $repositoryName, string $ref, string $checkName): int
959+
{
960+
$url = "/repos/$owner/$repositoryName/commits/$ref/check-runs";
961+
962+
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "Bearer $this->accessToken"], [
963+
'check_name' => $checkName,
964+
'filter' => 'latest',
965+
'per_page' => 1,
966+
]);
967+
968+
$responseHeadersStatusCode = $response['headers']['status-code'] ?? 0;
969+
if ($responseHeadersStatusCode >= 400) {
970+
return 0;
971+
}
972+
973+
$runs = $response['body']['check_runs'] ?? [];
974+
975+
return (int) ($runs[0]['id'] ?? 0);
976+
}
977+
958978
/**
959979
* Finds the most recent check run on a commit by name.
960980
* Returns the check run ID, or 0 if none found.

tests/VCS/Adapter/GitHubTest.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,4 +1279,163 @@ public function testUpdateComment(): void
12791279
{
12801280
$this->markTestSkipped('Requires existing PR — createPullRequest not implemented in GitHub adapter');
12811281
}
1282+
1283+
public function testGetCheckRunByName(): void
1284+
{
1285+
$repositoryName = 'test-get-check-run-by-name-' . \uniqid();
1286+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
1287+
1288+
try {
1289+
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1290+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
1291+
$commitHash = $commit['commitHash'];
1292+
1293+
$checkRun = $this->vcsAdapter->createCheckRun(
1294+
owner: static::$owner,
1295+
repositoryName: $repositoryName,
1296+
headSha: $commitHash,
1297+
name: 'ci/build',
1298+
status: 'in_progress',
1299+
);
1300+
1301+
$foundId = $this->vcsAdapter->getCheckRunByName(
1302+
static::$owner,
1303+
$repositoryName,
1304+
$commitHash,
1305+
'ci/build'
1306+
);
1307+
1308+
$this->assertEquals($checkRun['id'], $foundId);
1309+
} finally {
1310+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
1311+
}
1312+
}
1313+
1314+
public function testGetCheckRunByNameNoMatchReturnsZero(): void
1315+
{
1316+
$repositoryName = 'test-get-check-run-by-name-nomatch-' . \uniqid();
1317+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
1318+
1319+
try {
1320+
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1321+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
1322+
$commitHash = $commit['commitHash'];
1323+
1324+
$this->vcsAdapter->createCheckRun(
1325+
owner: static::$owner,
1326+
repositoryName: $repositoryName,
1327+
headSha: $commitHash,
1328+
name: 'ci/build',
1329+
status: 'in_progress',
1330+
);
1331+
1332+
$foundId = $this->vcsAdapter->getCheckRunByName(
1333+
static::$owner,
1334+
$repositoryName,
1335+
$commitHash,
1336+
'ci/lint'
1337+
);
1338+
1339+
$this->assertEquals(0, $foundId);
1340+
} finally {
1341+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
1342+
}
1343+
}
1344+
1345+
public function testGetCheckRunByNameInvalidRepositoryReturnsZero(): void
1346+
{
1347+
$foundId = $this->vcsAdapter->getCheckRunByName(
1348+
static::$owner,
1349+
'non-existing-repository-' . \uniqid(),
1350+
str_repeat('a', 40),
1351+
'ci/build'
1352+
);
1353+
1354+
$this->assertEquals(0, $foundId);
1355+
}
1356+
1357+
public function testGetCheckRunByNameReturnsMostRecent(): void
1358+
{
1359+
$repositoryName = 'test-get-check-run-by-name-recent-' . \uniqid();
1360+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
1361+
1362+
try {
1363+
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1364+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
1365+
$commitHash = $commit['commitHash'];
1366+
1367+
$first = $this->vcsAdapter->createCheckRun(
1368+
owner: static::$owner,
1369+
repositoryName: $repositoryName,
1370+
headSha: $commitHash,
1371+
name: 'ci/build',
1372+
status: 'in_progress',
1373+
);
1374+
1375+
$second = $this->vcsAdapter->createCheckRun(
1376+
owner: static::$owner,
1377+
repositoryName: $repositoryName,
1378+
headSha: $commitHash,
1379+
name: 'ci/build',
1380+
status: 'in_progress',
1381+
);
1382+
1383+
$this->assertGreaterThan($first['id'], $second['id']);
1384+
1385+
$foundId = $this->vcsAdapter->getCheckRunByName(
1386+
static::$owner,
1387+
$repositoryName,
1388+
$commitHash,
1389+
'ci/build'
1390+
);
1391+
1392+
$this->assertEquals($second['id'], $foundId);
1393+
} finally {
1394+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
1395+
}
1396+
}
1397+
1398+
public function testGetCheckRunByNameThenUpdate(): void
1399+
{
1400+
$repositoryName = 'test-get-check-run-by-name-update-' . \uniqid();
1401+
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
1402+
1403+
try {
1404+
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1405+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
1406+
$commitHash = $commit['commitHash'];
1407+
1408+
$this->vcsAdapter->createCheckRun(
1409+
owner: static::$owner,
1410+
repositoryName: $repositoryName,
1411+
headSha: $commitHash,
1412+
name: 'ci/build',
1413+
status: 'in_progress',
1414+
);
1415+
1416+
$checkRunId = $this->vcsAdapter->getCheckRunByName(
1417+
static::$owner,
1418+
$repositoryName,
1419+
$commitHash,
1420+
'ci/build'
1421+
);
1422+
1423+
$this->assertGreaterThan(0, $checkRunId);
1424+
1425+
$updated = $this->vcsAdapter->updateCheckRun(
1426+
owner: static::$owner,
1427+
repositoryName: $repositoryName,
1428+
checkRunId: $checkRunId,
1429+
conclusion: 'success',
1430+
title: 'Build succeeded.',
1431+
summary: 'All steps passed.',
1432+
);
1433+
1434+
$this->assertEquals($checkRunId, $updated['id']);
1435+
$this->assertEquals('completed', $updated['status']);
1436+
$this->assertEquals('success', $updated['conclusion']);
1437+
} finally {
1438+
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
1439+
}
1440+
}
12821441
}

0 commit comments

Comments
 (0)