Skip to content
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

fix: Catch error if failed to get contributions twice #424

Merged
merged 2 commits into from
Jan 17, 2023
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
8 changes: 7 additions & 1 deletion src/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function executeContributionGraphRequests(string $user, array $years): array
removeGitHubToken($tokens[$year]);
}
error_log("First attempt to decode response for $user's $year contributions failed. $message");
error_log("Contents: $contents");
// retry request
$query = buildContributionGraphQuery($user, $year);
$token = getGitHubToken();
Expand All @@ -96,6 +97,7 @@ function executeContributionGraphRequests(string $user, array $years): array
removeGitHubToken($token);
}
error_log("Failed to decode response for $user's $year contributions after 2 attempts. $message");
error_log("Contents: $contents");
continue;
}
}
Expand All @@ -120,7 +122,11 @@ function getContributionGraphs(string $user): array
// get the list of years the user has contributed and the current year's contribution graph
$currentYear = intval(date("Y"));
$responses = executeContributionGraphRequests($user, [$currentYear]);
$contributionYears = $responses[$currentYear]->data->user->contributionsCollection->contributionYears;
$contributionYears = $responses[$currentYear]->data->user->contributionsCollection->contributionYears ?? [];
// if there are no contribution years, an API error must have occurred
if (empty($contributionYears)) {
throw new AssertionError("Failed to retrieve contributions. This is likely a GitHub API issue.", 500);
}
// remove the current year from the list since it's already been fetched
$contributionYears = array_filter($contributionYears, function ($year) use ($currentYear) {
return $year !== $currentYear;
Expand Down