Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P2001
AddSubscribersFromJIRAIssuesHeraldCustomAction.php
Active
Public
Actions
Authored by
avivey
on Jul 14 2016, 11:51 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
Herald
Subscribers
None
<?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
2016-07-14 23:51:25 (UTC+0)
avivey
changed the visibility from "All Users" to "Public (No Login Required)".
This comment was removed by
avivey
.
avivey
mentioned this in
Community Resources
.
Jul 14 2016, 11:55 PM
2016-07-14 23:55:54 (UTC+0)
avivey
edited the content of this paste.
(Show Details)
Jul 21 2016, 9:54 PM
2016-07-21 21:54:04 (UTC+0)
avivey
edited the content of this paste.
(Show Details)
Aug 18 2016, 2:09 AM
2016-08-18 02:09:24 (UTC+0)
Log In to Comment