Github

https://github.com/f13dev/wp-github-oembed

Git clone:

git clone https://github.com/f13dev/wp-github-oembed

Introduction

As a user of Github who is maintaining a website about software development (this one), I quite like the wp-github-oembed plugin by leewillis77. Although a very good contribution, I felt that it was missing an important piece of information regarding Github repositories; no information is provided about releases.

Code modifications

In order to add release information two additional methods were required within the github_api class, first of all one to retrieve releases. If no releases exist for the repository null is returned, otherwise an array of release information is returned.

/**
 * Get release information for repository, used to determine if any releases exist
 * @param  string $owner      The repository's owner
 * @param  string $repository The respository name
 * @return object             The response from the GitHub API
 */
public function get_repo_releases( $owner, $repository ) {

	$this->log( "get_repo_releases( $owner, $repository )", GEDEBUG_CALL );

	$owner = trim( $owner, '/' );
	$repo = trim( $repository, '/' );

	$results = $this->call_api( "https://api.github.com/repos/$owner/$repo/releases" );

	return json_decode( $results['body'] );

}

Secondly, a method within github_api was required to retrieve an array of information about the latest release, this returns two pieces of information used within this contribution: html_url (the URL of the latest release) and tag_name (the releases tag, i.e. v1.0).

/**
 * Get latest release information for repository.
 * @param  string $owner      The repository's owner
 * @param  string $repository The respository name
 * @return object             The response from the GitHub API
 */
public function get_repo_releases_latest( $owner, $repository ) {

	$this->log( "get_repo_releases_latest( $owner, $repository )", GEDEBUG_CALL );

	$owner = trim( $owner, '/' );
	$repo = trim( $repository, '/' );

	$results = $this->call_api( "https://api.github.com/repos/$owner/$repository/releases/latest" );

	return json_decode( $results['body'] );

}

Finally, in order to utilise these two methods, the following additional code needs to be added to the oembed_github_repo function in github-embed.php.

$releases = $this->api->get_repo_releases ( $owner, $repository );
/*
 * Show the latest release if one exists, otherwise show Latest release: none.
 */
if ( $releases == null )
{
	$response->html .= "Latest release: none<br />";
}
else
{
	$latest_release = $this->api->get_repo_releases_latest ( $owner, $repository );
	$latest_release_url = $latest_release->html_url;
	$latest_release_tag = $latest_release->tag_name;
	$response->html .= 'Latest release: <a href="' . $latest_release_url . '" target="_blank">' . 
		$latest_release_tag . '</a><br />';
}

Now when repository information is requested, the response will include a link to the latest release, if one exists; otherwise “Latest release: none” will be displayed

Screen shot

Github Embed, showing one release tagged "v0.1-beta".

Github Embed, showing one release tagged “v0.1-beta”.


5 tags for "WP Github Embed, adding release information"

No comments on WP Github Embed, adding release information

Share your thoughts

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.