public function getDefaultBranches(array $repositories): array
{
$result = [];
foreach (array_chunk($repositories, self::DEFAULT_BRANCH_BATCH_SIZE, true) as $batch) {
$fields = [];
$variables = [];
$keys = [];
$index = 0;
foreach ($batch as $key => $repository) {
$alias = 'repository' . $index;
$keys[$alias] = $key;
$variables["vendor$index"] = $repository['vendor'];
$variables["name$index"] = $repository['repository'];
$fields[] = <<<GRAPHQL
$alias: repository(owner: \$vendor$index, name: \$name$index) {
defaultBranchRef { name target { oid } }
}
GRAPHQL;
$index++;
}
$definitions = [];
for ($i = 0; $i < $index; $i++) {
$definitions[] = "\$vendor$i: String!";
$definitions[] = "\$name$i: String!";
}
$query = sprintf(
"query (%s) {\n%s\n}",
implode(', ', $definitions),
$this->indent(implode("\n", $fields), 2),
);
$data = $this->client->graphql()->execute($query, $variables)['data'] ?? [];
foreach ($keys as $alias => $key) {
$defaultBranch = $data[$alias]['defaultBranchRef'] ?? null;
if ($defaultBranch === null) {
throw new RuntimeException("GitHub default branch data is unavailable for $key.");
}
$result[$key] = [
'branch' => $defaultBranch['name'],
'sha' => $defaultBranch['target']['oid'],
];
}
}
return $result;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.