Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions github_api.module
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,136 @@ function github_api_delete_branch($username = NULL, $repository = NULL, $source_
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}

/**
* Callback to fetch a repository's latest release.
*
* @param string $username
* The GitHub username of the repo owner.
*
* @param string $repository
* The name of the repository.
*
* @throws Exception
*/
function github_api_get_latest_release($username = NULL, $repository = NULL) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}

$client = github_api_client();

try {
$client->api('current_user')->show();
$release = $client->api('repo')->releases()->latest($username, $repository);

if (!(is_array($release))) {
throw new Exception('Fetch latest release failed.');
}

return $release;
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}

/**
* Callback to fetch a binary asset by ID and save it as a managed file.
*
* @param string $username
* The GitHub username of the repo owner.
*
* @param string $repository
* The name of the repository.
*
* @param int $asset_id
* The asset's ID.
*
* @param string $uri
* The URI to which to save the file. (e.g. 'public://filename.zip')
*
* @return object $file
* Drupal file object
*
* @throws Exception
*/
function github_api_fetch_asset($username = NULL, $repository, $asset_id, $uri) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}

$client = github_api_client();

try {
$url = url(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acrollet It's great to see you adding more functionality to the library.

The idea of this module is that it is a lightweight wrapper around [https://github.com/KnpLabs/php-github-api](KnP Lab's github library). They have implemented support for release assets. The composer.json should be updated to so the dependency is ^1.5.1 and then instead of using drupal_http_request() to make this call it should use the library.

I haven't run coder-review over the patch, but other than the issue above I don't see any other issues with the patch. If you fix it and squash the commits it should be an easy merge.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skwashd their support does not actually fetch a binary asset (from a private repo) as this code does, the get() method simply returns an object describing a github asset. I looked into adding a fetch method to the library, but it would be a non-trivial addition. c.f. KnpLabs/php-github-api#320

'https://api.github.com/repos/' . $username . '/' . $repository . '/releases/assets/' . $asset_id,
array('query' => array('access_token' => variable_get('github_api_token', '')))
);

// Retrieve file with curl. (To avoid memory limit issues)
set_time_limit(300);
$tempfile = drupal_tempnam(file_directory_temp(), 'rdp');
$fp = fopen($tempfile, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/octet-stream'));
curl_setopt($ch,CURLOPT_USERAGENT,'Drupal (+http://drupal.org/)');
curl_exec($ch);
curl_close($ch);
fclose($fp);

// Create the managed file.
$fp = fopen($tempfile, 'r');
$file = file_save_data($fp, $uri);
return $file;
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}

/**
* Callback to fetch a file by URL from a private repo and save it as a managed file.
*
* @param string $username
* The GitHub username of the repo owner.
*
* @param int $url
* The asset's ID.
*
* @param string $uri
* The URI to which to save the file. (e.g. 'public://filename.zip')
*
* @return object $file
* Drupal file object
*
* @throws Exception
*/
function github_api_fetch_private_file($username = NULL, $url, $uri) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}

$client = github_api_client();

try {
$remote_url = url(
$url,
array('query' => array('access_token' => variable_get('github_api_token', '')))
);

$options = array('timeout' => 300);
$asset = drupal_http_request($remote_url, $options);

if (!empty($asset->data)) {
$file = file_save_data($asset->data, $uri);
}
return $file;
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}