1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-04 04:32:43 +01:00
phorge-phorge/src/applications/differential/herald/HeraldDifferentialRevisionAdapter.php

174 lines
4.3 KiB
PHP
Raw Normal View History

<?php
final class HeraldDifferentialRevisionAdapter
extends HeraldDifferentialAdapter {
protected $revision;
protected $buildPlans = array();
protected $affectedPackages;
protected $changesets;
private $haveHunks;
public function getAdapterApplicationClass() {
return 'PhabricatorDifferentialApplication';
}
protected function newObject() {
return new DifferentialRevision();
}
protected function initializeNewAdapter() {
$this->revision = $this->newObject();
}
public function getObject() {
return $this->revision;
}
public function getAdapterContentType() {
return 'differential';
}
public function getAdapterContentName() {
return pht('Differential Revisions');
}
public function getAdapterContentDescription() {
return pht(
"React to revisions being created or updated.\n".
"Revision rules can send email, flag revisions, add reviewers, ".
"and run build plans.");
}
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
default:
return false;
}
}
public function getRepetitionOptions() {
return array(
HeraldRepetitionPolicyConfig::EVERY,
HeraldRepetitionPolicyConfig::FIRST,
);
}
public static function newLegacyAdapter(
DifferentialRevision $revision,
DifferentialDiff $diff) {
$object = new HeraldDifferentialRevisionAdapter();
// Reload the revision to pick up relationship information.
$revision = id(new DifferentialRevisionQuery())
->withIDs(array($revision->getID()))
->setViewer(PhabricatorUser::getOmnipotentUser())
->needRelationships(true)
->needReviewerStatus(true)
->executeOne();
$object->revision = $revision;
$object->setDiff($diff);
return $object;
}
public function getBuildPlans() {
return $this->buildPlans;
}
public function getHeraldName() {
return $this->revision->getTitle();
}
protected function loadChangesets() {
if ($this->changesets === null) {
$this->changesets = $this->getDiff()->loadChangesets();
}
return $this->changesets;
}
protected function loadChangesetsWithHunks() {
$changesets = $this->loadChangesets();
if ($changesets && !$this->haveHunks) {
$this->haveHunks = true;
id(new DifferentialHunkQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withChangesets($changesets)
->needAttachToChangesets(true)
->execute();
}
return $changesets;
}
public function loadAffectedPackages() {
if ($this->affectedPackages === null) {
$this->affectedPackages = array();
$repository = $this->loadRepository();
if ($repository) {
$packages = PhabricatorOwnersPackage::loadAffectedPackages(
$repository,
$this->loadAffectedPaths());
$this->affectedPackages = $packages;
}
}
return $this->affectedPackages;
}
public function loadReviewers() {
$reviewers = $this->getObject()->getReviewerStatus();
return mpull($reviewers, 'getReviewerPHID');
}
public function getActions($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
Support custom actions in Herald Summary: This was significantly easier than expected. Here's an example of what an extension class might look like: ``` <?php final class AddRiskReviewHeraldCustomAction extends HeraldCustomAction { public function appliesToAdapter(HeraldAdapter $adapter) { return $adapter instanceof HeraldDifferentialRevisionAdapter; } public function appliesToRuleType($rule_type) { return $rule_type == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL || $rule_type == HeraldRuleTypeConfig::RULE_TYPE_OBJECT; } public function getActionKey() { return 'custom:add-risk'; } public function getActionName() { return 'Add risk rating (JSON)'; } public function getActionType() { return HeraldAdapter::VALUE_TEXT; } public function applyEffect( HeraldAdapter $adapter, $object, HeraldEffect $effect) { $key = "phragile:risk-rating"; // Read existing value. $field_list = PhabricatorCustomField::getObjectFields( $object, PhabricatorCustomField::ROLE_VIEW); $field_list->readFieldsFromStorage($object); $field_list = mpull($field_list->getFields(), null, 'getFieldKey'); $field = $field_list[$key]; $field->setObject($object); $field->setViewer(PhabricatorUser::getOmnipotentUser()); $risk = $field->getValue(); $old_risk = $risk; // PHP copies arrays by default! // Add new value to array. $herald_args = phutil_json_decode($effect->getTarget()); $risk[$herald_args['key']] = array( 'value' => $herald_args['value'], 'reason' => $herald_args['reason']); $risk_key = $herald_args['key']; // Set new value. $adapter->queueTransaction( id(new DifferentialTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) ->setMetadataValue('customfield:key', $key) ->setOldValue($old_risk) ->setNewValue($risk)); return new HeraldApplyTranscript( $effect, true, pht( 'Modifying automatic risk ratings (key: %s)!', $risk_key)); } } ``` Test Plan: Created a custom action for differential revisions, set up a Herald rule to match and trigger the custom action, did 'arc diff' and saw the action trigger in the transcripts. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: locutus, edutibau, ite-klass, epriestley, Korvin Maniphest Tasks: T4884 Differential Revision: https://secure.phabricator.com/D8784
2014-07-02 06:29:46 +02:00
return array_merge(
array(
self::ACTION_APPLY_BUILD_PLANS,
),
parent::getActions($rule_type));
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
Support custom actions in Herald Summary: This was significantly easier than expected. Here's an example of what an extension class might look like: ``` <?php final class AddRiskReviewHeraldCustomAction extends HeraldCustomAction { public function appliesToAdapter(HeraldAdapter $adapter) { return $adapter instanceof HeraldDifferentialRevisionAdapter; } public function appliesToRuleType($rule_type) { return $rule_type == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL || $rule_type == HeraldRuleTypeConfig::RULE_TYPE_OBJECT; } public function getActionKey() { return 'custom:add-risk'; } public function getActionName() { return 'Add risk rating (JSON)'; } public function getActionType() { return HeraldAdapter::VALUE_TEXT; } public function applyEffect( HeraldAdapter $adapter, $object, HeraldEffect $effect) { $key = "phragile:risk-rating"; // Read existing value. $field_list = PhabricatorCustomField::getObjectFields( $object, PhabricatorCustomField::ROLE_VIEW); $field_list->readFieldsFromStorage($object); $field_list = mpull($field_list->getFields(), null, 'getFieldKey'); $field = $field_list[$key]; $field->setObject($object); $field->setViewer(PhabricatorUser::getOmnipotentUser()); $risk = $field->getValue(); $old_risk = $risk; // PHP copies arrays by default! // Add new value to array. $herald_args = phutil_json_decode($effect->getTarget()); $risk[$herald_args['key']] = array( 'value' => $herald_args['value'], 'reason' => $herald_args['reason']); $risk_key = $herald_args['key']; // Set new value. $adapter->queueTransaction( id(new DifferentialTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) ->setMetadataValue('customfield:key', $key) ->setOldValue($old_risk) ->setNewValue($risk)); return new HeraldApplyTranscript( $effect, true, pht( 'Modifying automatic risk ratings (key: %s)!', $risk_key)); } } ``` Test Plan: Created a custom action for differential revisions, set up a Herald rule to match and trigger the custom action, did 'arc diff' and saw the action trigger in the transcripts. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: locutus, edutibau, ite-klass, epriestley, Korvin Maniphest Tasks: T4884 Differential Revision: https://secure.phabricator.com/D8784
2014-07-02 06:29:46 +02:00
return array_merge(
array(),
Support custom actions in Herald Summary: This was significantly easier than expected. Here's an example of what an extension class might look like: ``` <?php final class AddRiskReviewHeraldCustomAction extends HeraldCustomAction { public function appliesToAdapter(HeraldAdapter $adapter) { return $adapter instanceof HeraldDifferentialRevisionAdapter; } public function appliesToRuleType($rule_type) { return $rule_type == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL || $rule_type == HeraldRuleTypeConfig::RULE_TYPE_OBJECT; } public function getActionKey() { return 'custom:add-risk'; } public function getActionName() { return 'Add risk rating (JSON)'; } public function getActionType() { return HeraldAdapter::VALUE_TEXT; } public function applyEffect( HeraldAdapter $adapter, $object, HeraldEffect $effect) { $key = "phragile:risk-rating"; // Read existing value. $field_list = PhabricatorCustomField::getObjectFields( $object, PhabricatorCustomField::ROLE_VIEW); $field_list->readFieldsFromStorage($object); $field_list = mpull($field_list->getFields(), null, 'getFieldKey'); $field = $field_list[$key]; $field->setObject($object); $field->setViewer(PhabricatorUser::getOmnipotentUser()); $risk = $field->getValue(); $old_risk = $risk; // PHP copies arrays by default! // Add new value to array. $herald_args = phutil_json_decode($effect->getTarget()); $risk[$herald_args['key']] = array( 'value' => $herald_args['value'], 'reason' => $herald_args['reason']); $risk_key = $herald_args['key']; // Set new value. $adapter->queueTransaction( id(new DifferentialTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) ->setMetadataValue('customfield:key', $key) ->setOldValue($old_risk) ->setNewValue($risk)); return new HeraldApplyTranscript( $effect, true, pht( 'Modifying automatic risk ratings (key: %s)!', $risk_key)); } } ``` Test Plan: Created a custom action for differential revisions, set up a Herald rule to match and trigger the custom action, did 'arc diff' and saw the action trigger in the transcripts. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: locutus, edutibau, ite-klass, epriestley, Korvin Maniphest Tasks: T4884 Differential Revision: https://secure.phabricator.com/D8784
2014-07-02 06:29:46 +02:00
parent::getActions($rule_type));
}
}
public function applyHeraldEffects(array $effects) {
assert_instances_of($effects, 'HeraldEffect');
$result = array();
foreach ($effects as $effect) {
$action = $effect->getAction();
switch ($action) {
case self::ACTION_APPLY_BUILD_PLANS:
foreach ($effect->getTarget() as $phid) {
$this->buildPlans[] = $phid;
}
$result[] = new HeraldApplyTranscript(
$effect,
true,
pht('Applied build plans.'));
break;
default:
$result[] = $this->applyStandardEffect($effect);
Support custom actions in Herald Summary: This was significantly easier than expected. Here's an example of what an extension class might look like: ``` <?php final class AddRiskReviewHeraldCustomAction extends HeraldCustomAction { public function appliesToAdapter(HeraldAdapter $adapter) { return $adapter instanceof HeraldDifferentialRevisionAdapter; } public function appliesToRuleType($rule_type) { return $rule_type == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL || $rule_type == HeraldRuleTypeConfig::RULE_TYPE_OBJECT; } public function getActionKey() { return 'custom:add-risk'; } public function getActionName() { return 'Add risk rating (JSON)'; } public function getActionType() { return HeraldAdapter::VALUE_TEXT; } public function applyEffect( HeraldAdapter $adapter, $object, HeraldEffect $effect) { $key = "phragile:risk-rating"; // Read existing value. $field_list = PhabricatorCustomField::getObjectFields( $object, PhabricatorCustomField::ROLE_VIEW); $field_list->readFieldsFromStorage($object); $field_list = mpull($field_list->getFields(), null, 'getFieldKey'); $field = $field_list[$key]; $field->setObject($object); $field->setViewer(PhabricatorUser::getOmnipotentUser()); $risk = $field->getValue(); $old_risk = $risk; // PHP copies arrays by default! // Add new value to array. $herald_args = phutil_json_decode($effect->getTarget()); $risk[$herald_args['key']] = array( 'value' => $herald_args['value'], 'reason' => $herald_args['reason']); $risk_key = $herald_args['key']; // Set new value. $adapter->queueTransaction( id(new DifferentialTransaction()) ->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD) ->setMetadataValue('customfield:key', $key) ->setOldValue($old_risk) ->setNewValue($risk)); return new HeraldApplyTranscript( $effect, true, pht( 'Modifying automatic risk ratings (key: %s)!', $risk_key)); } } ``` Test Plan: Created a custom action for differential revisions, set up a Herald rule to match and trigger the custom action, did 'arc diff' and saw the action trigger in the transcripts. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: locutus, edutibau, ite-klass, epriestley, Korvin Maniphest Tasks: T4884 Differential Revision: https://secure.phabricator.com/D8784
2014-07-02 06:29:46 +02:00
break;
}
}
return $result;
}
}