From 4cb7ce47dc099067071cfb3b6d4f553e375c1008 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Tue, 7 Feb 2023 10:51:24 -0700 Subject: [PATCH 1/3] fix: filter years before account creation --- src/stats.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/stats.php b/src/stats.php index d6551652..e8227605 100644 --- a/src/stats.php +++ b/src/stats.php @@ -15,6 +15,7 @@ function buildContributionGraphQuery(string $user, int $year): string $end = "$year-12-31T23:59:59Z"; return "query { user(login: \"$user\") { + createdAt contributionsCollection(from: \"$start\", to: \"$end\") { contributionYears contributionCalendar { @@ -127,9 +128,13 @@ function getContributionGraphs(string $user): array if (empty($contributionYears)) { throw new AssertionError("Failed to retrieve contributions. This is likely a GitHub API issue.", 500); } + // get user's created date (YYYY-MM-DDTHH:MM:SSZ format) extract the year + $userCreatedDateTimeString = $responses[$currentYear]->data->user->createdAt; + $userCreatedYear = intval(explode("-", $userCreatedDateTimeString)[0]); // remove the current year from the list since it's already been fetched - $contributionYears = array_filter($contributionYears, function ($year) use ($currentYear) { - return $year !== $currentYear; + // and filter out any years that are before the user's account creation + $contributionYears = array_filter($contributionYears, function ($year) use ($currentYear, $userCreatedYear) { + return $year !== $currentYear && $year >= $userCreatedYear; }); // get the contribution graphs for the previous years $responses += executeContributionGraphRequests($user, $contributionYears); From 841780cf084c9b4a063dfdcec4685288d49e0e76 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Tue, 7 Feb 2023 11:02:27 -0700 Subject: [PATCH 2/3] refactor: Remove use of contributionYears --- src/stats.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/stats.php b/src/stats.php index e8227605..07817276 100644 --- a/src/stats.php +++ b/src/stats.php @@ -17,7 +17,6 @@ function buildContributionGraphQuery(string $user, int $year): string user(login: \"$user\") { createdAt contributionsCollection(from: \"$start\", to: \"$end\") { - contributionYears contributionCalendar { weeks { contributionDays { @@ -123,19 +122,16 @@ 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 ?? []; + // get user's created date (YYYY-MM-DDTHH:MM:SSZ format) + $userCreatedDateTimeString = $responses[$currentYear]->data->user->createdAt ?? null; // if there are no contribution years, an API error must have occurred - if (empty($contributionYears)) { + if (empty($userCreatedDateTimeString)) { throw new AssertionError("Failed to retrieve contributions. This is likely a GitHub API issue.", 500); } - // get user's created date (YYYY-MM-DDTHH:MM:SSZ format) extract the year - $userCreatedDateTimeString = $responses[$currentYear]->data->user->createdAt; + // extract the year from the created datetime string $userCreatedYear = intval(explode("-", $userCreatedDateTimeString)[0]); - // remove the current year from the list since it's already been fetched - // and filter out any years that are before the user's account creation - $contributionYears = array_filter($contributionYears, function ($year) use ($currentYear, $userCreatedYear) { - return $year !== $currentYear && $year >= $userCreatedYear; - }); + // create an array of years from the user's created year to one year before the current year + $contributionYears = range($userCreatedYear, $currentYear - 1); // get the contribution graphs for the previous years $responses += executeContributionGraphRequests($user, $contributionYears); return $responses; From 8e8d14b1e1e86709b6ea4e4e90c62bfb3c76554b Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Tue, 7 Feb 2023 12:10:56 -0700 Subject: [PATCH 3/3] Add an exception for 1 year of backdated commits --- src/stats.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/stats.php b/src/stats.php index 07817276..0ad21e4c 100644 --- a/src/stats.php +++ b/src/stats.php @@ -17,6 +17,7 @@ function buildContributionGraphQuery(string $user, int $year): string user(login: \"$user\") { createdAt contributionsCollection(from: \"$start\", to: \"$end\") { + contributionYears contributionCalendar { weeks { contributionDays { @@ -131,9 +132,16 @@ function getContributionGraphs(string $user): array // extract the year from the created datetime string $userCreatedYear = intval(explode("-", $userCreatedDateTimeString)[0]); // create an array of years from the user's created year to one year before the current year - $contributionYears = range($userCreatedYear, $currentYear - 1); + $yearsToRequest = range($userCreatedYear, $currentYear - 1); + // also check the first contribution year if the year is before 2005 (the year Git was created) + // since the user may have backdated some commits to a specific year such as 1970 (see #448) + $contributionYears = $responses[$currentYear]->data->user->contributionsCollection->contributionYears ?? []; + $firstContributionYear = $contributionYears[count($contributionYears) - 1] ?? $userCreatedYear; + if ($firstContributionYear < 2005) { + array_unshift($yearsToRequest, $firstContributionYear); + } // get the contribution graphs for the previous years - $responses += executeContributionGraphRequests($user, $contributionYears); + $responses += executeContributionGraphRequests($user, $yearsToRequest); return $responses; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy