Created
July 19, 2016 22:21
-
-
Save aidancbrady/5b2fe3c98b894ad2f9dc1aac48f5da3e to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Plugin Name: Jenkins Wordpress | |
* Description: Jenkins interface for Wordpress | |
* Version: 0.2 | |
* Author: Galen Han | |
* Author URI: http://galenhan.com | |
*/ | |
$host = "http://ci.indiewikis.com"; | |
$webUrl = "http://aidancbrady.com"; | |
$username = "aidancbrady"; | |
$token = "970c5fdaf1a5a9a374e183754cfc11b3"; | |
add_shortcode( 'jenkins', 'list_jenkins' ); | |
add_shortcode( 'jenkins_legacy', 'list_jenkins_legacy' ); | |
add_action( 'wp_enqueue_scripts', 'add_scripts' ); | |
function list_jenkins($atts) { | |
global $webUrl; | |
$project_atts = shortcode_atts(array('project' => 'default'), $atts); | |
$fileUrl = "/home/aidan/public_html/wp-content/uploads/" . $project_atts['project'] . "/*/*/*"; | |
foreach (glob($fileUrl) as $file) { | |
$pieces = explode('/', $file); | |
$buildNo = $pieces[8]; | |
$relativePath = implode('/', array_slice($pieces,-5, 5)); | |
$fileName = end($pieces); | |
$fileName = explode('.', $fileName); | |
array_pop($fileName); | |
$fileName = implode('.', $fileName); | |
$version = end(explode('-', $fileName)); | |
//Check if build is recommended | |
if(strpos($buildNo, '-recommended') !== false) { | |
$listBuilds[$buildNo]['recommended'] = true; | |
} else { | |
$listBuilds[$buildNo]['recommended'] = false; | |
} | |
$listBuilds[$buildNo]['version'] = $version; | |
$listBuilds[$buildNo]['mc_version'] = $pieces[7]; | |
$listBuilds[$buildNo]['artifacts'][] = array($fileName, $relativePath); | |
} | |
krsort($listBuilds); | |
$output_recommended = ""; | |
foreach ($listBuilds as $key => $v) | |
{ | |
if($v['recommended'] === false) continue; //Skip recommended builds | |
foreach ($v['artifacts'] as $k => $g) | |
{ | |
$output_recommended .= '<a class="button" href="'; | |
$output_recommended .= ad_link($webUrl . '/' . $g[1]); | |
$output_recommended .='">' . $g[0] . '</a>'; | |
} | |
break; | |
} | |
$output_artifacts = ""; | |
$buildCount = 0; | |
foreach ($listBuilds as $key => $v) | |
{ | |
$output_artifacts .= "<tr "; | |
if($v['recommended'] === true) $output .= 'class="success"'; | |
$output_artifacts .= "><td>{$v['version']} for {$v['mc_version']}</td>"; | |
foreach ($v['artifacts'] as $k => $g) | |
{ | |
$url = ad_link($webUrl . '/' . $g[1]); | |
$name = explode('-', $g[0])[0]; | |
$output_artifacts .= "<td><a href='$url'>$name</a></td>"; | |
} | |
$output_artifacts .= "</tr>"; | |
$buildCount++; | |
} | |
if($buildCount === 0) { | |
$output_artifacts = "<tr><td colspan='6'>No previous versions</td>"; | |
} | |
$html = <<<EOD | |
<h3>Recommended downloads</h3> | |
<div class="downloads"> | |
$output_recommended | |
</div> | |
<h4 style="margin-top: 20px">Previous versions</h4> | |
<div class="previous-versions"> | |
<table> | |
<thead> | |
<th>Release</th> | |
<th colspan="5" style="text-align: center">Downloads</th> | |
</thead> | |
<tbody> | |
$output_artifacts | |
</tbody> | |
</table> | |
</div> | |
<div class="alert" style="margin-bottom: 20px"> | |
For legacy downloads, please visit the <a href="/{$project_atts['project']}/download/legacy">legacy downloads page</a>. | |
</div> | |
EOD; | |
return $html; | |
} | |
function list_jenkins_legacy( $atts ){ | |
global $token, $recommended_builds; | |
$project_atts = shortcode_atts(array('project' => 'default'), $atts); | |
$json = fetch_json("/job/" . $project_atts['project'] . "/api/json?depth=1&tree=allBuilds[*[*[*]]]"); | |
$builds = parse_builds($json); | |
$recent = array_slice($builds, 0, 4); //Get only 4 most recent builds; | |
//Fetch list of recommended builds | |
$recommended = fetch_json("/job/" . $project_atts['project'] . "/promotion/process/Recommended/api/json?depth=2"); | |
$recommended_builds = array(); | |
foreach ($recommended['builds'] as $value) { | |
$recommended_builds[] = $value['target']['number']; | |
} | |
//Filter out recommended builds | |
$legacy = array(); | |
foreach ($builds as $value) { | |
if(in_array($value['number'], $recommended_builds)) $legacy[] = $value; | |
} | |
$newest_recommended = $recommended['builds'][0]['target']; | |
$output_recommended = ""; | |
foreach ($newest_recommended['artifacts'] as $value) { | |
if($value['fileName'] == "build.properties") continue; | |
$output_recommended .= '<a class="button" href="'; | |
$output_recommended .= ad_link($newest_recommended['url'] . 'artifact/' . $value['relativePath']); | |
$output_recommended .='">' . $value['fileName'] . '</a>'; | |
} | |
$output_artifacts = print_artifacts($recent); | |
$changelog = build_changelog($builds); | |
$output_legacy = print_artifacts($legacy); | |
$html = <<<EOD | |
<h3>Legacy downloads</h3> | |
<button class="button" id="btn-legacy">Show legacy downloads</button> | |
<div id="legacy" style="display:none"> | |
<p>List of all recommended builds.</p> | |
<table> | |
<thead> | |
<th>Release</th> | |
<th colspan="5" style="text-align: center">Downloads</th> | |
</thead> | |
<tbody> | |
$output_legacy | |
</tbody> | |
</table> | |
</div> | |
<h3 style="margin-top: 30px">Changelog</h3> | |
<dl class="changelog"> | |
$changelog | |
</dl> | |
EOD; | |
return $html; | |
} | |
function parse_builds($json, $debug = false) { | |
$files = array(); | |
$i = 0; | |
foreach ($json['allBuilds'] as $value) { | |
if($value['result'] != "SUCCESS") continue; | |
$files[$i]['number'] = $value['number']; | |
$files[$i]['name'] = $value['fullDisplayName']; | |
$version = explode("-", $value['artifacts'][1]['fileName']); | |
if(count($version) == 3) { | |
$files[$i]['mc_version'] = $version[1]; | |
} | |
$version = end($version); | |
$files[$i]['version'] = substr($version , 0, -4); | |
$files[$i]['url'] = $value['url']; | |
$files[$i]['files'][] = $value['artifacts']; | |
/* | |
echo "<pre>"; | |
var_dump($value['changeSet']); | |
echo "</pre>"; | |
*/ | |
$files[$i]['change'] = array(); | |
foreach($value['changeSet']['items'] as $commit) { | |
$files[$i]['change'][] = $commit['msg']; | |
} | |
$i++; | |
} | |
return $files; | |
} | |
function build_changelog($json) { | |
$changelog = ""; | |
foreach ($json as $build) { | |
$changes = ""; | |
foreach ($build['change'] as $change) { | |
$changes .= "<li>$change</li>"; | |
} | |
$changelog .= "<dt>" . $build['version'] . ":</dt><dd><ul>" . $changes . "</ul></dd>"; | |
} | |
return $changelog; | |
} | |
function print_artifacts($json) { | |
global $recommended_builds; | |
$output = ""; | |
foreach ($json as $build) { | |
$output_2 = ""; | |
//Format rows | |
foreach ($build['files'][0] as $value) { | |
$name = explode("-", $value['fileName']); | |
if($name[0] == "build.properties") continue; | |
$output_2 .= '<td><a href="' . ad_link($build['url'] . 'artifact/' . $value['relativePath']) . '">' . $name[0] . '</a></td>'; | |
} | |
$output .= '<tr '; | |
if(in_array($build['number'], $recommended_builds)) $output .= 'class="success"'; | |
if(isset($build['mc_version'])) { | |
$version = "{$build['version']} for {$build['mc_version']}"; | |
} else { | |
$version = "{$build['version']}"; | |
} | |
$output .= "><td>$version</td>$output_2</tr>"; | |
} | |
return $output; | |
} | |
function fetch_json($url) { | |
global $host, $username, $token; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $host . $url); | |
curl_setopt($ch, CURLOPT_PORT, '8080'); | |
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$return = curl_exec($ch); | |
$json = json_decode($return, true); | |
curl_close($ch); | |
return $json; | |
} | |
function ad_link($url) { | |
return "http://adf.ly/1140958/dl.aidancbrady.com/download?u=" . urlencode($url) . "&wiki=" . urlencode("http://wiki.aidancbrady.com"); | |
} | |
function add_scripts() { | |
wp_enqueue_script( | |
'jenkins-wordpress-script', | |
plugin_dir_url( __FILE__ ) . 'js/main.js', | |
array( 'jquery' ), | |
"1.0.3", | |
true | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment