Skip to content

Commit

Permalink
add option --include-if-has-file=the-given-file (closes #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Aug 25, 2018
1 parent 8c14ed8 commit b33d801
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/MBO/SatisGitlab/Command/GitlabToConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use MBO\SatisGitlab\Git\ClientOptions;
use MBO\SatisGitlab\Git\GitlabProject;
use MBO\SatisGitlab\Filter\FilterCollection;
use MBO\SatisGitlab\Filter\IncludeIfHasFileFilter;



Expand Down Expand Up @@ -57,7 +58,7 @@ protected function configure() {
* Project filters
*/
->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)
/*
* satis config generation options
*/
Expand Down Expand Up @@ -95,10 +96,29 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$logger
);


$outputFile = $input->getOption('output');

// warning : this one is a "git client filter"
$projectFilter = $input->getOption('projectFilter');
$filterCollection = $this->createFilterCollection($input,$logger);

/*
* Create project filters according to input arguments
*/
$filterCollection = new FilterCollection($logger);
/* ignore option */
if ( ! empty($input->getOption('ignore')) ){
$filterCollection->addFilter(new IgnoreRegexpFilter(
$input->getOption('ignore')
));
}
/* include-if-has-file option */
if ( ! empty($input->getOption('include-if-has-file')) ){
$filterCollection->addFilter(new IncludeIfHasFileFilter(
$client,
$input->getOption('include-if-has-file'),
$logger
));
}

/*
* Create configuration builder
Expand Down Expand Up @@ -230,23 +250,6 @@ protected function createProjectMessage(
);
}

/**
* Create FilterCollection according to input arguments
*
* @param InputInterface $input
* @param LoggerInterface $logger
* @return FilterCollection
*/
protected function createFilterCollection(InputInterface $input, LoggerInterface $logger){
$filterCollection = new FilterCollection($logger);
/* ignore option */
if ( ! empty($input->getOption('ignore')) ){
$filterCollection->addFilter(new IgnoreRegexpFilter($input->getOption('ignore')));
}

return $filterCollection;
}

/**
* Create console logger
* @param OutputInterface $output
Expand Down
60 changes: 60 additions & 0 deletions src/MBO/SatisGitlab/Filter/IncludeIfHasFileFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace MBO\SatisGitlab\Filter;

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

/**
* Accept projects if git contains a given file
*
* TODO add mock based unit tests
*/
class IncludeIfHasFileFilter implements ProjectFilterInterface {

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

/**
* @var string
*/
protected $filePath;

public function __construct(
GitClientInterface $gitClient,
$filePath,
LoggerInterface $logger
)
{
$this->gitClient = $gitClient;
$this->filePath = $filePath;
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
public function isAccepted(ProjectInterface $project)
{
try {
$this->gitClient->getRawFile(
$project,
$this->filePath,
$project->getDefaultBranch()
);
return true;
}catch(\Exception $e){
$this->logger->debug(sprintf(
'%s (branch %s) : file %s not found',
$project->getName(),
$project->getDefaultBranch(),
$this->filePath
));
return false;
}
}

}
1 change: 1 addition & 0 deletions tests/Command/GitlabToConfigCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testWithFilter(){
'gitlab-url' => 'http://gitlab.com',
'gitlab-token' => $gitlabToken,
'--projectFilter' => 'sample-composer',
'--include-if-has-file' => 'README.md',
'--output' => $this->outputFile
));

Expand Down

0 comments on commit b33d801

Please sign in to comment.