diff --git a/dev/tools/apstats.php b/dev/tools/apstats.php index 3a70fc142c4..837ea758c2c 100755 --- a/dev/tools/apstats.php +++ b/dev/tools/apstats.php @@ -40,11 +40,15 @@ define('VERSION', "1.0"); $phpstanlevel = 3; +// Include Dolibarr environment +require_once $path.'../../htdocs/master.inc.php'; +require_once $path.'../../htdocs/core/lib/files.lib.php'; +require_once $path.'../../htdocs/core/lib/geturl.lib.php'; print '***** '.constant('PRODUCT').' - '.constant('VERSION').' *****'."\n"; if (empty($argv[1])) { print 'You must run this tool being into the root of the project.'."\n"; - print 'Usage: '.constant('PRODUCT').'.php pathto/outputfile.html [--dir-scc=pathtoscc] [--dir-phpstan=pathtophpstan]'."\n"; + print 'Usage: '.constant('PRODUCT').'.php pathto/outputfile.html [--dir-scc=pathtoscc|disabled] [--dir-phpstan=pathtophpstan|disabled]'."\n"; print 'Example: '.constant('PRODUCT').'.php documents/apstats/index.html --dir-scc=/snap/bin --dir-phpstan=~/git/phpstan/htdocs/includes/bin'; exit(0); } @@ -93,27 +97,30 @@ $urlgit = 'https://github.com/Dolibarr/dolibarr/blob/develop/'; // Count lines of code of application -$commandcheck = ($dirscc ? $dirscc.'/' : '').'scc . --exclude-dir=htdocs/includes,htdocs/custom,htdocs/theme/common/fontawesome-5,htdocs/theme/common/octicons'; -print 'Execute SCC to count lines of code in project: '.$commandcheck."\n"; -$output_arrproj = array(); -$resexecproj = 0; -exec($commandcheck, $output_arrproj, $resexecproj); +if ($dirscc != 'disabled') { + $commandcheck = ($dirscc ? $dirscc.'/' : '').'scc . --exclude-dir=htdocs/includes,htdocs/custom,htdocs/theme/common/fontawesome-5,htdocs/theme/common/octicons'; + print 'Execute SCC to count lines of code in project: '.$commandcheck."\n"; + $output_arrproj = array(); + $resexecproj = 0; + exec($commandcheck, $output_arrproj, $resexecproj); -// Count lines of code of dependencies -$commandcheck = ($dirscc ? $dirscc.'/' : '').'scc htdocs/includes htdocs/theme/common/fontawesome-5 htdocs/theme/common/octicons'; -print 'Execute SCC to count lines of code in dependencies: '.$commandcheck."\n"; -$output_arrdep = array(); -$resexecdep = 0; -exec($commandcheck, $output_arrdep, $resexecdep); - + // Count lines of code of dependencies + $commandcheck = ($dirscc ? $dirscc.'/' : '').'scc htdocs/includes htdocs/theme/common/fontawesome-5 htdocs/theme/common/octicons'; + print 'Execute SCC to count lines of code in dependencies: '.$commandcheck."\n"; + $output_arrdep = array(); + $resexecdep = 0; + exec($commandcheck, $output_arrdep, $resexecdep); +} // Get technical debt -$commandcheck = ($dirphpstan ? $dirphpstan.'/' : '').'phpstan --level='.$phpstanlevel.' -v analyze -a build/phpstan/bootstrap.php --memory-limit 5G --error-format=github'; -print 'Execute PHPStan to get the technical debt: '.$commandcheck."\n"; -$output_arrtd = array(); -$resexectd = 0; -exec($commandcheck, $output_arrtd, $resexectd); +if ($dirphpstan != 'disabled') { + $commandcheck = ($dirphpstan ? $dirphpstan.'/' : '').'phpstan --level='.$phpstanlevel.' -v analyze -a build/phpstan/bootstrap.php --memory-limit 5G --error-format=github'; + print 'Execute PHPStan to get the technical debt: '.$commandcheck."\n"; + $output_arrtd = array(); + $resexectd = 0; + exec($commandcheck, $output_arrtd, $resexectd); +} // Count lines of code of dependencies @@ -189,12 +196,78 @@ foreach (array('proj', 'dep') as $source) { // Search the max $arrayofmax = array('Lines'=>0); foreach (array('proj', 'dep') as $source) { - foreach ($arrayoflineofcode[$source] as $val) { - $arrayofmax['Lines'] = max($arrayofmax['Lines'], $val['Lines']); + if (!empty($arrayoflineofcode[$source])) { + foreach ($arrayoflineofcode[$source] as $val) { + $arrayofmax['Lines'] = max($arrayofmax['Lines'], $val['Lines']); + } } } + +// Retrieve the .git information +$nbofdays = 90; +$delay = (3600 * 24 * $nbofdays); +$urlgit = 'https://api.github.com/search/issues?q=is:pr+repo:Dolibarr/dolibarr+created:>'.dol_print_date(dol_now() - $delay, "%Y-%m"); + + +$arrayofalerts = array(); +$arrayofalerts1 = $arrayofalerts2 = $arrayofalerts3 = array(); + +// Count lines of code of application +$newurl = $urlgit.'+CVE'; +$result = getURLContent($newurl); +print 'Execute GET on github for '.$newurl."\n"; +if ($result && $result['http_code'] == 200) { + $arrayofalerts1 = json_decode($result['content']); + + foreach ($arrayofalerts1->items as $val) { + $tmpval = cleanVal($val); + if (preg_match('/CVE/i', $tmpval['title'])) { + $arrayofalerts[$tmpval['number']] = $tmpval; + } + } +} else { + print 'Error: failed to get github response'; + exit(-1); +} + +$newurl = $urlgit.'+yogosha'; +$result = getURLContent($newurl); +print 'Execute GET on github for '.$newurl."\n"; +if ($result && $result['http_code'] == 200) { + $arrayofalerts2 = json_decode($result['content']); + + foreach ($arrayofalerts2->items as $val) { + $tmpval = cleanVal($val); + if (preg_match('/yogosha:/i', $tmpval['title'])) { + $arrayofalerts[$tmpval['number']] = $tmpval; + } + } +} else { + print 'Error: failed to get github response'; + exit(-1); +} + +$newurl = $urlgit.'+Sec:'; +$result = getURLContent($newurl); +print 'Execute GET on github for '.$newurl."\n"; +if ($result && $result['http_code'] == 200) { + $arrayofalerts3 = json_decode($result['content']); + foreach ($arrayofalerts3->items as $val) { + $tmpval = cleanVal($val); + if (preg_match('/Sec:/i', $tmpval['title'])) { + $arrayofalerts[$tmpval['number']] = $tmpval; + } + } +} else { + print 'Error: failed to get github response'; + exit(-1); +} + +$timeend = time(); + + $timeend = time(); @@ -487,27 +560,50 @@ $html .= ''."\n"; $tmp = ''; $nblines = 0; -foreach ($output_arrtd as $line) { - $reg = array(); - //print $line."\n"; - preg_match('/^::error file=(.*),line=(\d+),col=(\d+)::(.*)$/', $line, $reg); - if (!empty($reg[1])) { - if ($nblines < 20) { - $tmp .= '
| ID | Title | Date |
| #'.$alert['number'].' | '; + $html .= ''.$alert['title'].' | '; + $html .= $alert['created_at'].' | '; + $html .= '