Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F15169
app/Listeners/PhabricatorListener.php
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
dereckson
Dec 21 2015, 17:11
2015-12-21 17:11:04 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
app/Listeners/PhabricatorListener.php
View Options
<?php
namespace
Nasqueron\Notifications\Listeners
;
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 new commit are t$callSignhere
*/
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
->
attachException
(
$ex
);
}
Event
::
fire
(
new
ReportEvent
(
$actionToReport
));
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public
function
subscribe
(
$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/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
13372
Default Alt Text
app/Listeners/PhabricatorListener.php (2 KB)
Attached To
Mode
P146 app/Listeners/PhabricatorListener.php
Attached
Detach File
Event Timeline
Log In to Comment