Skip to content

Add extra token #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 7, 2021
Merged
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
28 changes: 26 additions & 2 deletions src/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ function getContributionGraphs(string $user): array
return $response;
}

/**
* Get all tokens from environment variables (TOKEN, TOKEN2, TOKEN3, etc.) if they are set
*
* @return array<string> List of tokens
*/
function getGitHubTokens() {
// result is already calculated
if (isset($GLOBALS["ALL_TOKENS"])) {
return $GLOBALS["ALL_TOKENS"];
}
// find all tokens in environment variables
$tokens = array($_SERVER["TOKEN"] ?? "");
for ($i = 2; $i < 4; $i++) {
if (isset($_SERVER["TOKEN$i"])) {
// add token to list
$tokens[] = $_SERVER["TOKEN$i"];
}
}
// store for future use
$GLOBALS["ALL_TOKENS"] = $tokens;
return $tokens;
}

/** Create a CurlHandle for a POST request to GitHub's GraphQL API
*
* @param string $query GraphQL query
Expand All @@ -68,7 +91,8 @@ function getContributionGraphs(string $user): array
*/
function getGraphQLCurlHandle(string $query)
{
$token = $_SERVER["TOKEN"];
$all_tokens = getGitHubTokens();
$token = $all_tokens[array_rand($all_tokens)];
$headers = array(
"Authorization: bearer $token",
"Content-Type: application/json",
Expand Down Expand Up @@ -147,7 +171,7 @@ function getContributionYears(string $user): array
// API Error
if (!empty($response->errors)) {
// Other errors that contain a message field
throw new InvalidArgumentException($response->data->errors[0]->message);
throw new InvalidArgumentException($response->errors[0]->message);
}
// API did not return data
if (!isset($response->data) && isset($response->message)) {
Expand Down