diff --git a/.gitignore b/.gitignore index 560625aa..eee6db6c 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ DOCKER_ENV docker_tag # IDE -.vscode/ \ No newline at end of file +.vscode/ +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index bcf3da9b..2728ecd4 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ If the `theme` parameter is specified, any color customizations specified will b | `hide_total_contributions` | Hide the total contributions (Default: `false`) | `true` or `false` | | `hide_current_streak` | Hide the current streak (Default: `false`) | `true` or `false` | | `hide_longest_streak` | Hide the longest streak (Default: `false`) | `true` or `false` | +| `starting_year` | Starting year of contributions | Integer, must be `2005` or later, eg. `2017`. By default, your account creation year is used. | ### 🖌 Themes diff --git a/src/index.php b/src/index.php index 2de2c316..41cc4e0b 100644 --- a/src/index.php +++ b/src/index.php @@ -33,7 +33,8 @@ try { // get streak stats for user given in query string $user = preg_replace("/[^a-zA-Z0-9\-]/", "", $_REQUEST["user"]); - $contributionGraphs = getContributionGraphs($user); + $startingYear = isset($_REQUEST["starting_year"]) ? intval($_REQUEST["starting_year"]) : null; + $contributionGraphs = getContributionGraphs($user, $startingYear); $contributions = getContributionDates($contributionGraphs); if (isset($_GET["mode"]) && $_GET["mode"] === "weekly") { $stats = getWeeklyContributionStats($contributions); diff --git a/src/stats.php b/src/stats.php index ba77b4e8..6511758c 100644 --- a/src/stats.php +++ b/src/stats.php @@ -116,9 +116,10 @@ function executeContributionGraphRequests(string $user, array $years): array * Get all HTTP request responses for user's contributions * * @param string $user GitHub username to get graphs for + * @param int|null $startingYear Override the minimum year to get graphs for * @return array List of contribution graph response objects */ -function getContributionGraphs(string $user): array +function getContributionGraphs(string $user, ?int $startingYear = null): array { // get the list of years the user has contributed and the current year's contribution graph $currentYear = intval(date("Y")); @@ -131,8 +132,13 @@ function getContributionGraphs(string $user): array } // extract the year from the created datetime string $userCreatedYear = intval(explode("-", $userCreatedDateTimeString)[0]); + // if override parameter is null then define starting year + // as the user created year; else use the provided override year + $minimumYear = $startingYear ?: $userCreatedYear; + // make sure the minimum year is not before 2005 (the year Git was created) + $minimumYear = max($minimumYear, 2005); // create an array of years from the user's created year to one year before the current year - $yearsToRequest = range($userCreatedYear, $currentYear - 1); + $yearsToRequest = range($minimumYear, $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 ?? [];