Skip to content

Instantly share code, notes, and snippets.

@chipbennett
Created July 23, 2011 03:52
Show Gist options
  • Save chipbennett/1100987 to your computer and use it in GitHub Desktop.
Save chipbennett/1100987 to your computer and use it in GitHub Desktop.
Function to return tabulated GitHub API commits and issues data for use in WordPress Theme settings pages
/**
* Get GitHub API Data
*
* Uses the GitHub API (v3) to get information
* regarding open or closed issues (bug reports)
* or commits, then outputs them in a table.
*
* Derived from code originally developed by
* Michael Fields (@_mfields):
* @link https://gist.github.com/1061846 Simple Github commit API shortcode for WordPress
*
* @param string $context (required) API data context. Currently supports 'commits' and 'issues'. Default: 'commits'
* @param string $status (optional) Issue state, either 'open' or 'closed'. Only used for 'commits' context. Default: 'open'
* @param string $releasedate (optional) Date, in YYYY-MM-DD format, used to return commits/issues since last release.
* @param string $user (optional) GitHub user who owns repository.
* @param string $repo (optional) GitHub repository for which to return API data
*
* @return string table of formatted API data
*/
function oenology_get_github_api_data( $context = 'commits', $status = 'open', $releasedate = '2011-06-21', $user = 'chipbennett', $repo = 'oenology' ) {
$capability = 'read';
// $branch is user/repository string.
// Used variously throughout the function
$branch = $user . '/' . $repo;
// Create transient key string. Used to ensure API data are
// pinged only periodically. Different transient keys are
// created for commits, open issues, and closed issues.
$transient_key = 'gh_';
if ( 'commits' == $context ) {
$transient_key .= 'commits' . md5( $branch );
} elseif ( 'issues' == $context ) {
$transient_key .= 'issues_' . $status . md5( $branch );
}
// If cached (transient) data are used, output an HTML
// comment indicating such
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached .= "\n" . '<!--Returned from transient cache.-->';
}
// Construct the API request URL, based on $branch and
// $context, and for issues, $status
$apiurl = 'https://api.github.com/repos/' . $branch . '/' . $context;
if ( 'commits' == $context ) {
$apiurl .= '';;
} elseif ( 'issues' == $context ) {
$apiurl .= '?state=' . $status;
}
// Request the API data, using the constructed URL
$remote = wp_remote_get( esc_url( $apiurl ) );
// If the API data request results in an error, return
// an appropriate comment
if ( is_wp_error( $remote ) ) {
if ( current_user_can( $capability ) ) {
return '<p>Github API: Github is unavailable.</p>';
}
return;
}
// If the API returns a server error in response, output
// an error message indicating the server response.
if ( '200' != $remote['response']['code'] ) {
if ( current_user_can( $capability ) ) {
return '<p>Github API: Github responded with an HTTP status code of ' . esc_html( $remote['response']['code'] ) . '.</p>';
}
return;
}
// If the API returns a valid response, the data will be
// json-encoded; so decode it.
$data = json_decode( $remote['body'] );
// If the decoded json data is null, return a message
// indicating that no data were returned.
if ( ! isset( $data ) || empty( $data ) ) {
$apidata = $context;
if ( 'issues' == $context ) {
$apidata = $status . ' ' . $context;
}
if ( current_user_can( $capability ) ) {
return '<p>No ' . $apidata . ' could be found.</p>';
return '<p>Github API: No ' . $apidata . ' could be found for this repository.</p>';
}
return;
}
// If the decoded json data has content, prepare the data
// to be output.
if ( 'issues' == $context ) {
// $reportdate is used as a table column header
$reportdate = ( 'open' == $status ? 'Reported' : 'Closed' );
// $reportobject is used to return the appropriate timestamp
$reportobject = ( 'open' == $status ? 'created_at' : 'closed_at' );
} elseif ( 'commits' == $context ) {
// $reportdate is used as a table column header
$reportdate = 'Date';
}
// $reportidlabel is used as a table column header
$reportidlabel = ( 'issues' == $context ? '#' : 'Commit' );
// $datelastrelease is the PHP date of last released, based
// on the $releasedate parameter passed to the function
$datelastrelease = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $releasedate ) ), 'U' );
// Begin constructing the table
$output = '';
$output .= "\n" . '<table class="github-api github-issues">';
$output .= "\n" . '<thead>';
$output .= "\n\t" . '<tr><th>' . $reportidlabel . '</th><th>' . $reportdate . '</th><th>Issue</th>';
if ( 'issues' == $context ) {
$output .= '<th>Label</th>';
}
$output .= '</tr>';
$output .= "\n" . '</thead>';
$output .= "\n" . '<tbody>';
// Step through each object in the $data array
foreach( $data as $object ) {
if ( 'issues' == $context ) {
$url = 'https://github.com/' . $branch . '/' . $context .'/' . $object->number;
$reportid = $object->number;
$message = $object->title;
$label = $object->labels;
$label = $label[0];
$labelname = $label->name;
$labelcolor = $label->color;
$objecttime = $object->$reportobject;
} else if ( 'commits' == $context ) {
$url = $object->url;
$reportid = substr( $object->sha, 0, 6 );
$commit = $object->commit;
$message = $commit->message;
$author = $commit->author;
$objecttime = $author->date;
}
$time = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $objecttime ) ), 'U' );
$timestamp = date( 'dMy', $time );
$time_human = 'About ' . human_time_diff( $time, get_date_from_gmt( date( 'Y-m-d H:i:s' ), 'U' ) ) . ' ago';
$time_machine = date( 'Y-m-d\TH:i:s\Z', $time );
$time_title_attr = date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $time );
// Only output $data reported/created/closed since
// the last release
if ( $time > $datelastrelease ) {
$output .= "\n\t" . '<tr>';
$output .= '<td style="padding:3px 5px;text-align:center;font-weight:bold;"><a href="' . esc_url( $url ) . '">' . $reportid . '</a></td>';
$output .= '<td style="padding:3px 5px;text-align:center;color:#999;font-size:12px;"><time title="' . esc_attr( $time_title_attr ) . '" datetime="' . esc_attr( $time_machine ) . '">' . esc_html( $timestamp ) . '</time></td>';
$output .= '<td style="padding:3px 5px;font-size:12px;">' . esc_html( $message ) . '</td>';
if ( 'issues' == $context ) {
$output .= '<td style="padding-left:5px;text-align:center;"><div style="text-shadow:#555 1px 1px 0px;border:1px solid #bbb;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;padding:3px;padding-bottom:5px;padding-top:1px;font-weight:bold;background-color:#ffffff;color:#' . $labelcolor . ';">' . $labelname . '</div></td>';
}
$output .= '</tr>';
}
}
// Complete construction of the table
$output .= "\n" . '</tbody>';
$output .= "\n" . '</table>';
// Set the transient (cache) for the API data
set_transient( $transient_key, $output, 600 );
// Return the output
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment