From 9b6eae395e5596333094e9da598ba2905dc8d9f3 Mon Sep 17 00:00:00 2001 From: Adrian Rollett Date: Thu, 28 Jan 2016 16:06:34 -0700 Subject: [PATCH 1/4] Add callback to fetch latest release for a repo Fix release check Add callback to fetch release asset by ID and store as a managed file. Document how to avoid storing GH password Revert changes to README --- github_api.module | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/github_api.module b/github_api.module index 5f4c0f1..7dbcc5e 100644 --- a/github_api.module +++ b/github_api.module @@ -325,3 +325,87 @@ 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/' . $repository . '/' . $username . '/releases/assets/' . $asset_id, + array('query' => array('access_token' => variable_get('github_api_token', ''))) + ); + + $options = array( + 'headers' => array( + 'Accept' => 'application/octet-stream', + ), + 'timeout' => 300, + ); + $asset = drupal_http_request($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); + } +} From 247f9c5fdf70fab35159bab8422ac859169ce69b Mon Sep 17 00:00:00 2001 From: Adrian Rollett Date: Tue, 23 Feb 2016 17:09:19 -0700 Subject: [PATCH 2/4] Add callback to fetch a file by URL from a private repo --- github_api.module | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/github_api.module b/github_api.module index 7dbcc5e..8fff097 100644 --- a/github_api.module +++ b/github_api.module @@ -409,3 +409,46 @@ function github_api_fetch_asset($username = NULL, $repository, $asset_id, $uri) 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); + } +} From b8965df091436dafb34149b2ac5cce61249d5b71 Mon Sep 17 00:00:00 2001 From: Adrian Rollett Date: Thu, 25 Feb 2016 12:42:07 -0700 Subject: [PATCH 3/4] Correct asset ID URI --- github_api.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github_api.module b/github_api.module index 8fff097..11f4d16 100644 --- a/github_api.module +++ b/github_api.module @@ -388,7 +388,7 @@ function github_api_fetch_asset($username = NULL, $repository, $asset_id, $uri) try { $url = url( - 'https://api.github.com/repos/' . $repository . '/' . $username . '/releases/assets/' . $asset_id, + 'https://api.github.com/repos/' . $username . '/' . $repository . '/releases/assets/' . $asset_id, array('query' => array('access_token' => variable_get('github_api_token', ''))) ); From ab89da845efc25437d3585b4bd2936786ffb063c Mon Sep 17 00:00:00 2001 From: Adrian Rollett Date: Mon, 28 Mar 2016 11:20:48 -0600 Subject: [PATCH 4/4] Use curl to fetch remote files --- github_api.module | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/github_api.module b/github_api.module index 11f4d16..92666bd 100644 --- a/github_api.module +++ b/github_api.module @@ -392,17 +392,23 @@ function github_api_fetch_asset($username = NULL, $repository, $asset_id, $uri) array('query' => array('access_token' => variable_get('github_api_token', ''))) ); - $options = array( - 'headers' => array( - 'Accept' => 'application/octet-stream', - ), - 'timeout' => 300, - ); - $asset = drupal_http_request($url, $options); - - if (!empty($asset->data)) { - $file = file_save_data($asset->data, $uri); - } + // 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) {