1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-08 04:48:28 +01:00

Improve exception behavior for Herald commit rules which fail to load diff context

Summary:
This code is a little funky right now, and can return `array("error message")` and then try to call `getHunks()` on it. Additionally, each field loads the commit's changes separately.

Instead, load the commit's changes once and cache them, and handle exceptions appropriately.

Test Plan:
  - Created a rule like "changed, added, removed content all match /.*/" to force all fields to generate.
  - Ran it successfully.
  - Faked an error and ran it, got reasonable results.

Reviewers: btrahan

Reviewed By: btrahan

CC: bigo, aran

Differential Revision: https://secure.phabricator.com/D7384
This commit is contained in:
epriestley 2013-10-23 08:28:58 -07:00
parent b5a009337f
commit 2c3d071a26

View file

@ -17,6 +17,7 @@ final class HeraldCommitAdapter extends HeraldAdapter {
protected $repository; protected $repository;
protected $commit; protected $commit;
protected $commitData; protected $commitData;
private $commitDiff;
protected $emailPHIDs = array(); protected $emailPHIDs = array();
protected $addCCPHIDs = array(); protected $addCCPHIDs = array();
@ -272,15 +273,50 @@ final class HeraldCommitAdapter extends HeraldAdapter {
return $diff; return $diff;
} }
private function loadChangesets() { private function getDiffContent($type) {
try { if ($this->commitDiff === null) {
$diff = $this->loadCommitDiff(); try {
} catch (Exception $ex) { $this->commitDiff = $this->loadCommitDiff();
return array( } catch (Exception $ex) {
'<<< Failed to load diff, this may mean the change was '. $this->commitDiff = $ex;
'unimaginably enormous. >>>'); phlog($ex);
}
} }
return $diff->getChangesets();
if ($this->commitDiff instanceof Exception) {
$ex = $this->commitDiff;
$ex_class = get_class($ex);
$ex_message = pht('Failed to load changes: %s', $ex->getMessage());
return array(
'<'.$ex_class.'>' => $ex_message,
);
}
$changes = $this->commitDiff->getChangesets();
$result = array();
foreach ($changes as $change) {
$lines = array();
foreach ($change->getHunks() as $hunk) {
switch ($type) {
case '-':
$lines[] = $hunk->makeOldFile();
break;
case '+':
$lines[] = $hunk->makeNewFile();
break;
case '*':
$lines[] = $hunk->makeChanges();
break;
default:
throw new Exception("Unknown content selection '{$type}'!");
}
}
$result[$change->getFilename()] = implode("\n", $lines);
}
return $result;
} }
public function getHeraldField($field) { public function getHeraldField($field) {
@ -299,41 +335,11 @@ final class HeraldCommitAdapter extends HeraldAdapter {
case self::FIELD_REPOSITORY: case self::FIELD_REPOSITORY:
return $this->repository->getPHID(); return $this->repository->getPHID();
case self::FIELD_DIFF_CONTENT: case self::FIELD_DIFF_CONTENT:
$dict = array(); return $this->getDiffContent('*');
$lines = array();
$changes = $this->loadChangesets();
foreach ($changes as $change) {
$lines = array();
foreach ($change->getHunks() as $hunk) {
$lines[] = $hunk->makeChanges();
}
$dict[$change->getFilename()] = implode("\n", $lines);
}
return $dict;
case self::FIELD_DIFF_ADDED_CONTENT: case self::FIELD_DIFF_ADDED_CONTENT:
$dict = array(); return $this->getDiffContent('+');
$lines = array();
$changes = $this->loadChangesets();
foreach ($changes as $change) {
$lines = array();
foreach ($change->getHunks() as $hunk) {
$lines[] = implode('', $hunk->getAddedLines());
}
$dict[$change->getFilename()] = implode("\n", $lines);
}
return $dict;
case self::FIELD_DIFF_REMOVED_CONTENT: case self::FIELD_DIFF_REMOVED_CONTENT:
$dict = array(); return $this->getDiffContent('-');
$lines = array();
$changes = $this->loadChangesets();
foreach ($changes as $change) {
$lines = array();
foreach ($change->getHunks() as $hunk) {
$lines[] = implode('', $hunk->getRemovedLines());
}
$dict[$change->getFilename()] = implode("\n", $lines);
}
return $dict;
case self::FIELD_AFFECTED_PACKAGE: case self::FIELD_AFFECTED_PACKAGE:
$packages = $this->loadAffectedPackages(); $packages = $this->loadAffectedPackages();
return mpull($packages, 'getPHID'); return mpull($packages, 'getPHID');