From 3b88f40c855dba1c5eb8b2c043daa0484e24f425 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sun, 28 Aug 2022 13:50:10 +0200 Subject: [PATCH] Only run if .gitlab-ci.yml has been modified --- src/GitlabApiClient.php | 4 ---- src/GitlabLintTask.php | 9 +++++++++ tests/GitlabLintTaskTest.php | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/GitlabApiClient.php b/src/GitlabApiClient.php index e748cfb..a3c6c4d 100644 --- a/src/GitlabApiClient.php +++ b/src/GitlabApiClient.php @@ -57,10 +57,6 @@ public function lint(): array $yamlEncoder = new YamlEncoder(); $jsonEncoder = new JsonEncoder(); - if (! file_exists($this->configFile)) { - throw GitlabLinterException::fileNotFound($this->configFile); - } - $fileContents = (string) file_get_contents($this->configFile); $jsonData = $jsonEncoder->encode( diff --git a/src/GitlabLintTask.php b/src/GitlabLintTask.php index 87b110b..4e4eeb2 100644 --- a/src/GitlabLintTask.php +++ b/src/GitlabLintTask.php @@ -49,6 +49,15 @@ public function run(ContextInterface $context): TaskResultInterface { $config = $this->getConfig()->getOptions(); + if (! file_exists($config['gitlab_file'])) { + throw GitlabLinterException::fileNotFound($config['gitlab_file']); + } + + $files = $context->getFiles()->name(basename($config['gitlab_file'])); + if (count($files) === 0) { + return TaskResult::createSkipped($this, $context); + } + $apiClient = new GitlabApiClient($config); /** @var array{valid: bool, errors: array} $response */ diff --git a/tests/GitlabLintTaskTest.php b/tests/GitlabLintTaskTest.php index 54609f1..a2a85b6 100644 --- a/tests/GitlabLintTaskTest.php +++ b/tests/GitlabLintTaskTest.php @@ -22,6 +22,7 @@ use Headsnet\GrumPHP\GitlabLint\GitlabLinterException; use Headsnet\GrumPHP\GitlabLint\GitlabLintTask; use PHPUnit\Framework\TestCase; +use SplFileInfo; class GitlabLintTaskTest extends TestCase { @@ -96,12 +97,20 @@ public function gitlabFileDataProvider(): array */ private function buildTask(string $file, string $apiToken): array { + $gitlabFile = sprintf('%s/%s', __DIR__, $file); $processBuilder = $this->createMock(ProcessBuilder::class); $rawProcessFormatter = $this->createMock(RawProcessFormatter::class); - $context = new class() implements ContextInterface { + $context = new class($gitlabFile) implements ContextInterface { + private string $gitlabFile; + + public function __construct(string $gitlabFile) + { + $this->gitlabFile = $gitlabFile; + } + public function getFiles(): FilesCollection { - return new FilesCollection(); + return new FilesCollection([new SplFileInfo($this->gitlabFile)]); } }; @@ -115,7 +124,7 @@ public function getFiles(): FilesCollection 'gitlab_lint', [ 'api_token' => $apiToken, - 'gitlab_file' => sprintf('%s/%s', __DIR__, $file), + 'gitlab_file' => $gitlabFile, 'gitlab_url' => 'gitlab.com', ], new Metadata([])