Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/Actions/ElaborateSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Actions;

use App\Factories\ConfigurationResolverFactory;
use App\Output\AgentReporter;
use Illuminate\Console\Command;
use PhpCsFixer\Console\Report\FixReport;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -45,7 +47,11 @@ public function execute($totalFiles, $changes)
$this->output->isDecorated()
);

if ($format = $this->input->getOption('format')) {
$format = $this->input->getOption('format');

if (ConfigurationResolverFactory::runningInAgent()) {
$this->displayUsingFormatter($summary, 'agent');
} elseif ($format) {
$this->displayUsingFormatter($summary, $format);
} else {
$this->summaryOutput->handle($summary, $totalFiles);
Expand Down Expand Up @@ -74,6 +80,7 @@ public function execute($totalFiles, $changes)
protected function displayUsingFormatter($summary, ?string $format = null, ?string $outputPath = null)
{
$reporter = match ($format) {
'agent' => new AgentReporter,
'checkstyle' => new FixReport\CheckstyleReporter,
'gitlab' => new FixReport\GitlabReporter,
'json' => new FixReport\JsonReporter,
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/FixCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function execute()
return [$exception->getCode(), []];
}

if (is_null($this->input->getOption('format'))) {
if (is_null($this->input->getOption('format')) && ! ConfigurationResolverFactory::runningInAgent()) {
$this->progress->subscribe();
}

Expand Down
8 changes: 8 additions & 0 deletions app/Factories/ConfigurationResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public static function fromIO($input, $output)

return [$resolver, $totalFiles];
}

/**
* Determine if Pint is being run by an AI agent.
*/
public static function runningInAgent(): bool
{
return (bool) getenv('OPENCODE') || (bool) getenv('CLAUDECODE');
}
}
49 changes: 49 additions & 0 deletions app/Output/AgentReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Output;

use App\Project;
use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
use PhpCsFixer\Console\Report\FixReport\ReportSummary;

final class AgentReporter implements ReporterInterface
{
/**
* Get the format's name.
*/
public function getFormat(): string
{
return 'agent';
}

/**
* Process changed files array and returns generated report.
*/
public function generate(ReportSummary $reportSummary): string
{
$changed = $reportSummary->getChanged();

$projectPath = Project::path().DIRECTORY_SEPARATOR;

$status = match (true) {
$changed === [] => 'pass',
$reportSummary->isDryRun() => 'fail',
default => 'fixed',
};

$output = ['status' => $status];

if ($changed !== []) {
$output['files'] = [];

foreach ($changed as $path => $change) {
$output['files'][] = [
'path' => str_replace($projectPath, '', $path),
'fixers' => $change['appliedFixers'],
];
}
}

return json_encode($output, JSON_THROW_ON_ERROR);
}
}
71 changes: 71 additions & 0 deletions tests/Feature/FormatTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

afterEach(function () {
putenv('OPENCODE');
putenv('CLAUDECODE');
});

it('outputs checkstyle format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
Expand Down Expand Up @@ -79,3 +84,69 @@
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});

it('outputs agent format with fail status on test mode', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'agent',
]);

$json = json_decode($output, true);

expect($statusCode)->toBe(1)
->and($output)->toBeJson()
->and($json['status'])->toBe('fail')
->and($json)->toHaveKey('files')
->and($json['files'][0])->toHaveKeys(['path', 'fixers'])
->and($json['files'][0]['fixers'])->toBeArray()
->and($json)->not->toHaveKey('about')
->and($json)->not->toHaveKey('time')
->and($json)->not->toHaveKey('memory');
});

it('outputs agent format with pass status when no issues', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/without-issues-laravel'),
'--format' => 'agent',
]);

$json = json_decode($output, true);

expect($statusCode)->toBe(0)
->and($output)->toBeJson()
->and($json['status'])->toBe('pass')
->and($json)->not->toHaveKey('files');
});

it('auto-detects agent format via OPENCODE env var', function () {
putenv('OPENCODE=1');

[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
]);

$json = json_decode($output, true);

expect($statusCode)->toBe(1)
->and($output)->toBeJson()
->and($json)->toHaveKey('files')
->and($json['files'][0])->toHaveKeys(['path', 'fixers']);
});

it('auto-detects agent format via CLAUDECODE env var', function () {
putenv('CLAUDECODE=1');

[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
]);

$json = json_decode($output, true);

expect($statusCode)->toBe(1)
->and($output)->toBeJson()
->and($json)->toHaveKey('files')
->and($json['files'][0])->toHaveKeys(['path', 'fixers']);
});