mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-11 09:22:40 +01:00
134c8f5547
Summary: Ref T4195. Adds "Author" and "Committer" fields. Test Plan: Created a rule using these fields: {F90897} ...then pushed git, mercurial and svn commits and verified the correct values populated in the transcript: {F90898} Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4195 Differential Revision: https://secure.phabricator.com/D7802
247 lines
6.8 KiB
PHP
247 lines
6.8 KiB
PHP
<?php
|
|
|
|
final class HeraldPreCommitContentAdapter extends HeraldAdapter {
|
|
|
|
private $log;
|
|
private $hookEngine;
|
|
private $changesets;
|
|
private $commitRef;
|
|
|
|
public function setPushLog(PhabricatorRepositoryPushLog $log) {
|
|
$this->log = $log;
|
|
return $this;
|
|
}
|
|
|
|
public function setHookEngine(DiffusionCommitHookEngine $engine) {
|
|
$this->hookEngine = $engine;
|
|
return $this;
|
|
}
|
|
|
|
public function getAdapterApplicationClass() {
|
|
return 'PhabricatorApplicationDiffusion';
|
|
}
|
|
|
|
public function getObject() {
|
|
return $this->log;
|
|
}
|
|
|
|
public function getAdapterContentName() {
|
|
return pht('Commit Hook: Commit Content');
|
|
}
|
|
|
|
public function getFieldNameMap() {
|
|
return array(
|
|
) + parent::getFieldNameMap();
|
|
}
|
|
|
|
public function getFields() {
|
|
return array_merge(
|
|
array(
|
|
self::FIELD_BODY,
|
|
self::FIELD_AUTHOR,
|
|
self::FIELD_COMMITTER,
|
|
self::FIELD_DIFF_FILE,
|
|
self::FIELD_DIFF_CONTENT,
|
|
self::FIELD_DIFF_ADDED_CONTENT,
|
|
self::FIELD_DIFF_REMOVED_CONTENT,
|
|
self::FIELD_REPOSITORY,
|
|
self::FIELD_PUSHER,
|
|
self::FIELD_PUSHER_PROJECTS,
|
|
self::FIELD_RULE,
|
|
),
|
|
parent::getFields());
|
|
}
|
|
|
|
public function getConditionsForField($field) {
|
|
switch ($field) {
|
|
}
|
|
return parent::getConditionsForField($field);
|
|
}
|
|
|
|
public function getActions($rule_type) {
|
|
switch ($rule_type) {
|
|
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
|
|
return array(
|
|
self::ACTION_BLOCK,
|
|
self::ACTION_NOTHING
|
|
);
|
|
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
|
|
return array(
|
|
self::ACTION_NOTHING,
|
|
);
|
|
}
|
|
}
|
|
|
|
public function getValueTypeForFieldAndCondition($field, $condition) {
|
|
return parent::getValueTypeForFieldAndCondition($field, $condition);
|
|
}
|
|
|
|
public function getPHID() {
|
|
return $this->getObject()->getPHID();
|
|
}
|
|
|
|
public function getHeraldName() {
|
|
return pht('Push Log');
|
|
}
|
|
|
|
public function getHeraldField($field) {
|
|
$log = $this->getObject();
|
|
switch ($field) {
|
|
case self::FIELD_BODY:
|
|
return $this->getCommitRef()->getMessage();
|
|
case self::FIELD_AUTHOR:
|
|
return $this->getAuthorPHID();
|
|
case self::FIELD_COMMITTER:
|
|
return $this->getCommitterPHID();
|
|
case self::FIELD_DIFF_FILE:
|
|
return $this->getDiffContent('name');
|
|
case self::FIELD_DIFF_CONTENT:
|
|
return $this->getDiffContent('*');
|
|
case self::FIELD_DIFF_ADDED_CONTENT:
|
|
return $this->getDiffContent('+');
|
|
case self::FIELD_DIFF_REMOVED_CONTENT:
|
|
return $this->getDiffContent('-');
|
|
case self::FIELD_REPOSITORY:
|
|
return $this->hookEngine->getRepository()->getPHID();
|
|
case self::FIELD_PUSHER:
|
|
return $this->hookEngine->getViewer()->getPHID();
|
|
case self::FIELD_PUSHER_PROJECTS:
|
|
return $this->hookEngine->loadViewerProjectPHIDsForHerald();
|
|
}
|
|
|
|
return parent::getHeraldField($field);
|
|
}
|
|
|
|
public function applyHeraldEffects(array $effects) {
|
|
assert_instances_of($effects, 'HeraldEffect');
|
|
|
|
$result = array();
|
|
foreach ($effects as $effect) {
|
|
$action = $effect->getAction();
|
|
switch ($action) {
|
|
case self::ACTION_NOTHING:
|
|
$result[] = new HeraldApplyTranscript(
|
|
$effect,
|
|
true,
|
|
pht('Did nothing.'));
|
|
break;
|
|
case self::ACTION_BLOCK:
|
|
$result[] = new HeraldApplyTranscript(
|
|
$effect,
|
|
true,
|
|
pht('Blocked push.'));
|
|
break;
|
|
default:
|
|
throw new Exception(pht('No rules to handle action "%s"!', $action));
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function getDiffContent($type) {
|
|
if ($this->changesets === null) {
|
|
try {
|
|
$this->changesets = $this->hookEngine->loadChangesetsForCommit(
|
|
$this->log->getRefNew());
|
|
} catch (Exception $ex) {
|
|
$this->changesets = $ex;
|
|
}
|
|
}
|
|
|
|
if ($this->changesets instanceof Exception) {
|
|
$ex_class = get_class($this->changesets);
|
|
$ex_message = $this->changesets->getmessage();
|
|
if ($type === 'name') {
|
|
return array("<{$ex_class}: {$ex_message}>");
|
|
} else {
|
|
return array("<{$ex_class}>" => $ex_message);
|
|
}
|
|
}
|
|
|
|
$result = array();
|
|
if ($type === 'name') {
|
|
foreach ($this->changesets as $change) {
|
|
$result[] = $change->getFilename();
|
|
}
|
|
} else {
|
|
foreach ($this->changesets as $change) {
|
|
$lines = array();
|
|
foreach ($change->getHunks() as $hunk) {
|
|
switch ($type) {
|
|
case '-':
|
|
$lines[] = $hunk->makeOldFile();
|
|
break;
|
|
case '+':
|
|
$lines[] = $hunk->makeNewFile();
|
|
break;
|
|
case '*':
|
|
default:
|
|
$lines[] = $hunk->makeChanges();
|
|
break;
|
|
}
|
|
}
|
|
$result[$change->getFilename()] = implode('', $lines);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function getCommitRef() {
|
|
if ($this->commitRef === null) {
|
|
$this->commitRef = $this->hookEngine->loadCommitRefForCommit(
|
|
$this->log->getRefNew());
|
|
}
|
|
return $this->commitRef;
|
|
}
|
|
|
|
private function getAuthorPHID() {
|
|
$repository = $this->hookEngine->getRepository();
|
|
$vcs = $repository->getVersionControlSystem();
|
|
switch ($vcs) {
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
|
|
$ref = $this->getCommitRef();
|
|
$author = $ref->getAuthor();
|
|
if (!strlen($author)) {
|
|
return null;
|
|
}
|
|
return $this->lookupUser($author);
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
|
|
// In Subversion, the pusher is always the author.
|
|
return $this->hookEngine->getViewer()->getPHID();
|
|
}
|
|
}
|
|
|
|
private function getCommitterPHID() {
|
|
$repository = $this->hookEngine->getRepository();
|
|
$vcs = $repository->getVersionControlSystem();
|
|
switch ($vcs) {
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
|
|
// Here, if there's no committer, we're going to return the author
|
|
// instead.
|
|
$ref = $this->getCommitRef();
|
|
$committer = $ref->getCommitter();
|
|
if (!strlen($committer)) {
|
|
return $this->getAuthorPHID();
|
|
}
|
|
$phid = $this->lookupUser($committer);
|
|
if (!$phid) {
|
|
return $this->getAuthorPHID();
|
|
}
|
|
return $phid;
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
|
|
// In Subversion, the pusher is always the committer.
|
|
return $this->hookEngine->getViewer()->getPHID();
|
|
}
|
|
}
|
|
|
|
private function lookupUser($author) {
|
|
return id(new DiffusionResolveUserQuery())
|
|
->withName($author)
|
|
->execute();
|
|
}
|
|
|
|
}
|