Page MenuHomePhabricator
Paste P2001

AddSubscribersFromJIRAIssuesHeraldCustomAction.php
ActivePublic

Authored by avivey on Jul 14 2016, 11:51 PM.
<?php
final class AddSubscribersFromJIRAIssuesHeraldCustomAction
extends HeraldAction {
const ACTIONCONST = 'jira:add-cc';
public function supportsRuleType($rule_type) {
return ($rule_type != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
}
public function supportsObject($object) {
return $object instanceof DifferentialRevision;
}
public function getHeraldActionName() {
return 'Add Subscribers from JIRA issues';
}
public function getHeraldActionStandardType() {
return self::STANDARD_NONE;
}
public function applyEffect($object, HeraldEffect $effect) {
$rule = $effect->getRule();
$viewer = PhabricatorUser::getOmnipotentUser();
$adapter = $this->getAdapter();
$provider = PhabricatorJIRAAuthProvider::getJIRAProvider();
if (!$provider) {
return;
}
$jira_issue_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
$object->getPHID(),
PhabricatorJiraIssueHasObjectEdgeType::EDGECONST);
if (!$jira_issue_phids) {
return;
}
$xobjs = id(new DoorkeeperExternalObjectQuery())
->setViewer($viewer)
->withPHIDs($jira_issue_phids)
->execute();
$xobjs = mgroup($xobjs, 'getApplicationDomain');
if (!$xobjs) {
return;
}
$cc_phids = array();
foreach ($xobjs as $domain => $xobj_list) {
$account = id(new PhabricatorExternalAccountQuery())
->setViewer($viewer)
->withUserPHIDs(array($rule->getAuthorPHID())) // TODO?
->withAccountTypes(array($provider->getProviderType()))
->withAccountDomains(array($domain))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
$futures = array();
foreach ($xobj_list as $key => $xobj) {
$id = $xobj->getObjectID();
$futures[$key] = $provider->newJIRAFuture(
$account,
'rest/api/2/issue/'.phutil_escape_uri($id).'/watchers',
'GET');
}
$usernames = array();
foreach (new FutureIterator($futures) as $key => $future) {
try {
$result = $future->resolveJSON();
$list = idx($result, 'watchers', array());
$list = ifilter($list, 'key');
$usernames[] = ipull($list, 'key');
} catch (Exception $ex) {
phlog($ex);
}
}
$usernames = array_mergev($usernames);
if (!$usernames) {
continue;
}
$externals = id(new PhabricatorExternalAccountQuery())
->setViewer($viewer)
->withAccountTypes(array($provider->getProviderType()))
->withAccountIDs($usernames)
->withAccountDomains(array($domain))
->execute();
$cc_phids[] = mpull($externals, 'getUserPHID');
}
$cc_phids = array_mergev($cc_phids);
if (!$cc_phids) {
return;
}
$xaction = $adapter->newTransaction()
->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
->setNewValue(
array(
'+' => $cc_phids,
));
$adapter->queueTransaction($xaction);
$this->logEffect('do.subscribe', $cc_phids);
}
public function getActionEffectMap() {
return array(
'do.subscribe' => array(
'icon' => 'fa-envelope',
'color' => 'green',
'name' => pht('Add subscribers from JIRA'),
),
);
}
public function renderActionDescription($value) {
return pht('Add Subscribers from JIRA issues.');
}
}

Event Timeline

avivey created this paste.Jul 14 2016, 11:51 PM
avivey changed the visibility from "All Users" to "Public (No Login Required)".
This comment was removed by avivey.
avivey edited the content of this paste. (Show Details)Jul 21 2016, 9:54 PM
avivey edited the content of this paste. (Show Details)Aug 18 2016, 2:09 AM