From e4ccc5e6eac2bfebb8770a1a8f73f965ef0146fc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 13 Jun 2013 13:05:33 +0200 Subject: [ticket/11603] Fix github API calls - Some URLs changed - Response is a plain array now - Added error messages when API limit is reached PHPBB3-11603 --- git-tools/merge.php | 11 +++++-- git-tools/setup_github_network.php | 60 ++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 14 deletions(-) (limited to 'git-tools') diff --git a/git-tools/merge.php b/git-tools/merge.php index 2acd2280b9..5eb48a53f8 100755 --- a/git-tools/merge.php +++ b/git-tools/merge.php @@ -128,6 +128,7 @@ function api_request($query) curl_setopt($c, CURLOPT_URL, "https://api.github.com/$query"); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_USERAGENT, 'phpBB/1.0'); + curl_setopt($c, CURLOPT_HEADER, true); $contents = curl_exec($c); curl_close($c); @@ -135,13 +136,19 @@ function api_request($query) { throw new RuntimeException("Error: failed to retrieve pull request data\n", 4); } + $contents = json_decode($contents); - return json_decode($contents); + if (isset($contents->message) && strpos($contents->message, 'API Rate Limit') === 0) + { + exit('Reached github API Rate Limit. Please try again later' . "\n"); + } + + return $contents; } function get_pull($username, $repository, $pull_id) { - $request = api_request("pulls/$username/$repository/$pull_id"); + $request = api_request("repos/$username/$repository/pulls/$pull_id"); $pull = $request->pull; diff --git a/git-tools/setup_github_network.php b/git-tools/setup_github_network.php index 95f8c86ba3..de3ea55d13 100755 --- a/git-tools/setup_github_network.php +++ b/git-tools/setup_github_network.php @@ -60,7 +60,7 @@ function work($scope, $username, $repository, $developer) if ($forks === false || $collaborators === false) { - echo "Error: failed to retrieve network or collaborators\n"; + echo "Error: failed to retrieve forks or collaborators\n"; return 1; } @@ -143,34 +143,70 @@ function get_repository_url($username, $repository, $ssh = false) return $url_base . $username . '/' . $repository . '.git'; } -function api_request($query) +function api_request($query, $full_url = false) { $c = curl_init(); - curl_setopt($c, CURLOPT_URL, "https://api.github.com/$query"); + if ($full_url) + { + curl_setopt($c, CURLOPT_URL, $query); + } + else + { + curl_setopt($c, CURLOPT_URL, "https://api.github.com/$query?per_page=100"); + } curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_USERAGENT, 'phpBB/1.0'); + curl_setopt($c, CURLOPT_HEADER, true); $contents = curl_exec($c); curl_close($c); + $sub_request_result = array(); + if ($contents && strpos($contents, "\r\n\r\n") > 0) + { + list($header, $contents) = explode("\r\n\r\n", $contents); + foreach (explode("\n", $header) as $header_element) + { + if (strpos($header_element, 'Link') === 0) + { + list($head, $header_content) = explode(': ', $header_element); + foreach (explode(', ', $header_content) as $links) + { + list($url, $rel) = explode('; ', $links); + if ($rel == 'rel="next"') + { + $sub_request_result = api_request(substr($url, 1, -1), true); + } + } + } + } + } + if ($contents === false) { return false; } - return json_decode($contents); + $contents = json_decode($contents); + + if (isset($contents->message) && strpos($contents->message, 'API Rate Limit') === 0) + { + exit('Reached github API Rate Limit. Please try again later' . "\n"); + } + + return ($sub_request_result) ? array_merge($sub_request_result, $contents) : $contents; } function get_contributors($username, $repository) { - $request = api_request("repos/show/$username/$repository/contributors"); + $request = api_request("repos/$username/$repository/stats/contributors"); if ($request === false) { return false; } $usernames = array(); - foreach ($request->contributors as $contributor) + foreach ($request as $contribution) { - $usernames[$contributor->login] = $contributor->login; + $usernames[$contribution->author->login] = $contribution->author->login; } return $usernames; @@ -178,14 +214,14 @@ function get_contributors($username, $repository) function get_organisation_members($username) { - $request = api_request("organizations/$username/public_members"); + $request = api_request("orgs/$username/public_members"); if ($request === false) { return false; } $usernames = array(); - foreach ($request->users as $member) + foreach ($request as $member) { $usernames[$member->login] = $member->login; } @@ -195,16 +231,16 @@ function get_organisation_members($username) function get_collaborators($username, $repository) { - $request = api_request("repos/show/$username/$repository/collaborators"); + $request = api_request("repos/$username/$repository/collaborators"); if ($request === false) { return false; } $usernames = array(); - foreach ($request->collaborators as $collaborator) + foreach ($request as $collaborator) { - $usernames[$collaborator] = $collaborator; + $usernames[$collaborator->login] = $collaborator->login; } return $usernames; -- cgit v1.2.1