Skip to content

Commit 630b6be

Browse files
committed
fix #35: stricter filters for co-authored-by lines
1 parent f65074a commit 630b6be

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

entities/GitHub/GitHubPatch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function commits() : array {
100100

101101
$authors = [];
102102
preg_match_all('/^Co-authored-by: ([^<]+) <([^>]+)>$/Sm',
103-
$commit['commit']['message'], $m, PREG_SET_ORDER);
103+
$commit['message'], $m, PREG_SET_ORDER);
104104
foreach ($m as $author) {
105105
$authors[] = ['', $author[1], $author[2]];
106106
}

entities/Patch.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,28 @@ static function factory(ProjGroup $group, string $url, $type,
136136
throw new ValidationException(
137137
'Invalid email used in commit: ' . $commit['email']);
138138

139-
if (str_starts_with($commit['message'], 'Merge branch '))
139+
$msg = $commit['message'];
140+
if (str_starts_with($msg, 'Merge branch '))
140141
throw new ValidationException('Merge commits are not allowed');
141142

142143
if (empty($commit['co-authored']) &&
143-
preg_match('/Co[- ]*authored[- ]*by\s*:.*/Si',
144-
$commit['commit']['message'], $m))
144+
preg_match('/Co[- ]*authored[- ]*by\s*:.*/Si', $msg, $m))
145145
throw new ValidationException("Invalid Co-authored-by line:\n$m[0]");
146146

147+
if (!empty($commit['co-authored'])) {
148+
$coauthor = false;
149+
foreach (explode("\n", trim($msg)) as $line) {
150+
if (str_starts_with($line, 'Co-authored-by:')) {
151+
$coauthor = true;
152+
} else if ($coauthor) {
153+
throw new ValidationException("Co-authored-by lines must be at ".
154+
"the end\n$msg");
155+
}
156+
}
157+
}
158+
147159
check_reasonable_name($commit['name'], $group);
148-
check_wrapped_commit_text($commit['message'], 72);
160+
check_wrapped_commit_text($msg, 72);
149161
}
150162

151163
if ($url_exception = DONT_WANT_ISSUE_IN_COMMIT_MSG[$repo->id] ?? '') {

0 commit comments

Comments
 (0)