Legoktm has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/386565 )

Change subject: Show code coverage percent on index page
......................................................................

Show code coverage percent on index page

For doc.wikimedia.org/cover/, show the normal listing of
sub-directories, but also show a progress bar with that repository's
code coverage percentage if a clover.xml file from PHPUnit is present.

Bug: T146970
Change-Id: I186f19ed7a7be964f3351979856b083a486fd733
---
M org/wikimedia/doc/cover/index.php
M shared/Page.php
2 files changed, 139 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/integration/docroot 
refs/changes/65/386565/1

diff --git a/org/wikimedia/doc/cover/index.php 
b/org/wikimedia/doc/cover/index.php
index 5dfe310..889fdce 100644
--- a/org/wikimedia/doc/cover/index.php
+++ b/org/wikimedia/doc/cover/index.php
@@ -1,7 +1,140 @@
 <?php
+/**
+ * Copyright (C) 2017 Kunal Mehta <lego...@member.fsf.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
 require_once __DIR__ . '/../../../../shared/DocPage.php';
 
-$p = DocPage::newDirIndex( 'Coverage' );
+/**
+ * Show a dashboard of code coverage results on the main index page
+ */
+class CoveragePage extends DocPage {
+
+       /**
+        * Defaults from phpunit/src/Util/Configuration.php
+        */
+       const COVERAGE_LOW = 50;
+       const COVERAGE_HIGH = 90;
+
+       /**
+        * Lists directory similar to dir index, but
+        * includes a progress bar
+        */
+       public function handleCoverageIndex() {
+               // Get list of directories with clover.xml
+               $results = glob( __DIR__ . '/*/clover.xml' );
+               $this->embedCSS( file_get_contents( __DIR__ . '/cover.css' ) );
+               $this->addHtmlContent( '<ul class="nav nav-pills nav-stacked">' 
);
+               $html = '';
+               foreach ( $results as $clover ) {
+                       $info = $this->parseClover( $clover );
+                       $dirName = htmlspecialchars( basename( dirname( $clover 
) ) );
+                       $safeName = htmlspecialchars( $info['name'] );
+                       $percent = sprintf( '%.2f', $info['percent'] );
+                       $color = $this->getLevelColor( $info['percent'] );
+                       $html .= <<<HTML
+<li>
+       <a href="./$dirName/">
+               <div class="progress-name">$safeName</div>
+               <div class="progress">
+                       <div class="progress-bar progress-bar-$color" 
role="progressbar" aria-valuenow="$percent" 
+                               aria-valuemin="0" aria-valuemax="100" 
style="width: $percent%">
+                               $percent%
+                       </div>
+               </div>
+       </a>
+</li>
+HTML;
+               }
+               $this->addHtmlContent( "$html</ul>" );
+       }
+
+       /**
+        * Get data out of the clover.xml file
+        *
+        * @param string $fname
+        * @return array|bool false on failure
+        */
+       protected function parseClover( $fname ) {
+               $contents = file_get_contents( $fname );
+               if ( !$contents ) {
+                       // Race condition?
+                       return false;
+               }
+
+               $xml = new SimpleXMLElement( $contents );
+               $metrics = $xml->project->metrics;
+               $percent = (
+                       (int)$metrics['coveredmethods'] +
+                       (int)$metrics['coveredconditionals'] +
+                       (int)$metrics['coveredstatements'] +
+                       (int)$metrics['coveredelements']
+               ) / (
+                       (int)$metrics['methods'] +
+                       (int)$metrics['conditionals'] +
+                       (int)$metrics['statements'] +
+                       (int)$metrics['elements']
+               );
+               return [
+                       'name' => (string)$xml->project->package['name'],
+                       'percent' => $percent * 100,
+               ];
+       }
+
+       /**
+        * Get the CSS class for the progress bar,
+        * based on code in PHP_CodeCoverage
+        *
+        * @param float $percent
+        * @return string
+        */
+       protected function getLevelColor( $percent ) {
+               if ( $percent <= self::COVERAGE_LOW ) {
+                       return 'danger';
+               } elseif ( $percent >= self::COVERAGE_HIGH ) {
+                       return 'success';
+               } else {
+                       // In the middle
+                       return 'warning';
+               }
+       }
+
+       /**
+        * Exclude directories that already have been listed
+        *
+        * @return string[]
+        */
+       protected function getDirIndexDirectories () {
+               $dirs = parent::getDirIndexDirectories();
+               $noClover = [];
+               foreach ( $dirs as $dir ) {
+                       if ( !file_exists( "$dir/clover.xml" ) ) {
+                               $noClover[] = $dir;
+                       }
+               }
+
+               return $noClover;
+       }
+}
+
+/** @var CoveragePage $p */
+$p = CoveragePage::newDirIndex( 'Coverage' );
 $p->setRootDir( dirname( __DIR__ ) );
+$p->handleCoverageIndex();
 $p->handleDirIndex();
 $p->flush();
diff --git a/shared/Page.php b/shared/Page.php
index a1bc6e1..96aed10 100644
--- a/shared/Page.php
+++ b/shared/Page.php
@@ -370,6 +370,10 @@
                return $p;
        }
 
+       protected function getDirIndexDirectories() {
+               return glob( "{$this->dir}/*", GLOB_ONLYDIR );
+       }
+
        public function handleDirIndex() {
                if ( $this->flags & self::INDEX_PREFIX ) {
                        if ( $this->flags & self::INDEX_PARENT_PREFIX && 
strpos( $this->getRootPath(), '/' ) !== false ) {
@@ -379,7 +383,7 @@
                        }
                }
 
-               $subDirPaths = glob( "{$this->dir}/*", GLOB_ONLYDIR );
+               $subDirPaths = $this->getDirIndexDirectories();
                if ( $this->flags & self::INDEX_ALLOW_SKIP ) {
                        if ( count( $subDirPaths ) === 1 ) {
                                header( 'Location: ./' . basename( 
$subDirPaths[0] ) . '/' );

-- 
To view, visit https://gerrit.wikimedia.org/r/386565
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I186f19ed7a7be964f3351979856b083a486fd733
Gerrit-PatchSet: 1
Gerrit-Project: integration/docroot
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to