-
-
Save FrankM1/6ddf303f22411c95cfae118b9bf8cdc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('TOKEN_SECRET', 'plugin_token' ); | |
define('PLUGIN_SLUG', 'plugin_slug' ); | |
EdwinDeployer::parse(); | |
class EdwinDeployer { | |
public static $json = false; | |
public static $repo = ''; | |
private static $version = ''; | |
private static $base_dir = ''; | |
private static $main_dir = ''; | |
private static $git_dir = ''; | |
private static $svn_dir = ''; | |
public static function create_test() { | |
} | |
public static function parse() { | |
$result = self::secure_call(); | |
if ( !$result ) { | |
http_response_code(401); | |
echo 'Bad Secret'; | |
return; | |
} | |
$result = self::get_payload(); | |
if ( !$result ) { | |
http_response_code(400); | |
echo 'No Payload'; | |
return; | |
} | |
$result = self::get_git_repo(); | |
if ( !$result ) { | |
http_response_code(400); | |
echo 'No Repository URI'; | |
return; | |
} | |
if ( !isset( $_REQUEST['plugin'] ) ) { | |
http_response_code(400); | |
echo 'No WP.org slug provided'; | |
return; | |
} | |
if ( 'refs/heads/assets' == self::$json['ref'] ) { | |
echo 'do assets update'; | |
self::git_prepare(); | |
self::git_assets(); | |
self::svn_push( 'Assets updated' ); | |
self::git_remove(); | |
} else if ( 0 === strpos( self::$json['ref'], 'refs/tags/' ) && 'refs/heads/master' === self::$json['base_ref'] ) { | |
self::$version = preg_replace( '#^refs/tags/#ims', '', self::$json['ref'] ); | |
echo 'do version '.self::$version.' update'; | |
self::git_prepare(); | |
self::git_assets(); | |
self::git_version(); | |
self::svn_push( self::$version.': '.self::$json['head_commit']['message'] ); | |
self::git_remove(); | |
} else if ( isset( self::$json['commits'] ) ) { | |
$readme = false; | |
foreach ( self::$json['commits'] as $commit ) { | |
if ( in_array( 'readme.txt', $commit['modified'] ) || in_array( 'readme.txt', $commit['added'] ) ) { | |
$readme = true; | |
break; | |
} | |
} | |
if ( $readme ) { | |
echo 'do readme update'; | |
self::git_prepare(); | |
self::git_readme(); | |
self::svn_push( 'Readme.txt updated: '.self::$json['head_commit']['message'] ); | |
self::git_remove(); | |
} | |
} | |
} | |
private static function git_prepare() { | |
self::$base_dir = dirname( __FILE__ ); | |
self::$main_dir = self::$base_dir . '/temp'. '/' . self::$json['repository']['full_name']; | |
if ( !file_exists( self::$main_dir ) ) { | |
mkdir( self::$main_dir, 0777, true ); | |
} | |
chdir( self::$main_dir ); | |
exec( "git clone ".self::$repo ); | |
self::$git_dir = self::$main_dir . '/' . self::$json['repository']['name']; | |
chdir( self::$git_dir ); | |
self::$svn_dir = self::$main_dir . '/svn'; | |
$slug = PLUGIN_SLUG; | |
exec( "svn co http://plugins.svn.wordpress.org/{$slug}/ ".self::$svn_dir ); | |
} | |
private static function git_remove() { | |
chdir( self::$base_dir ); | |
exec( 'rm -rf '.self::$main_dir ); | |
} | |
private static function svn_push( $message ) { | |
chdir( self::$svn_dir ); | |
exec( 'svn stat | awk \'/^\?/ {print $2}\' | xargs svn add > /dev/null 2>&1' ); | |
exec( 'svn stat | awk \'/^\!/ {print $2}\' | xargs svn rm --force' ); | |
exec( 'svn stat'); | |
exec( 'svn cleanup' ); | |
exec( 'svn ci . -m "'.$message.'. Published via deployer.edwinapp.com" --username deployer --password \''.DEPLOYER_PASSWORD.'\' --no-auth-cache --non-interactive' ); | |
} | |
private static function git_assets() { | |
exec( 'git checkout assets' ); | |
if ( !file_exists(self::$svn_dir."/assets") ) { | |
mkdir( self::$svn_dir."/assets", 0777, true ); | |
} | |
exec( "git archive --format=tar assets | tar -C ".self::$svn_dir."/assets -xf -", $a, $b ); | |
} | |
private static function get_version($url) { | |
// ini_set('user_agent',''); | |
$json = self::get_file($url); | |
$json = json_decode( $json, true ); | |
if ( !isset( $json[0] ) ) { | |
return false; | |
} | |
$json = $json[0]['name']; | |
return $json; | |
} | |
private static function git_readme() { | |
$version = self::get_version(self::$json['repository']['tags_url']); | |
if ( false === $version ) { | |
return false; | |
} | |
exec( 'git checkout master' ); | |
exec( "git archive --format=tar master ./readme.txt | tar -C ".self::$svn_dir."/trunk -xf -" ); | |
exec( "git archive --format=tar master ./readme.txt | tar -C ".self::$svn_dir."/tags/".$version."/ -xf -" ); | |
} | |
private static function git_version() { | |
exec( 'git checkout master' ); | |
if ( !file_exists( self::$svn_dir . "/tags/" . self::$version ) ) { | |
mkdir( self::$svn_dir . "/tags/" . self::$version, 0777, true ); | |
} | |
exec( "git archive --format=tar ".self::$version." | tar -C ".self::$svn_dir."/tags/".self::$version." -xf -", $a, $b ); | |
exec( "rm -rf ".self::$svn_dir."/trunk/*" ); | |
exec( "git archive --format=tar ".self::$version." ./readme.txt | tar -C ".self::$svn_dir."/trunk -xf -" ); | |
} | |
private static function get_git_repo() { | |
if ( ! isset( self::$json['repository'] ) ) { | |
return false; | |
} | |
if ( ! isset( self::$json['repository']['clone_url'] ) ) { | |
return false; | |
} | |
self::$repo = self::$json['repository']['clone_url']; | |
return true; | |
} | |
private static function secure_call() { | |
self::$json = file_get_contents('php://input'); | |
$hubSignature = $_SERVER['HTTP_X_HUB_SIGNATURE']; | |
// Split signature into algorithm and hash | |
list($algo, $hash) = explode('=', $hubSignature, 2); | |
// Calculate hash based on payload and the secret | |
$payloadHash = hash_hmac($algo, self::$json, TOKEN_SECRET); | |
// Check if hashes are equivalent | |
if ($hash !== $payloadHash) { | |
return false; | |
} | |
return true; | |
} | |
private static function get_payload() { | |
if ( !self::$json ) { | |
return false; | |
} | |
self::$json = json_decode( self::$json, true ); | |
return true; | |
} | |
private static function get_file( $url ) { | |
$curl = curl_init(); | |
// Set some options - we are passing in a useragent too here | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url, | |
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0)' | |
)); | |
// Send the request & save response to $resp | |
$resp = curl_exec($curl); | |
// Close request to clear up some resources | |
curl_close($curl); | |
return $resp; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment