Skip to content

Commit

Permalink
feat: Added option to override starting year for contributions (#507)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonah jonah@freshidea.com
Co-authored-by: Jonah Lawrence <jonah@freshidea.com>
  • Loading branch information
chrisK824 and DenverCoder1 authored Apr 23, 2023
1 parent 9b9b63d commit fb24da9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ DOCKER_ENV
docker_tag

# IDE
.vscode/
.vscode/
.idea/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<stdClass> 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"));
Expand All @@ -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 ?? [];
Expand Down

0 comments on commit fb24da9

Please sign in to comment.