Skip to content

Commit

Permalink
Add grouping of output in GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbentley committed Mar 21, 2023
1 parent 15d7599 commit 5a2bed6
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 21 deletions.
14 changes: 7 additions & 7 deletions resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ services:
logger: '@grumphp.logger'
inherit_environment_variables: true

grumphp.io:
alias: grumphp.io.console
GrumPHP\IO\IOFactory:
arguments:
- '@OndraM\CiDetector\CiDetector'

grumphp.io.console:
class: GrumPHP\IO\ConsoleIO
grumphp.io:
factory: ['@GrumPHP\IO\IOFactory', 'create']
arguments:
- '@console.input'
- '@console.output'
public: true
- '@console.input'
- '@console.output'

grumphp.logger:
class: Monolog\Logger
Expand Down
13 changes: 9 additions & 4 deletions src/Console/Command/Git/CommitMsgCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\TestSuiteCollection;
use GrumPHP\IO\ConsoleIO;
use GrumPHP\IO\IOFactory;
use GrumPHP\IO\IOInterface;
use GrumPHP\Locator\ChangedFiles;
use GrumPHP\Locator\StdInFiles;
use GrumPHP\Runner\TaskRunner;
Expand Down Expand Up @@ -60,13 +61,16 @@ class CommitMsgCommand extends Command
*/
private $paths;

private IOFactory $IOFactory;

public function __construct(
TestSuiteCollection $testSuites,
StdInFiles $stdInFilesLocator,
ChangedFiles $changedFilesLocator,
TaskRunner $taskRunner,
Filesystem $filesystem,
Paths $paths
Paths $paths,
IOFactory $IOFactory
) {
parent::__construct();

Expand All @@ -76,6 +80,7 @@ public function __construct(
$this->filesystem = $filesystem;
$this->paths = $paths;
$this->stdInFilesLocator = $stdInFilesLocator;
$this->IOFactory = $IOFactory;
}

public static function getDefaultName(): string
Expand All @@ -93,7 +98,7 @@ protected function configure(): void

public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new ConsoleIO($input, $output);
$io = $this->IOFactory->create($input, $output);
$files = $this->getCommittedFiles($io);

/** @var string $gitUser */
Expand Down Expand Up @@ -124,7 +129,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK;
}

protected function getCommittedFiles(ConsoleIO $io): FilesCollection
protected function getCommittedFiles(IOInterface $io): FilesCollection
{
if ($stdin = $io->readCommandInput(STDIN)) {
return $this->stdInFilesLocator->locate($stdin);
Expand Down
13 changes: 9 additions & 4 deletions src/Console/Command/Git/PreCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\TestSuiteCollection;
use GrumPHP\IO\ConsoleIO;
use GrumPHP\IO\IOFactory;
use GrumPHP\IO\IOInterface;
use GrumPHP\Locator\ChangedFiles;
use GrumPHP\Locator\StdInFiles;
use GrumPHP\Runner\TaskRunner;
Expand Down Expand Up @@ -46,18 +47,22 @@ class PreCommitCommand extends Command
*/
private $taskRunner;

private IOFactory $IOFactory;

public function __construct(
TestSuiteCollection $testSuites,
StdInFiles $stdInFilesLocator,
ChangedFiles $changedFilesLocator,
TaskRunner $taskRunner
TaskRunner $taskRunner,
IOFactory $IOFactory
) {
parent::__construct();

$this->testSuites = $testSuites;
$this->stdInFilesLocator = $stdInFilesLocator;
$this->changedFilesLocator = $changedFilesLocator;
$this->taskRunner = $taskRunner;
$this->IOFactory = $IOFactory;
}

public static function getDefaultName(): string
Expand All @@ -78,7 +83,7 @@ protected function configure(): void

public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new ConsoleIO($input, $output);
$io = $this->IOFactory->create($input, $output);
$files = $this->getCommittedFiles($io);

$context = (
Expand All @@ -95,7 +100,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK;
}

protected function getCommittedFiles(ConsoleIO $io): FilesCollection
protected function getCommittedFiles(IOInterface $io): FilesCollection
{
if ($stdin = $io->readCommandInput(STDIN)) {
return $this->stdInFilesLocator->locate($stdin);
Expand Down
13 changes: 9 additions & 4 deletions src/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\TestSuiteCollection;
use GrumPHP\IO\ConsoleIO;
use GrumPHP\IO\IOFactory;
use GrumPHP\IO\IOInterface;
use GrumPHP\Locator\RegisteredFiles;
use GrumPHP\Locator\StdInFiles;
use GrumPHP\Runner\TaskRunner;
Expand Down Expand Up @@ -44,18 +45,22 @@ class RunCommand extends Command
*/
private $taskRunner;

private IOFactory $IOFactory;

public function __construct(
TestSuiteCollection $testSuites,
StdInFiles $stdInFileLocator,
RegisteredFiles $registeredFilesLocator,
TaskRunner $taskRunner
TaskRunner $taskRunner,
IOFactory $IOFactory
) {
parent::__construct();

$this->testSuites = $testSuites;
$this->stdInFileLocator = $stdInFileLocator;
$this->registeredFilesLocator = $registeredFilesLocator;
$this->taskRunner = $taskRunner;
$this->IOFactory = $IOFactory;
}

public static function getDefaultName(): string
Expand Down Expand Up @@ -83,7 +88,7 @@ protected function configure(): void

public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new ConsoleIO($input, $output);
$io = $this->IOFactory->create($input, $output);

/** @var string $taskNames */
$taskNames = $input->getOption('tasks') ?? '';
Expand All @@ -107,7 +112,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK;
}

private function detectFiles(ConsoleIO $io): FilesCollection
private function detectFiles(IOInterface $io): FilesCollection
{
if ($stdin = $io->readCommandInput(STDIN)) {
return $this->stdInFileLocator->locate($stdin);
Expand Down
9 changes: 9 additions & 0 deletions src/IO/ConsoleIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ static function (string $message) use ($color) : string {
);
}

public function startGroup(string $title): void
{
$this->style()->title($title);
}

public function endGroup(): void
{
}

/**
* Serializing this IO will result in an unwritable resource stream.
* Therefor we serialize the data end build up a new stream instead.
Expand Down
20 changes: 20 additions & 0 deletions src/IO/GitHubActionsIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace GrumPHP\IO;

use Symfony\Component\Console\Output\ConsoleSectionOutput;

class GitHubActionsIO extends ConsoleIO
{
public function startGroup(string $title): void
{
$this->write('::group::' . $title);
parent::startGroup($title);
}

public function endGroup(): void
{
parent::endGroup();
$this->write('::endgroup::');
}
}
27 changes: 27 additions & 0 deletions src/IO/IOFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace GrumPHP\IO;
use OndraM\CiDetector\Ci\GitHubActions;
use OndraM\CiDetector\CiDetector;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class IOFactory
{
public function __construct(private CiDetector $ciDetector)
{
}

public function create(InputInterface $input, OutputInterface $output): IOInterface
{
if ($this->ciDetector->isCiDetected()) {
$platform = $this->ciDetector->detect();

if ($platform instanceof GitHubActions) {
return new GitHubActionsIO($input, $output);
}
}

return new ConsoleIO($input, $output);
}
}
4 changes: 4 additions & 0 deletions src/IO/IOInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public function style(): StyleInterface;
public function section(): ConsoleSectionOutput;

public function colorize(array $messages, string $color): array;

public function startGroup(string $title): void;

public function endGroup(): void;
}
3 changes: 2 additions & 1 deletion src/Runner/Middleware/GroupByPriorityMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public function handle(TaskRunnerContext $context, callable $next): TaskResultCo
->groupByPriority();

foreach ($grouped as $priority => $tasks) {
$this->IO->style()->title('Running tasks with priority '.$priority.'!');
$this->IO->startGroup('Running tasks with priority '.$priority.'!');
$results = new TaskResultCollection(array_merge(
$results->toArray(),
$next($context->withTasks($tasks))->toArray()
));
$this->IO->endGroup();

// Stop on failure:
if ($this->config->stopOnFailure() && $results->isFailed()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Runner/Reporting/RunnerReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ private function reportWarningMessages(array $warningMessages): void
private function reportFailedMessages(array $messages, string $color): void
{
foreach ($messages as $label => $message) {
$this->IO->style()->title($label);
$this->IO->startGroup($label);
$this->IO->writeError($this->IO->colorize([$message], $color));
$this->IO->endGroup();
}
}

Expand Down

0 comments on commit 5a2bed6

Please sign in to comment.