Skip to content

Commit 4dd058e

Browse files
committed
fix #29: track hash changes of branches to avoid cheating
1 parent 630b6be commit 4dd058e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

cron.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ function run_patch_stats() {
173173
continue;
174174
try {
175175
$oldstatus = $patch->getStatus();
176+
$oldhash = $patch->hash;
176177
$patch->updateStats();
177178
$newstatus = $patch->getStatus();
179+
$newhash = $patch->hash;
178180

179181
if ($newstatus != $oldstatus) {
180182
$patch->comments->add(
@@ -187,9 +189,15 @@ function run_patch_stats() {
187189
link_patch($patch));
188190
echo "Patch $patch->id changed status from $oldstatus to ",
189191
$newstatus, "\n";
190-
} else {
191-
echo "Patch $patch->id status unchanged\n";
192192
}
193+
194+
if ($newhash != $oldhash) {
195+
$patch->comments->add(
196+
new PatchComment($patch, "New branch hash: $newhash"));
197+
echo "Patch $patch->id changed hash from $oldhash to $newhash\n";
198+
}
199+
echo "Updated patch $patch->id\n";
200+
193201
} catch (ValidationException $ex) {
194202
error_ta($group, "Patch $patch->id is broken", <<< EOF
195203
Cron job failed to process patch $patch->id

entities/GitHub/GitHubPatch.php

+9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function commits() : array {
9797
$commit['name'] = $commit['commit']['author']['name'];
9898
$commit['email'] = $commit['commit']['author']['email'];
9999
$commit['message'] = $commit['commit']['message'];
100+
$commit['hash'] = $commit['sha'];
100101

101102
$authors = [];
102103
preg_match_all('/^Co-authored-by: ([^<]+) <([^>]+)>$/Sm',
@@ -111,6 +112,14 @@ public function commits() : array {
111112
return $commits;
112113
}
113114

115+
protected function computeBranchHash() : string {
116+
$hash = '';
117+
foreach ($this->stats()['commits'] as $commit) {
118+
$hash = $commit['sha'];
119+
}
120+
return $hash;
121+
}
122+
114123
protected function computeLinesAdded() : int {
115124
$add = 0;
116125
foreach ($this->stats()['files'] as $f) {

entities/Patch.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ abstract class Patch
7272
/** @ManyToMany(targetEntity="User") */
7373
public $students;
7474

75+
/** @Column(length=64) */
76+
public string $hash = '';
77+
7578
/** @Column */
7679
public int $lines_added;
7780

@@ -94,16 +97,17 @@ static function factory(ProjGroup $group, string $url, $type,
9497
$p->type = (int)$type;
9598
$p->issue_url = check_url($issue_url);
9699

97-
$description = trim($description);
98-
$p->comments->add(
99-
new PatchComment($p, "Patch submitted\n$description", $submitter));
100-
101100
try {
102101
$p->updateStats();
103102
} catch (Exception $ex) {
104103
throw new ValidationException('Patch not found');
105104
}
106105

106+
$description = trim($description);
107+
$p->comments->add(
108+
new PatchComment($p, "Patch submitted; hash: {$p->hash}\n\n$description",
109+
$submitter));
110+
107111
try {
108112
if (!$description)
109113
throw new ValidationException("Empty description");
@@ -154,7 +158,7 @@ static function factory(ProjGroup $group, string $url, $type,
154158
"the end\n$msg");
155159
}
156160
}
157-
}
161+
}
158162

159163
check_reasonable_name($commit['name'], $group);
160164
check_wrapped_commit_text($msg, 72);
@@ -206,6 +210,7 @@ abstract public function isValid() : bool;
206210
abstract public function branch() : string;
207211
abstract public function origin() : string;
208212
abstract public function commits() : array;
213+
abstract protected function computeBranchHash() : string;
209214
abstract protected function computeLinesAdded() : int;
210215
abstract protected function computeLinesDeleted() : int;
211216
abstract protected function computeFilesModified() : int;
@@ -214,6 +219,8 @@ abstract public function setPR(PullRequest $pr);
214219
abstract public function getPR() : ?PullRequest;
215220

216221
public function updateStats() {
222+
$this->hash = $this->computeBranchHash();
223+
217224
if ($pr = $this->getPR()) {
218225
$legal = $this->status == PATCH_PR_OPEN;
219226
if ($pr->wasMerged()) {

0 commit comments

Comments
 (0)