Skip to content

Commit aa96a41

Browse files
committed
feat: add getCheckRunByName to look up check run ID by commit ref and name
1 parent 59b6483 commit aa96a41

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/VCS/Adapter.php

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

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

src/VCS/Adapter/Git/GitHub.php

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

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

0 commit comments

Comments
 (0)