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

feat: Added option to override starting year for contributions #507

Merged
Merged
Show file tree
Hide file tree
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
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