Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F10813569
PhabricatorListener.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
PhabricatorListener.php
View Options
<?php
namespace
Nasqueron\Notifications\Listeners
;
use
Nasqueron\Notifications\Actions\ActionError
;
use
Nasqueron\Notifications\Actions\NotifyNewCommitsAction
;
use
Nasqueron\Notifications\Events\GitHubPayloadEvent
;
use
Nasqueron\Notifications\Events\ReportEvent
;
use
Nasqueron\Notifications\Phabricator\PhabricatorAPI
;
use
Nasqueron\Notifications\Phabricator\PhabricatorAPIException
;
use
Event
;
class
PhabricatorListener
{
///
/// GitHub → Phabricator
///
/**
* Handles payload events
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
public
function
onGitHubPayload
(
GitHubPayloadEvent
$event
)
{
if
(
$event
->
event
===
'push'
)
{
$this
->
notifyNewCommits
(
$event
);
}
}
/**
* @return string the repository call sign "OPS", or "" if not in Phabricator
*/
private
static
function
getCallSign
(
PhabricatorAPI
$api
,
$remoteURI
)
{
$reply
=
$api
->
call
(
'repository.query'
,
[
'remoteURIs[0]'
=>
$remoteURI
]
);
if
(!
count
(
$reply
))
{
return
""
;
}
return
PhabricatorAPI
::
getFirstResult
(
$reply
)->
callsign
;
}
/**
* Notifies Phabricator there are new commits to pull
*/
public
function
notifyNewCommits
(
GitHubPayloadEvent
$event
)
{
$api
=
PhabricatorAPI
::
forProject
(
$event
->
door
);
if
(!
$api
)
{
// We don't have a Phabricator instance for this project.
return
;
}
$callSign
=
static
::
getCallSign
(
$api
,
$event
->
payload
->
repository
->
clone_url
);
if
(
$callSign
===
""
)
{
return
;
}
$actionToReport
=
new
NotifyNewCommitsAction
(
$callSign
);
try
{
$api
->
call
(
'diffusion.looksoon'
,
[
'callsigns[0]'
=>
$callSign
]
);
}
catch
(
PhabricatorAPIException
$ex
)
{
$actionToReport
->
attachError
(
new
ActionError
(
$ex
));
}
Event
::
fire
(
new
ReportEvent
(
$actionToReport
));
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public
function
subscribe
(
\Illuminate\Events\Dispatcher
$events
)
{
$class
=
'Nasqueron
\N
otifications
\L
isteners
\P
habricatorListener'
;
$events
->
listen
(
'Nasqueron
\N
otifications
\E
vents
\G
itHubPayloadEvent'
,
"$class@onGitHubPayload"
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Jul 29, 13:55 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2815304
Default Alt Text
PhabricatorListener.php (2 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment