|
| 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 | +/** |
| 25 | + * Script to verify that all commits have been signed-off, if a commit doesn't end |
| 26 | + * with a signed-off message the script is failing. |
| 27 | + */ |
| 28 | +$baseDir = __DIR__ . '/../'; |
| 29 | + |
| 30 | +$pullRequestNumber = getenv('DRONE_PULL_REQUEST'); |
| 31 | + |
| 32 | +if(!is_string($pullRequestNumber) || $pullRequestNumber === '') { |
| 33 | + echo("The environment variable DRONE_PULL_REQUEST has no proper value.\n"); |
| 34 | + exit(1); |
| 35 | +} |
| 36 | + |
| 37 | +$ch = curl_init(); |
| 38 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 39 | +curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/nextcloud/server/pulls/'.$pullRequestNumber.'/commits'); |
| 40 | +curl_setopt($ch, CURLOPT_USERAGENT, 'CI for Nextcloud (https://github.com/nextcloud/server)'); |
| 41 | +$response = curl_exec($ch); |
| 42 | +curl_close($ch); |
| 43 | + |
| 44 | +shell_exec( |
| 45 | + sprintf( |
| 46 | + 'cd %s && git fetch', |
| 47 | + escapeshellarg($baseDir), |
| 48 | + escapeshellarg($pullRequestNumber) |
| 49 | + ) |
| 50 | +); |
| 51 | + |
| 52 | +$decodedResponse = json_decode($response, true); |
| 53 | +if(!is_array($decodedResponse) || count($decodedResponse) === 0) { |
| 54 | + echo("Could not decode JSON response from GitHub API.\n"); |
| 55 | + exit(1); |
| 56 | +} |
| 57 | + |
| 58 | +// Get all commits SHAs |
| 59 | +$commits = []; |
| 60 | + |
| 61 | +foreach($decodedResponse as $commit) { |
| 62 | + if(!isset($commit['sha'])) { |
| 63 | + echo("No SHA specified in $commit\n"); |
| 64 | + exit(1); |
| 65 | + } |
| 66 | + $commits[] = $commit['sha']; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +if(count($commits) < 1) { |
| 71 | + echo("Could not read commits.\n"); |
| 72 | + exit(1); |
| 73 | +} |
| 74 | + |
| 75 | +$notSignedCommits = []; |
| 76 | +foreach($commits as $commit) { |
| 77 | + if($commit === '') { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $signOffMessage = false; |
| 82 | + $commitMessageLines = |
| 83 | + explode( |
| 84 | + "\n", |
| 85 | + shell_exec( |
| 86 | + sprintf( |
| 87 | + 'cd %s && git rev-list --format=%%B --max-count=1 %s', |
| 88 | + $baseDir, |
| 89 | + $commit |
| 90 | + ) |
| 91 | + ) |
| 92 | + ); |
| 93 | + |
| 94 | + foreach($commitMessageLines as $line) { |
| 95 | + if(preg_match('/^Signed-off-by: .* <.*@.*>$/', $line)) { |
| 96 | + echo "$commit is signed-off with \"$line\"\n"; |
| 97 | + $signOffMessage = true; |
| 98 | + continue; |
| 99 | + } |
| 100 | + } |
| 101 | + if($signOffMessage === true) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + $notSignedCommits[] = $commit; |
| 106 | +} |
| 107 | + |
| 108 | +if($notSignedCommits !== []) { |
| 109 | + echo("\n"); |
| 110 | + echo("Some commits were not signed off!\n"); |
| 111 | + echo("Missing signatures on:\n"); |
| 112 | + foreach ($notSignedCommits as $commit) { |
| 113 | + echo("- " . $commit . "\n"); |
| 114 | + } |
| 115 | + echo("Build has failed\n"); |
| 116 | + exit(1); |
| 117 | +} else { |
| 118 | + exit(0); |
| 119 | +} |
| 120 | + |
0 commit comments