Skip to content

Commit ec87af5

Browse files
fix: Solve issue with missing years by retrying (#370)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 148cff7 commit ec87af5

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

src/stats.php

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@
22

33
declare(strict_types=1);
44

5+
/**
6+
* Build a query for a contribution graph
7+
*
8+
* @param string $user GitHub username to get graphs for
9+
* @param int $year Year to get graph for
10+
*
11+
* @return string GraphQL query
12+
*/
13+
function buildContributionGraphQuery(string $user, int $year)
14+
{
15+
$start = "$year-01-01T00:00:00Z";
16+
$end = "$year-12-31T23:59:59Z";
17+
return "query {
18+
user(login: \"$user\") {
19+
contributionsCollection(from: \"$start\", to: \"$end\") {
20+
contributionCalendar {
21+
totalContributions
22+
weeks {
23+
contributionDays {
24+
contributionCount
25+
date
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}";
32+
}
33+
534
/**
635
* Get all HTTP request responses for user's contributions
736
*
@@ -17,23 +46,7 @@ function getContributionGraphs(string $user): array
1746
$requests = [];
1847
foreach ($contributionYears as $year) {
1948
// create query for year
20-
$start = "$year-01-01T00:00:00Z";
21-
$end = "$year-12-31T23:59:59Z";
22-
$query = "query {
23-
user(login: \"$user\") {
24-
contributionsCollection(from: \"$start\", to: \"$end\") {
25-
contributionCalendar {
26-
totalContributions
27-
weeks {
28-
contributionDays {
29-
contributionCount
30-
date
31-
}
32-
}
33-
}
34-
}
35-
}
36-
}";
49+
$query = buildContributionGraphQuery($user, $year);
3750
// create curl request
3851
$requests[$year] = getGraphQLCurlHandle($query);
3952
}
@@ -47,16 +60,29 @@ function getContributionGraphs(string $user): array
4760
do {
4861
curl_multi_exec($multi, $running);
4962
} while ($running);
63+
// collect responses from last to first
64+
$response = [];
65+
foreach ($requests as $year => $request) {
66+
$contents = curl_multi_getcontent($request);
67+
$decoded = json_decode($contents);
68+
// if response is empty or invalid, retry request one time
69+
if (empty($decoded)) {
70+
$query = buildContributionGraphQuery($user, $year);
71+
$request = getGraphQLCurlHandle($query);
72+
$contents = curl_exec($request);
73+
if ($contents === false) {
74+
error_log("Failed to decode response for $user's $year contributions after 2 attempts.");
75+
continue;
76+
}
77+
$decoded = json_decode($contents);
78+
}
79+
array_unshift($response, $decoded);
80+
}
5081
// close the handles
5182
foreach ($requests as $request) {
5283
curl_multi_remove_handle($multi, $request);
5384
}
5485
curl_multi_close($multi);
55-
// collect responses from last to first
56-
$response = [];
57-
foreach ($requests as $request) {
58-
array_unshift($response, json_decode(curl_multi_getcontent($request)));
59-
}
6086
return $response;
6187
}
6288

@@ -190,17 +216,15 @@ function getContributionYears(string $user): array
190216
*/
191217
function getContributionDates(array $contributionGraphs): array
192218
{
193-
// get contributions from HTML
194219
$contributions = [];
195220
$today = date("Y-m-d");
196221
$tomorrow = date("Y-m-d", strtotime("tomorrow"));
222+
// sort contribution calendars by year key
223+
ksort($contributionGraphs);
197224
foreach ($contributionGraphs as $graph) {
198225
if (!empty($graph->errors)) {
199226
throw new AssertionError($graph->data->errors[0]->message, 502);
200227
}
201-
if (empty($graph)) {
202-
continue;
203-
}
204228
$weeks = $graph->data->user->contributionsCollection->contributionCalendar->weeks;
205229
foreach ($weeks as $week) {
206230
foreach ($week->contributionDays as $day) {

0 commit comments

Comments
 (0)