1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-26 13:39:08 +01:00
phorge-phorge/src/applications/differential/herald/HeraldDifferentialAdapter.php
epriestley c013108675 Modernize all Herald DifferentialDiff fields
Summary: Ref T8726. This deals with all the Differential diff fields, same deal as previous changes.

Test Plan:
  - Wrote a rule with every field.
  - Migrated it.
  - Saw the same rule working.
  - Rigged the hell out of transcripts (diffs normally do not generate transcripts, because the only action is "block" and they don't exist yet when Herald runs).
  - Verified that all fields looked sensible in the transcript.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T8726

Differential Revision: https://secure.phabricator.com/D13590
2015-07-08 12:25:38 -07:00

96 lines
2.4 KiB
PHP

<?php
abstract class HeraldDifferentialAdapter extends HeraldAdapter {
private $repository = false;
private $diff;
abstract protected function loadChangesets();
abstract protected function loadChangesetsWithHunks();
public function getDiff() {
return $this->diff;
}
public function setDiff(DifferentialDiff $diff) {
$this->diff = $diff;
return $this;
}
public function loadRepository() {
if ($this->repository === false) {
$repository_phid = $this->getObject()->getRepositoryPHID();
if ($repository_phid) {
$repository = id(new PhabricatorRepositoryQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withPHIDs(array($repository_phid))
->executeOne();
} else {
$repository = null;
}
$this->repository = $repository;
}
return $this->repository;
}
public function loadAffectedPaths() {
$changesets = $this->loadChangesets();
$paths = array();
foreach ($changesets as $changeset) {
$paths[] = $this->getAbsoluteRepositoryPathForChangeset($changeset);
}
return $paths;
}
protected function getAbsoluteRepositoryPathForChangeset(
DifferentialChangeset $changeset) {
$repository = $this->loadRepository();
if (!$repository) {
return '/'.ltrim($changeset->getFilename(), '/');
}
$diff = $this->getDiff();
return $changeset->getAbsoluteRepositoryPath($repository, $diff);
}
public function loadContentDictionary() {
$add_lines = DifferentialHunk::FLAG_LINES_ADDED;
$rem_lines = DifferentialHunk::FLAG_LINES_REMOVED;
$mask = ($add_lines | $rem_lines);
return $this->loadContentWithMask($mask);
}
public function loadAddedContentDictionary() {
return $this->loadContentWithMask(DifferentialHunk::FLAG_LINES_ADDED);
}
public function loadRemovedContentDictionary() {
return $this->loadContentWithMask(DifferentialHunk::FLAG_LINES_REMOVED);
}
protected function loadContentWithMask($mask) {
$changesets = $this->loadChangesetsWithHunks();
$dict = array();
foreach ($changesets as $changeset) {
$content = array();
foreach ($changeset->getHunks() as $hunk) {
$content[] = $hunk->getContentWithMask($mask);
}
$path = $this->getAbsoluteRepositoryPathForChangeset($changeset);
$dict[$path] = implode("\n", $content);
}
return $dict;
}
}