Page MenuHomePhabricator

Update Packagist from Phabricator
ActivePublic

Authored by cesar on Fri, Nov 6, 12:44 PM.
<?php
ini_set('display_errors', true);
file_put_contents('./post.content', print_r(json_decode(file_get_contents('php://input')), true));
/**
* This is your phabricator's access data. The URL is the base URL of the phabricator install,
* and the token if the conduit token you generated for this script
*
* @see http://yourphabricator.com/settings/user/{username}/page/apitokens/
*/
$phabricator = [
'url' => 'url to your phabricator',
'token' => "conduit-api-token"
];
$packagist = [
'username' => 'packagist username',
'token' => 'packagist-api-token'
];
/*
* Get the payload from your phabricator webhook
*/
$payload = json_decode(file_get_contents('php://input'));
$commit = $payload->object->phid;
/*
* We now need to query the server to find the commit that originated the
* webhook so we know which repository originated the call.
*/
$ch = curl_init(sprintf('%s/api/phid.lookup', $phabricator['server']));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'api.token' => $phabricator['token'],
'names' => [$commit]
]));
$meta = json_decode(curl_exec($ch));
/*
* Extract the repository URL so we can pass that to our Packagist.
*
* This URL is not a perfect match to what Packagist will have, since phabricator seems
* to output the repo URL as phab.com/source/package/ and then use phab.com/source/package.git
* but it seems packagist is lenient enough to accept it anyway.
*
* Should this ever change, we can always process this on our end.
*/
$repoid = explode(':', $meta->result->{$commit}->name)[0];
$ch = curl_init(sprintf('%s/api/phid.lookup', $phabricator['server']));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'api.token' => $phabricator['token'],
'names' => [$repoid]
]));
$repo = json_decode(curl_exec($ch));
$url = $repo->result->{$repoid}->uri;
/*
* Send an update to packagist.
*/
$ch = curl_init(sprintf('https://packagist.org/api/update-package?username=%s&apiToken=%s', $packagist['user'], $packagist['token']));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type:application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'repository' => [
'url' => $url
]
]));
curl_exec($ch);

Event Timeline

cesar created this object with visibility "Public (No Login Required)".