diff --git a/bin/pull-request-coverage.sh b/bin/pull-request-coverage.sh index e0e69a72..35824690 100644 --- a/bin/pull-request-coverage.sh +++ b/bin/pull-request-coverage.sh @@ -1,9 +1,14 @@ #!/bin/bash +echo "Getting pull request head branch..." +export PHPUNIT_HEAD_BRANCH=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/PluginAndPartners/cart-magento2/pulls/${PR_NUMBER} \ +| jq ".head.ref" \ +| xargs) + echo "Getting pull request files..." export PHPUNIT_FILES=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/PluginAndPartners/cart-magento2/pulls/${PR_NUMBER}/files \ | jq ".[].filename" \ | grep -E 'php"$' \ | xargs) -php magento2/app/code/MercadoPago/Test/pull-request-coverage-checker.php clover.xml 40 $PHPUNIT_FILES +php magento2/app/code/MercadoPago/Test/pull-request-coverage-checker.php clover.xml 40 $PHPUNIT_HEAD_BRANCH $PHPUNIT_FILES diff --git a/src/MercadoPago/Test/pull-request-coverage-checker.php b/src/MercadoPago/Test/pull-request-coverage-checker.php index fb15a51b..6979a9a0 100644 --- a/src/MercadoPago/Test/pull-request-coverage-checker.php +++ b/src/MercadoPago/Test/pull-request-coverage-checker.php @@ -1,56 +1,109 @@ xpath('//class'); + $totalElements = 0; + $checkedElements = 0; -for ($i = 3; $i < count($argv); $i++) { - $filename = str_replace('src/', '', $argv[$i]); - $filename = str_replace('/', '\\', $filename); - $filename = str_replace('.php', '', $filename); + foreach ($classes as $class) { + if (in_array($class['name'], $pullRequestFiles)) { + $totalElements += (int) $class->metrics['elements']; + $checkedElements += (int) $class->metrics['coveredelements']; + } + } - $pullRequestFiles[] = $filename; + return [ + 'totalElements' => $totalElements, + 'checkedElements' => $checkedElements, + ]; } -if (!file_exists($cloverFile)) { - throw new InvalidArgumentException('Invalid clover file provided'); -} +function parse_pull_request_files($argv) { + $pullRequestFiles = []; + + for ($i = 4; $i < count($argv); $i++) { + $filename = str_replace('src/', '', $argv[$i]); + $filename = str_replace('/', '\\', $filename); + $filename = str_replace('.php', '', $filename); + + if (is_testable($filename)) { + $pullRequestFiles[] = $filename; + print_r($filename . ' is a testable file' . PHP_EOL); + } + } -if (!$percentage) { - throw new InvalidArgumentException('An integer checked percentage must be given as second parameter'); + return $pullRequestFiles; } -if (count($pullRequestFiles) == 0) { - print_r('Pull Request does not contain any php file to check code coverage'); - return; +function is_testable($filename) { + // Add all untestable php files + $whitelist = [ + 'MercadoPago\Test\coverage-checker', + 'MercadoPago\Test\pull-request-coverage-checker', + ]; + + return in_array($filename, $whitelist) ? false : true; } -$xml = new SimpleXMLElement(file_get_contents($cloverFile)); -$classes = $xml->xpath('//class'); -$totalElements = 0; -$checkedElements = 0; +function is_hotfix_branch($branchName) { + return strpos($branchName, 'hotfix'); +} -foreach ($classes as $class) { - if (in_array($class['name'], $pullRequestFiles)) { - $totalElements += (int) $class->metrics['elements']; - $checkedElements += (int) $class->metrics['coveredelements']; +function validate_clover_file($cloverFile) { + if (!file_exists($cloverFile)) { + throw new InvalidArgumentException('Invalid clover file provided'); } } -if ($totalElements == 0 || $checkedElements == 0) { - print_r('Pull Request does not contain tested php files to check code coverage'); - return; +function validate_percentage_param($percentage) { + if (!$percentage) { + throw new InvalidArgumentException('An integer checked percentage must be given as second parameter'); + } } -$coverage = ($checkedElements / $totalElements) * 100; +function validate_pull_request_coverage($totalElements, $checkedElements, $percentage) { + $coverage = ($checkedElements / $totalElements) * 100; + + if ($coverage >= $percentage) { + print_r('Code coverage is ' . $coverage); + print_r(' -> Pull Request OK'); + return; + } -if ($coverage >= $percentage) { - print_r('Code coverage is ' . $coverage); - print_r(' -> Pull Request OK'); - return; + print_r('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); + print_r(' -> Pull Request Rejected'); + + throw new Exception('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); } -print_r('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); -print_r(' -> Pull Request Rejected'); +function execute($argv) { + $branchName = $argv[3]; + $cloverFile = $argv[1]; + $percentage = min(100, max(0, (int) $argv[2])); + $pullRequestFiles = parse_pull_request_files($argv); + + if (!is_hotfix_branch($branchName)) { + validate_clover_file($cloverFile); + validate_percentage_param($percentage); + + $elements = get_elements($cloverFile, $pullRequestFiles); + $totalElements = $elements['totalElements']; + $checkedElements = $elements['checkedElements']; + + if ($totalElements == 0 || $checkedElements == 0) { + if (count($pullRequestFiles) === 0) { + print_r('Pull request does not contain testable files'); + return; + } + + throw new Exception('Pull Request does not contain tested php files to check code coverage'); + } + + validate_pull_request_coverage($totalElements, $checkedElements, $percentage); + + return; + } +} -throw new Exception('Code coverage is ' . round($coverage, 2) . '%, which is below the accepted ' . $percentage . '%'); +execute($argv);