Skip to content

Commit ca33d73

Browse files
committed
fix: enforce prefix search semantics and fix Gogs branch existence scan
1 parent a63ae11 commit ca33d73

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,14 @@ private function listBranchesPage(string $owner, string $repositoryName, int $pe
846846
$pageInfo = $refs['pageInfo'] ?? [];
847847
$hasNext = $pageInfo['hasNextPage'] ?? false;
848848

849+
// GitHub's query param does substring matching; post-filter to enforce prefix semantics.
850+
$names = array_map(fn ($branch) => $branch['name'] ?? '', $refs['nodes'] ?? []);
851+
if ($search !== '') {
852+
$names = array_values(array_filter($names, fn ($name) => str_starts_with($name, $search)));
853+
}
854+
849855
return [
850-
'items' => array_values(array_map(fn ($branch) => $branch['name'] ?? '', $refs['nodes'] ?? [])),
856+
'items' => array_values($names),
851857
'hasNext' => $hasNext,
852858
'nextCursor' => $hasNext ? ($pageInfo['endCursor'] ?? null) : null,
853859
];

src/VCS/Adapter/Git/Gogs.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,18 @@ public function getCommit(string $owner, string $repositoryName, string $commitH
260260
*/
261261
public function getLatestCommit(string $owner, string $repositoryName, string $branch): array
262262
{
263-
// Gogs ignores sha param — verify branch exists first
264-
$result = $this->listBranches($owner, $repositoryName);
265-
if (!in_array($branch, $result['items'], true)) {
263+
// Gogs ignores sha param — verify branch exists by scanning all pages
264+
$page = 1;
265+
$found = false;
266+
do {
267+
$result = $this->listBranches($owner, $repositoryName, 100, $page);
268+
if (in_array($branch, $result['items'], true)) {
269+
$found = true;
270+
break;
271+
}
272+
$page++;
273+
} while ($result['hasNext']);
274+
if (!$found) {
266275
throw new Exception("Branch '{$branch}' not found");
267276
}
268277

0 commit comments

Comments
 (0)