Skip to content

Instantly share code, notes, and snippets.

@christianc1
Created July 7, 2017 01:27
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 christianc1/a87671a7cdf136597e8b52545c526899 to your computer and use it in GitHub Desktop.
Save christianc1/a87671a7cdf136597e8b52545c526899 to your computer and use it in GitHub Desktop.
Check Github Statuses of Composer Packages
...
test:
override:
- $(which php) package-statuses.php
...
<?php
$github_token = getenv( 'GITHUB_STATUS_TOKEN' );
/**
* Print to Stdout
*
* @param string $string
* @return void
*/
function stdout( string $string ) {
fwrite( STDOUT, $string . PHP_EOL );
}
/**
* Get Github Endpoint
*
* @param string $source The repository source, ex ( liftux/playbook-core ).
* @param string $commit The commit sha hash.
* @param string $token The Github user token with status scope to use with the endpoint.
* @return string The endpoint url.
*/
function github_endpoint( string $source, string $commit, string $token ) {
return 'https://api.github.com/repos/'
. rtrim ( ltrim( $source, '/' ), '/' )
. '/commits/'
. $commit
. '/status'
. '?access_token=' . $token;
}
/**
* Get Status
*
* @param string $endpoint The endpoint to GET.
* @return string The result of a curl call.
*/
function get_status( string $endpoint ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_URL, $endpoint );
curl_setopt( $ch, CURLOPT_USERAGENT, 'christianc1' );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
/**
* Get Packages
*
* @return array An array of package objects.
*/
function get_packages() : array {
$lock = json_decode( file_get_contents( __DIR__ . '/wordpress/composer.lock' ) );
return is_array( $lock->packages ) ? $lock->packages : [];
}
/**
* Check statuses
*
* @param array $packages An array of package objects.
* @param string $token The github user token with status scope to use for each package.
* @return void
*/
function check_statuses( array $packages, string $token ) {
foreach ( $packages as $package ) {
$status = 'Skipped';
if ( strpos( $package->source->url, 'github.com' ) !== false ) {
$org = str_replace( [ 'https://github.com/', 'git@github.com:', '.git' ], '', $package->source->url );
$raw = get_status( github_endpoint( $org, $package->source->reference, $token ) );
$json = json_decode( $raw );
$status = ucfirst( $json->state );
}
stdout( 'Status of ' . $package->source->url . '@' . $package->source->reference . ': ' . $status );
if ( 'Failure' === $status ) {
stdout( '/!\------------ Aborting ------------/!\\' );
//exit( 1 );
}
}
}
check_statuses( get_packages(), $github_token );
exit( 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment