Skip to content

Instantly share code, notes, and snippets.

@xrstf
Created February 28, 2013 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrstf/5053982 to your computer and use it in GitHub Desktop.
Save xrstf/5053982 to your computer and use it in GitHub Desktop.
This script can be used with Bitbucket's POST service to update packages on packagist.org.
<?php
/*
* Dieses Script dient als "Proxy" zwischen Bitbucket und Packagist. Es empfängt
* einen POST-Request über den generischen POST-Service von Bitbucket und leitet
* diesen als PUT-Request an Packagist weiter, um das dazugehörige Paket zu
* aktualisieren.
*/
function clean($var) {
return get_magic_quotes_gpc() ? stripslashes($var) : $var;
}
$apiTokens = array(
'xrstf' => '....'
);
error_reporting(0);
ini_set('display_errors', 'off');
////////////////////////////////////////////////////////////////////////////////
// check request
if (empty($_POST['payload']) || !is_string($_POST['payload'])) {
header('HTTP/1.0 400 Bad Request');
die('bad request');
}
if (empty($_GET['username']) || !is_string($_GET['username']) || !isset($apiTokens[$_GET['username']])) {
header('HTTP/1.0 400 Bad Request');
die('bad request');
}
////////////////////////////////////////////////////////////////////////////////
// determine repository from Bitbucket payload
$username = clean($_GET['username']);
$payload = json_decode(clean($_POST['payload']));
$repo = trim($payload->repository->absolute_url, '/');
////////////////////////////////////////////////////////////////////////////////
// deduct package name from repository's composer.json
require 'develop/guzzle.phar';
$client = new Guzzle\Http\Client('https://bitbucket.org/');
$response = $client
// cannot get the cert chain to work, and using HTTP yields a redirect to HTTPS anyway
->setSslVerification(false, false, 0)
->get($repo.'/raw/tip/composer.json')
->send()
;
if ($response->getStatusCode() !== 200) {
header('HTTP/1.0 404 Not Found');
die('could not find composer.json');
}
$composer = json_decode($response->getBody(true));
$package = $composer->name;
////////////////////////////////////////////////////////////////////////////////
// update package on packagist
$client = new Guzzle\Http\Client('https://packagist.org/');
$data = http_build_query(array(
'username' => $username,
'apiToken' => $apiTokens[$username],
'autoUpdated' => 1,
'update' => 1
), '', '&');
$client
->setSslVerification('develop/packagist.pem', true, 2)
->put('packages/'.$package.'?'.$data)
->send()
;
print "done.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment