Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 4ebe419

Browse files
Create lock file only flag (#77)
* Upgrade dependencies * Add lock file flag * Add lock file flag test
1 parent 682af7f commit 4ebe419

10 files changed

Lines changed: 443 additions & 393 deletions

composer.lock

Lines changed: 389 additions & 373 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analysis/SnippetAnalysis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function calculateWfp(string $absoluteFilename, string $relativeFilename
7272
return null;
7373
}
7474

75-
/* Gram/Window configuration. Modifying these values would require rehashing the KB */
75+
/* Gram/Window configuration. Modifying these values would require rehashing the KB */
7676
$GRAM = 30;
7777
$WINDOW = 64;
7878
$LIMIT = 10000;

src/Command/CheckScanCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io):
121121
$fgColor = 'blue';
122122
}
123123

124-
$io->text("<fg=${fgColor};options=bold>⨯ The rule triggered, causing ${causingString}</>");
124+
$io->text("<fg={$fgColor};options=bold>⨯ The rule triggered, causing {$causingString}</>");
125125
}
126126

127127
$io->text(' Manage rule: <fg=blue>'.$ruleOutputData['ruleLink'].'</>');
@@ -258,7 +258,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
258258
} elseif ($numRulesChecked === 1) {
259259
$io->text('1 rule was checked:');
260260
} else {
261-
$io->text("${numRulesChecked} rules were checked:");
261+
$io->text("{$numRulesChecked} rules were checked:");
262262
}
263263

264264
foreach ($status['automationRules'] as $rule) {

src/Command/CompoundCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
$findAndUploadOutput = $findAndUploadOutput->fetch();
9090
$uploadIdMatches = [];
9191
if (\preg_match(
92-
"/bin\/console {$checkScanCommand->getName()} your-username your-password (\w+)/m",
93-
$findAndUploadOutput,
94-
$uploadIdMatches
95-
) !== 1) {
92+
"/bin\/console {$checkScanCommand->getName()} your-username your-password (\w+)/m",
93+
$findAndUploadOutput,
94+
$uploadIdMatches
95+
) !== 1) {
9696
return 0;
9797
}
9898
$uploadId = $uploadIdMatches[1];
@@ -136,9 +136,9 @@ private function runFindAndUploadCommand(
136136
$output->getFormatter()
137137
);
138138

139-
//This will return all given options merged with default values for ungiven options
139+
// This will return all given options merged with default values for ungiven options
140140
$options = $input->getOptions();
141-
//Unset option because it does not exist in FindAndUploadFilesCommand
141+
// Unset option because it does not exist in FindAndUploadFilesCommand
142142
unset($options[self::OPTION_DISABLE_CONDITIONAL_SKIP_SCAN]);
143143
$newOptions = [];
144144
foreach ($options as $option => $value) {

src/Command/FindAndUploadFilesCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class FindAndUploadFilesCommand extends Command
5555
public const OPTION_RECURSIVE_FILE_SEARCH = 'recursive-file-search';
5656
private const OPTION_UPLOAD_ALL_FILES = 'upload-all-files';
5757
private const OPTION_AUTHOR = 'author';
58+
private const OPTION_LOCK_FILE_ONLY = 'lockfile';
5859

5960
private HttpClientInterface $debrickedClient;
6061

@@ -167,6 +168,12 @@ protected function configure(): void
167168
null,
168169
InputOption::VALUE_OPTIONAL,
169170
'Default branch for the repository'
171+
)
172+
->addOption(
173+
self::OPTION_LOCK_FILE_ONLY,
174+
'l',
175+
InputOption::VALUE_NONE,
176+
'Use this option to output lock files only'
170177
);
171178
}
172179

@@ -217,12 +224,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
217224

218225
$repository = \strval($input->getArgument(self::ARGUMENT_REPOSITORY_NAME));
219226
$commit = \strval($input->getArgument(self::ARGUMENT_COMMIT_NAME));
227+
$lockFileOnly = (bool) $input->getOption(self::OPTION_LOCK_FILE_ONLY);
220228

221229
$uploadId = null;
222230

223231
try {
224232
$io->writeln('Getting supported dependency file names from Debricked', OutputInterface::VERBOSITY_VERBOSE);
225-
$fileGroups = FileGroupFinder::find($api, $searchDirectory, $recursiveFileSearch, $directoriesToExcludeArray);
233+
$fileGroups = FileGroupFinder::find($api, $searchDirectory, $recursiveFileSearch, $directoriesToExcludeArray, $lockFileOnly);
226234
} catch (TransportExceptionInterface $e) {
227235
$io->error("Failed to get supported dependency file names: {$e->getMessage()}");
228236

src/Command/FindFilesCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class FindFilesCommand extends Command
3030
protected static $defaultName = 'debricked:files:find';
3131

3232
private const OPTION_JSON = 'json';
33+
private const OPTION_LOCK_FILE_ONLY = 'lockfile';
3334

3435
private HttpClientInterface $debrickedClient;
3536

@@ -78,6 +79,12 @@ protected function configure(): void
7879
InputOption::VALUE_REQUIRED,
7980
'Enter a comma separated list of directories to exclude. Such as: --excluded-directories="vendor,node_modules,tests"',
8081
'vendor,node_modules,tests'
82+
)
83+
->addOption(
84+
self::OPTION_LOCK_FILE_ONLY,
85+
'l',
86+
InputOption::VALUE_NONE,
87+
'Use this option to output lock files only'
8188
);
8289
}
8390

@@ -101,6 +108,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
101108
$searchDirectory = preg_replace('#/+#', '/', $searchDirectory); // remove duplicate slashes.
102109

103110
$recursiveFileSearch = (bool) $input->getOption(FindAndUploadFilesCommand::OPTION_RECURSIVE_FILE_SEARCH);
111+
104112
$directoriesToExcludeString = $input->getOption(FindAndUploadFilesCommand::OPTION_DIRECTORIES_TO_EXCLUDE);
105113
$directoriesToExcludeArray = [];
106114
if (empty($directoriesToExcludeString) === false) {
@@ -109,8 +117,10 @@ public function execute(InputInterface $input, OutputInterface $output): int
109117
$io->note('No directories will be ignored');
110118
}
111119

120+
$lockFileOnly = (bool) $input->getOption(self::OPTION_LOCK_FILE_ONLY);
121+
112122
try {
113-
$fileGroups = FileGroupFinder::find($api, $searchDirectory, $recursiveFileSearch, $directoriesToExcludeArray);
123+
$fileGroups = FileGroupFinder::find($api, $searchDirectory, $recursiveFileSearch, $directoriesToExcludeArray, $lockFileOnly);
114124
} catch (TransportExceptionInterface $e) {
115125
$io->error("Failed to get supported dependency file names: {$e->getMessage()}");
116126

src/Model/DependencyFileFormat.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ class DependencyFileFormat
1616
*
1717
* @return DependencyFileFormat[]
1818
*/
19-
public static function make(array $formats): array
19+
public static function make(array $formats, bool $lockFileOnly = false): array
2020
{
2121
$dependencyFormats = [];
2222
foreach ($formats as $format) {
23-
$dependencyFormats[] = new self($format['regex'], $format['documentationUrl'], $format['lockFileRegexes']);
23+
if ($lockFileOnly) {
24+
$dependencyFormats[] = new self('', $format['documentationUrl'], $format['lockFileRegexes']);
25+
} else {
26+
$dependencyFormats[] = new self($format['regex'], $format['documentationUrl'], $format['lockFileRegexes']);
27+
}
2428
}
2529

2630
return $dependencyFormats;

src/Service/FileGroupFinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FileGroupFinder
2222
*
2323
* @throws TransportExceptionInterface|HttpExceptionInterface|DirectoryNotFoundException
2424
*/
25-
public static function find(API $api, string $searchDirectory, bool $recursiveFileSearch, array $excludedDirectories): array
25+
public static function find(API $api, string $searchDirectory, bool $recursiveFileSearch, array $excludedDirectories, bool $lockFileOnly = false): array
2626
{
2727
$finder = self::makeFinder($searchDirectory, $recursiveFileSearch, $excludedDirectories);
2828

@@ -32,7 +32,7 @@ public static function find(API $api, string $searchDirectory, bool $recursiveFi
3232
'/api/1.0/open/files/supported-formats'
3333
);
3434
$dependencyFileFormats = \json_decode($dependencyFileNamesResponse->getContent(), true);
35-
$dependencyFileFormats = DependencyFileFormat::make($dependencyFileFormats);
35+
$dependencyFileFormats = DependencyFileFormat::make($dependencyFileFormats, $lockFileOnly);
3636

3737
$lockFiles = self::findLockFiles($finder, $dependencyFileFormats);
3838

tests/Command/FindFilesCommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public function testExecuteJson(): void
6161
$this->assertIsArray($fileGroup['lockFiles']);
6262
}
6363
[$fileGroup1, $fileGroup2] = [$fileGroups[0], $fileGroups[1]];
64-
$this->assertEquals('composer.json', $fileGroup1['dependencyFile']);
64+
$this->assertEquals('bin/.phpunit/phpunit-9.5-0/composer.json', $fileGroup1['dependencyFile']);
6565
$this->assertCount(1, $fileGroup1['lockFiles']);
6666
$lockFile = $fileGroup1['lockFiles'][0];
67-
$this->assertEquals('composer.lock', $lockFile);
67+
$this->assertEquals('bin/.phpunit/phpunit-9.5-0/composer.lock', $lockFile);
6868

69-
$this->assertEquals('bin/.phpunit/phpunit-9.5-0/composer.json', $fileGroup2['dependencyFile']);
69+
$this->assertEquals('composer.json', $fileGroup2['dependencyFile']);
7070
$this->assertCount(1, $fileGroup2['lockFiles']);
7171
$lockFile = $fileGroup2['lockFiles'][0];
72-
$this->assertEquals('bin/.phpunit/phpunit-9.5-0/composer.lock', $lockFile);
72+
$this->assertEquals('composer.lock', $lockFile);
7373
}
7474

7575
public function testExecuteInvalidCredentials(): void

tests/Service/FileGroupFinderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FileGroupFinderTest extends TestCase
1919
public function setUp(): void
2020
{
2121
$this->apiMock = $this->getMockBuilder(API::class)->disableOriginalConstructor()->getMock();
22-
$responseMock = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
22+
$responseMock = $this->getMockBuilder(ResponseInterface::class)->getMock();
2323

2424
$responseMock->expects($this->atMost(1))->method('getContent')->willReturn(FindAndUploadFilesCommandTest::FORMATS_JSON_STRING);
2525

@@ -71,4 +71,16 @@ public function testFindExcludedFilesAreIgnored(): void
7171
}
7272
}
7373
}
74+
75+
public function testFindOnlyLockFiles(): void
76+
{
77+
$fileGroups = FileGroupFinder::find($this->apiMock, getcwd(), false, [], true);
78+
$this->assertCount(1, $fileGroups);
79+
$fileGroup = $fileGroups[0];
80+
$this->assertNull($fileGroup->getDependencyFile());
81+
$this->assertNotEmpty($fileGroup->getLockFiles());
82+
$this->assertCount(1, $fileGroup->getLockFiles());
83+
$lockFile = $fileGroup->getLockFiles()[0];
84+
$this->assertInstanceOf(\SplFileInfo::class, $lockFile);
85+
}
7486
}

0 commit comments

Comments
 (0)