-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mborne/issue_20
Add option --include-if-has-file=the-given-file
- Loading branch information
Showing
15 changed files
with
354 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Filter; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use MBO\SatisGitlab\Git\ProjectInterface; | ||
|
||
/** | ||
* Compose a list of filter to simplify command line integration | ||
*/ | ||
class FilterCollection implements ProjectFilterInterface { | ||
|
||
/** | ||
* @var ProjectFilterInterface[] | ||
*/ | ||
private $filters; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(LoggerInterface $logger){ | ||
$this->filters = array(); | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Add a filter to the collection | ||
* | ||
* @param ProjectFilterInterface $filter | ||
* @return void | ||
*/ | ||
public function addFilter(ProjectFilterInterface $filter){ | ||
$this->filters[] = $filter; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isAccepted(ProjectInterface $project){ | ||
foreach ( $this->filters as $filter ){ | ||
if ( ! $filter->isAccepted($project) ){ | ||
$this->logger->debug(sprintf( | ||
"[%s]Ignoring project %s", | ||
get_class($filter), | ||
$project->getName() | ||
)); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Filter; | ||
|
||
use MBO\SatisGitlab\Git\ProjectInterface; | ||
|
||
/** | ||
* Ignore project according to a regular expression | ||
*/ | ||
class IgnoreRegexpFilter implements ProjectFilterInterface { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $ignoreRegexp; | ||
|
||
public function __construct($ignoreRegexp) | ||
{ | ||
assert(!empty($ignoreRegexp)); | ||
$this->ignoreRegexp = $ignoreRegexp; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isAccepted(ProjectInterface $project) | ||
{ | ||
if ( preg_match("/$this->ignoreRegexp/", $project->getName() ) ){ | ||
return false; | ||
}else{ | ||
return true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace MBO\SatisGitlab\Filter; | ||
|
||
use MBO\SatisGitlab\Git\ProjectInterface; | ||
|
||
|
||
/** | ||
* Test if a project should be included in satis config (regexp, ) | ||
*/ | ||
interface ProjectFilterInterface { | ||
|
||
/** | ||
* Returns true if the project should be included in satis configuration | ||
* | ||
* @param ProjectInterface $project | ||
* @return boolean | ||
*/ | ||
public function isAccepted(ProjectInterface $project); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Tests\SatisGitlab\Filter; | ||
|
||
use Tests\SatisGitlab\TestCase; | ||
|
||
use Psr\Log\NullLogger; | ||
|
||
use MBO\SatisGitlab\Filter\FilterCollection; | ||
use MBO\SatisGitlab\Git\ProjectInterface; | ||
use MBO\SatisGitlab\Filter\ProjectFilterInterface; | ||
|
||
/** | ||
* Test FilterCollection | ||
*/ | ||
class FilterCollectionTest extends TestCase { | ||
|
||
public function testEmpty(){ | ||
$filterCollection = new FilterCollection(new NullLogger()); | ||
$project = $this->createMockProject('test'); | ||
$this->assertTrue($filterCollection->isAccepted($project)); | ||
} | ||
|
||
/** | ||
* Create a fake project filter returning true or false | ||
* | ||
* @param boolean $accepted | ||
* @return ProjectFilterInterface | ||
*/ | ||
private function createMockFilter($accepted){ | ||
$filter = $this->getMockBuilder(ProjectFilterInterface::class) | ||
->getMock() | ||
; | ||
$filter->expects($this->any()) | ||
->method('isAccepted') | ||
->willReturn($accepted) | ||
; | ||
return $filter; | ||
} | ||
|
||
|
||
public function testOneTrue(){ | ||
$filterCollection = new FilterCollection(new NullLogger()); | ||
$filterCollection->addFilter($this->createMockFilter(true)); | ||
$project = $this->createMockProject('test'); | ||
$this->assertTrue($filterCollection->isAccepted($project)); | ||
} | ||
|
||
public function testOneFalse(){ | ||
$filterCollection = new FilterCollection(new NullLogger()); | ||
$filterCollection->addFilter($this->createMockFilter(false)); | ||
$project = $this->createMockProject('test'); | ||
$this->assertFalse($filterCollection->isAccepted($project)); | ||
} | ||
|
||
/** | ||
* Check that isAccepted is unanymous | ||
*/ | ||
public function testTrueFalseTrue(){ | ||
$filterCollection = new FilterCollection(new NullLogger()); | ||
$filterCollection->addFilter($this->createMockFilter(true)); | ||
$filterCollection->addFilter($this->createMockFilter(false)); | ||
$filterCollection->addFilter($this->createMockFilter(true)); | ||
$project = $this->createMockProject('test'); | ||
$this->assertFalse($filterCollection->isAccepted($project)); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.