Skip to content

Commit f8de660

Browse files
LukasReschkeschiessle
authored andcommitted
Add checker for signed off commits
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
1 parent dcd44c6 commit f8de660

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

.drone.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pipeline:
2626
when:
2727
matrix:
2828
TESTS: app-check-code
29+
signed-off-check:
30+
image: nextcloudci/php7.0:php7.0-2
31+
commands:
32+
- php ./build/signed-off-checker.php
33+
when:
34+
matrix:
35+
TESTS: signed-off-check
2936
syntax-php5.6:
3037
image: nextcloudci/php5.6:php5.6-2
3138
commands:
@@ -153,6 +160,7 @@ pipeline:
153160

154161
matrix:
155162
include:
163+
- TESTS: signed-off-check
156164
- TESTS: integration
157165
- TESTS: jsunit
158166
- TESTS: check-autoloader

build/signed-off-checker.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)