signed-off-checker.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * Script to verify that all commits have been signed-off, if a commit doesn't end
  25. * with a signed-off message the script is failing.
  26. */
  27. $baseDir = __DIR__ . '/../';
  28. $pullRequestNumber = getenv('DRONE_PULL_REQUEST');
  29. $repoOwner = getenv('DRONE_REPO_OWNER');
  30. $repoName = getenv('DRONE_REPO_NAME');
  31. $droneEvent = getenv('DRONE_BUILD_EVENT');
  32. if(is_string($droneEvent) && $droneEvent === 'push') {
  33. echo("Push event - no signed-off check required.\n");
  34. exit(0);
  35. }
  36. if(!is_string($pullRequestNumber) || $pullRequestNumber === '') {
  37. echo("The environment variable DRONE_PULL_REQUEST has no proper value.\n");
  38. exit(1);
  39. }
  40. if(!is_string($repoOwner) || $repoOwner === '') {
  41. echo("The environment variable DRONE_REPO_OWNER has no proper value.\n");
  42. exit(1);
  43. }
  44. if(!is_string($repoName) || $repoName === '') {
  45. echo("The environment variable DRONE_REPO_NAME has no proper value.\n");
  46. exit(1);
  47. }
  48. $ch = curl_init();
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  50. curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/'.$repoOwner.'/'.$repoName.'/pulls/'.$pullRequestNumber.'/commits');
  51. curl_setopt($ch, CURLOPT_USERAGENT, 'CI for Nextcloud (https://github.com/nextcloud/server)');
  52. $response = curl_exec($ch);
  53. curl_close($ch);
  54. $decodedResponse = json_decode($response, true);
  55. if(!is_array($decodedResponse) || count($decodedResponse) === 0) {
  56. echo("Could not decode JSON response from GitHub API.\n");
  57. exit(1);
  58. }
  59. // Get all commits SHAs
  60. $commits = [];
  61. foreach($decodedResponse as $commit) {
  62. if(!isset($commit['sha'])) {
  63. echo("No SHA specified in $commit\n");
  64. exit(1);
  65. }
  66. if(!isset($commit['commit']['message'])) {
  67. echo("No commit message specified in $commit\n");
  68. exit(1);
  69. }
  70. $commits[$commit['sha']] = $commit['commit']['message'];
  71. }
  72. if(count($commits) < 1) {
  73. echo("Could not read commits.\n");
  74. exit(1);
  75. }
  76. $notSignedCommits = [];
  77. foreach($commits as $commit => $message) {
  78. if($commit === '') {
  79. continue;
  80. }
  81. $signOffMessage = false;
  82. $commitMessageLines = explode("\n", $message);
  83. foreach($commitMessageLines as $line) {
  84. if(preg_match('/^Signed-off-by: .* <.*@.*>$/', $line)) {
  85. echo "$commit is signed-off with \"$line\"\n";
  86. $signOffMessage = true;
  87. continue;
  88. }
  89. }
  90. if($signOffMessage === true) {
  91. continue;
  92. }
  93. $notSignedCommits[] = $commit;
  94. }
  95. if($notSignedCommits !== []) {
  96. echo("\n");
  97. echo("Some commits were not signed off!\n");
  98. echo("Missing signatures on:\n");
  99. foreach ($notSignedCommits as $commit) {
  100. echo("- " . $commit . "\n");
  101. }
  102. echo("Build has failed\n");
  103. exit(1);
  104. } else {
  105. exit(0);
  106. }