Skip to content

Commit

Permalink
Add gitlab group/namespace filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
richgerdes committed Oct 23, 2018
1 parent f3373cf commit fec9641
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/MBO/SatisGitlab/Command/GitlabToConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use MBO\SatisGitlab\Git\GitlabProject;
use MBO\SatisGitlab\Filter\FilterCollection;

use MBO\SatisGitlab\Filter\GitlabNamespaceFilter;
use MBO\SatisGitlab\Filter\IgnoreRegexpFilter;
use MBO\SatisGitlab\Filter\IncludeIfHasFileFilter;
use MBO\SatisGitlab\Filter\ProjectTypeFilter;
Expand Down Expand Up @@ -63,6 +64,7 @@ protected function configure() {
->addOption('ignore', 'i', InputOption::VALUE_REQUIRED, 'ignore project according to a regexp, for ex : "(^phpstorm|^typo3\/library)"', null)
->addOption('include-if-has-file',null,InputOption::VALUE_REQUIRED, 'include in satis config if project contains a given file, for ex : ".satisinclude"', null)
->addOption('project-type',null,InputOption::VALUE_REQUIRED, 'include in satis config if project is of a specified type, for ex : "library"', null)
->addOption('gitlab-groups',null,InputOption::VALUE_REQUIRED, 'include in satis config if gitlab project namespace is in the list, for ex : "2,Diaspora"', null)
/*
* satis config generation options
*/
Expand Down Expand Up @@ -131,6 +133,12 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$logger
));
}
/* project-type option */
if ( ! empty($input->getOption('gitlab-groups')) ){
$filterCollection->addFilter(new GitlabNamespaceFilter(
$input->getOption('gitlab-groups')
));
}

/*
* Create configuration builder
Expand Down
64 changes: 64 additions & 0 deletions src/MBO/SatisGitlab/Filter/GitlabNamespaceFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace MBO\SatisGitlab\Filter;

use MBO\SatisGitlab\Git\ProjectInterface;
use MBO\SatisGitlab\Git\ClientInterface as GitClientInterface;
use Psr\Log\LoggerInterface;

/**
* Filter projects based on GitLab project namespace name or id.
*/
class GitlabNamespaceFilter implements ProjectFilterInterface {
/**
* @var string[]
*/
protected $groups;

/**
* @var GitClientInterface
*/
protected $gitClient;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* GitlabNamespaceFilter constructor.
*
* @param string $type
* @param GitClientInterface $gitClient
* @param LoggerInterface $logger
*/
public function __construct($groups)
{
assert(!empty($groups));
$this->groups = explode(',',strtolower($groups));
}

/**
* {@inheritDoc}
*/
public function isAccepted(ProjectInterface $project)
{
$project_info = $project->getRawMetadata();
if (isset($project_info['namespace'])) {

// Extra data from namespace to patch on.
$valid_keys = [
'name' => 'name',
'id' => 'id',
];
$namespace_info = array_intersect_key($project_info['namespace'], $valid_keys);
$namespace_info = array_map('strtolower', $namespace_info);

if (!empty($namespace_info) && !empty(array_intersect($namespace_info, $this->groups))) {
// Accept any package with a permitted namespace name or id.
return TRUE;
}
}
return FALSE;
}
}

0 comments on commit fec9641

Please sign in to comment.