Skip to content

feat: Add option to generate Gitlab compatible endpoints #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
42 changes: 35 additions & 7 deletions src/GenerateFlexEndpointCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,32 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'generate:flex-endpoint', description: 'Generates the json files required by Flex')]
class GenerateFlexEndpointCommand extends Command
{
private const PROVIDER_GITHUB = 'github';
private const PROVIDER_GITLAB = 'gitlab';

protected function configure(): void
{
$this
->addArgument('repository', InputArgument::REQUIRED, 'The name of the GitHub repository')
->addArgument('repository', InputArgument::REQUIRED, 'The name of the repository')
->addArgument('source_branch', InputArgument::REQUIRED, 'The source branch of recipes')
->addArgument('flex_branch', InputArgument::REQUIRED, 'The branch of the target Flex endpoint')
->addArgument('output_directory', InputArgument::REQUIRED, 'The directory where generated files should be stored')
->addArgument('versions_json', InputArgument::OPTIONAL, 'The file where versions of Symfony are described')
->addOption('contrib')
->addOption(
'provider',
null,
InputOption::VALUE_REQUIRED,
'Provider of the repository (github or gitlab)',
self::PROVIDER_GITHUB,
[self::PROVIDER_GITHUB, self::PROVIDER_GITLAB]
)
;
}

Expand All @@ -38,6 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$outputDir = $input->getArgument('output_directory');
$versionsJson = $input->getArgument('versions_json');
$contrib = $input->getOption('contrib');
$provider = $input->getOption('provider');

$aliases = $recipes = $recipeConflicts = $versions = [];

Expand Down Expand Up @@ -99,6 +112,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
ksort($recipes, \SORT_NATURAL);
ksort($recipeConflicts, \SORT_NATURAL);

switch ($provider) {
case self::PROVIDER_GITHUB:
$baseHost = 'github.com';
$recipeTemplate = sprintf('https://raw.githubusercontent.com/%s/%s/{package_dotted}.{version}.json', $repository, $flexBranch);
$archivedRecipesTemplate = sprintf('https://raw.githubusercontent.com/%s/%s/archived/{package_dotted}/{ref}.json', $repository, $flexBranch);
break;
case self::PROVIDER_GITLAB:
$baseHost = 'gitlab.com';
$recipeTemplate = sprintf('https://gitlab.com/api/v4/projects/%s/repository/files/{package_dotted}.{version}.json/raw?ref=%s', urlencode($repository), $flexBranch);
$archivedRecipesTemplate = sprintf('https://gitlab.com/api/v4/projects/%s/repository/files/archived%%2F{package_dotted}%%2F{ref}.json/raw?ref=%s', urlencode($repository), $flexBranch);
break;
default:
throw new \InvalidArgumentException(sprintf('Unsupported provider "%s".', $provider));
}

file_put_contents($outputDir.'/index.json', json_encode([
'aliases' => $aliases,
'recipes' => $recipes,
Expand All @@ -107,12 +135,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'branch' => $sourceBranch,
'is_contrib' => $contrib,
'_links' => [
'repository' => sprintf('github.com/%s', $repository),
'origin_template' => sprintf('{package}:{version}@github.com/%s:%s', $repository, $sourceBranch),
'recipe_template' => sprintf('https://raw.githubusercontent.com/%s/%s/{package_dotted}.{version}.json', $repository, $flexBranch),
'recipe_template_relative' => sprintf('{package_dotted}.{version}.json', $repository, $flexBranch),
'archived_recipes_template' => sprintf('https://raw.githubusercontent.com/%s/%s/archived/{package_dotted}/{ref}.json', $repository, $flexBranch),
'archived_recipes_template_relative' => sprintf('archived/{package_dotted}/{ref}.json', $repository, $flexBranch),
'repository' => sprintf('%s/%s', $baseHost, $repository),
'origin_template' => sprintf('{package}:{version}@%s/%s:%s', $baseHost, $repository, $sourceBranch),
'recipe_template' => $recipeTemplate,
'recipe_template_relative' => sprintf('{package_dotted}.{version}.json'),
'archived_recipes_template' => $archivedRecipesTemplate,
'archived_recipes_template_relative' => sprintf('archived/{package_dotted}/{ref}.json'),
],
], \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)."\n");

Expand Down