1
0

gen-coverage-badge.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. if (!isset($argv[1])) {
  8. echo "Clover file is missing" . PHP_EOL;
  9. exit;
  10. }
  11. try {
  12. $cloverFile = $argv[1];
  13. $doc = simplexml_load_file($cloverFile);
  14. $metrics = [];
  15. foreach ($doc->project->metrics->attributes() as $k => $v) {
  16. $metrics[$k] = $v->__toString();
  17. }
  18. $c0 = $metrics['coveredmethods'] / $metrics['methods'];
  19. $c1 = $metrics['coveredelements'] / $metrics['elements'];
  20. $c2 = $metrics['coveredstatements'] / $metrics['statements'];
  21. echo $c0 . PHP_EOL;
  22. echo $c1 . PHP_EOL;
  23. echo $c2 . PHP_EOL;
  24. $percent = (int)($c2 * 100);
  25. $color = 'red';
  26. if ($percent >= 50) {
  27. $color = 'yellow';
  28. }
  29. if ($percent >= 75) {
  30. $color = 'green';
  31. }
  32. $content = file_get_contents("https://img.shields.io/badge/coverage-$percent%-$color.svg");
  33. file_put_contents('coverage.svg', $content);
  34. } catch (Exception $ex) {
  35. echo $ex->getMessage() . PHP_EOL;
  36. $content = file_get_contents("https://img.shields.io/badge/coverage-ERROR-red.svg");
  37. file_put_contents('coverage.svg', $content);
  38. }