diff --git a/github_api.module b/github_api.module index 5f4c0f1..92666bd 100644 --- a/github_api.module +++ b/github_api.module @@ -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( + '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); + } +}