Use sensiolabs/ansi-to-html to parse the build logs.

Added an AnsiConverter helper.

Use the AnsiConverter in the email and page templates that display the build log.

Use a dedicated stylesheet for the ANSI converter.

It can be customized.
It can be inlined in the notifications.

Do not use ProphecyTestCase when not needed.
This commit is contained in:
Adirelle 2015-02-26 15:57:45 +01:00 committed by Tobias van Beek
parent 039324c056
commit d804438a87
9 changed files with 162 additions and 9 deletions

View file

@ -13,6 +13,7 @@ use b8;
use b8\Exception\HttpException\NotFoundException;
use b8\Http\Response\JsonResponse;
use PHPCI\BuildFactory;
use PHPCI\Helper\AnsiConverter;
use PHPCI\Helper\Lang;
use PHPCI\Model\Build;
use PHPCI\Model\Project;
@ -198,11 +199,7 @@ class BuildController extends \PHPCI\Controller
*/
protected function cleanLog($log)
{
$log = str_replace('[0;32m', '<span style="color: green">', $log);
$log = str_replace('[0;31m', '<span style="color: red">', $log);
$log = str_replace('[0m', '</span>', $log);
return $log;
return AnsiConverter::convert($log);
}
/**

View file

@ -0,0 +1,55 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Helper;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
/**
* Converts ANSI output to HTML.
*
* @package PHPCI\Helper
*/
final class AnsiConverter
{
static private $converter = null;
/**
* Initialize the singletion.
*
* @return AnsiToHtmlConverter
*/
private static function getInstance()
{
if (self::$converter === null) {
self::$converter = new AnsiToHtmlConverter(null, false);
}
return self::$converter;
}
/**
* Convert a text containing ANSI colr sequences into HTML code.
*
* @param string $text The text to convert
*
* @return string The HTML code.
*/
public static function convert($text)
{
return self::getInstance()->convert($text);
}
/**
* Do not instanciate this class.
*/
private function __construct()
{
}
}

View file

@ -1,3 +1,5 @@
<?php use PHPCI\Helper\AnsiConverter; ?>
<style><?php include(__DIR__ . '/../../../public/assets/css/ansi-colors.css'); ?></style>
<div style="background: #090; padding: 25px;">
<div style="background: #fff; padding: 15px; border-radius: 5px">
<div style="font-family: arial, verdana, sans-serif; font-size: 25px; margin-bottom: 15px">
@ -8,7 +10,7 @@
<p>Your commit <strong><?php print $build->getCommitId(); ?></strong> genrate a successfull build in project <strong><?php print $project->getTitle(); ?></strong>.</p>
<p style="margin: 10px; background: #fafafa"><?php print $build->getCommitMessage(); ?></p>
<pre><?php print $build->getLog(); ?></pre>
<pre class="ansi_color_bg_black ansi_color_fg_white" style="padding: 4px"><?php print AnsiConverter::convert($build->getLog()); ?></pre>
<p>You can review <a href="<?php print $build->getCommitLink(); ?>">your commit</a> and the <a href="<?php print PHPCI_URL . 'build/view/' . $build->getId(); ?>">build log</a>.</p>
</div>
</div>

View file

@ -13,6 +13,7 @@
<link href="<?php print PHPCI_URL; ?>assets/css/datepicker/datepicker3.css" rel="stylesheet" type="text/css" />
<link href="<?php print PHPCI_URL; ?>assets/css/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
<link href="<?php print PHPCI_URL; ?>assets/css/ansi-colors.css" rel="stylesheet" type="text/css" />
<!-- Theme style -->
<link href="<?php print PHPCI_URL; ?>assets/css/AdminLTE.css" rel="stylesheet" type="text/css" />

View file

@ -0,0 +1,20 @@
<?php
namespace PHPCI\Helper\Tests;
use PHPCI\Helper\AnsiConverter;
use PHPUnit_Framework_TestCase;
class AnsiConverterTest extends PHPUnit_Framework_TestCase
{
public function testConvert_convertToHtml()
{
$input = "\e[31mThis is red !\e[0m";
$expectedOutput = '<span class="ansi_color_bg_black ansi_color_fg_red">This is red !</span>';
$actualOutput = AnsiConverter::convert($input);
$this->assertEquals($expectedOutput, $actualOutput);
}
}

View file

@ -40,7 +40,8 @@
"psr/log": "~1.0",
"monolog/monolog": "~1.6",
"pimple/pimple": "~1.1",
"robmorgan/phinx": "~0.4"
"robmorgan/phinx": "~0.4",
"sensiolabs/ansi-to-html": "~1.1"
},
"require-dev": {

47
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "4ae188c6be1c1388de6271a3b0e0475d",
"hash": "5fc23800ea77b50b496d34f7aa5cf6b3",
"packages": [
{
"name": "block8/b8framework",
@ -315,6 +315,50 @@
],
"time": "2015-02-23 16:38:12"
},
{
"name": "sensiolabs/ansi-to-html",
"version": "v1.1.0",
"source": {
"type": "git",
"url": "https://github.com/sensiolabs/ansi-to-html.git",
"reference": "92d2ef7ffba5418be060d8ba8adaf7223d741f93"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sensiolabs/ansi-to-html/zipball/92d2ef7ffba5418be060d8ba8adaf7223d741f93",
"reference": "92d2ef7ffba5418be060d8ba8adaf7223d741f93",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"suggest": {
"twig/twig": "Provides nice templating features"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"autoload": {
"psr-0": {
"SensioLabs\\AnsiConverter": "."
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "A library to convert a text with ANSI codes to HTML",
"time": "2014-08-01 14:02:39"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v5.3.1",
@ -2030,6 +2074,7 @@
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=5.3.8",
"ext-pdo": "*",

View file

@ -0,0 +1,32 @@
.ansi_color_fg_black { color: black }
.ansi_color_bg_black { background-color: black }
.ansi_color_fg_red { color: darkred }
.ansi_color_bg_red { background-color: darkred }
.ansi_color_fg_green { color: green }
.ansi_color_bg_green { background-color: green }
.ansi_color_fg_yellow { color: yellow }
.ansi_color_bg_yellow { background-color: yellow }
.ansi_color_fg_blue { color: blue }
.ansi_color_bg_blue { background-color: blue }
.ansi_color_fg_magenta { color: darkmagenta }
.ansi_color_bg_magenta { background-color: darkmagenta }
.ansi_color_fg_cyan { color: cyan }
.ansi_color_bg_cyan { background-color: cyan }
.ansi_color_fg_white { color: white }
.ansi_color_bg_white { background-color: white }
.ansi_color_fg_brblack { color: black }
.ansi_color_bg_brblack { background-color: black }
.ansi_color_fg_brred { color: red }
.ansi_color_bg_brred { background-color: red }
.ansi_color_fg_brgreen { color: lightgreen }
.ansi_color_bg_brgreen { background-color: lightgreen }
.ansi_color_fg_bryellow { color: lightyellow }
.ansi_color_bg_bryellow { background-color: lightyellow }
.ansi_color_fg_brblue { color: lightblue }
.ansi_color_bg_brblue { background-color: lightblue }
.ansi_color_fg_brmagenta { color: magenta }
.ansi_color_bg_brmagenta { background-color: magenta }
.ansi_color_fg_brcyan { color: lightcyan }
.ansi_color_bg_brcyan { background-color: lightcyan }
.ansi_color_fg_brwhite { color: white }
.ansi_color_bg_brwhite { background-color: white }

View file

@ -8,7 +8,7 @@ var logPlugin = ActiveBuild.UiPlugin.extend({
},
render: function() {
var container = $('<pre></pre>');
var container = $('<pre class="ansi_color_bg_black ansi_color_fg_white"></pre>');
container.css({height: '300px', 'overflow-y': 'auto'});
container.html(ActiveBuild.buildData.log);