<?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) {
phlog('applying jira 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());
foreach ($list as $watcher) {
$usernames[] = strtolower(idx($watcher, 'key'$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.');
}
}