From 71f950e947f0de905e0cb295e27e4a05569a26a8 Mon Sep 17 00:00:00 2001 From: devfle <52854338+devfle@users.noreply.github.com> Date: Wed, 21 Feb 2024 23:18:03 +0000 Subject: [PATCH] feat: get all contributes since registration --- graphql_query.py | 25 ++++++++++++++++++++----- readme_level.py | 19 ++++++++++++++++--- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/graphql_query.py b/graphql_query.py index 2f77c09..625be39 100644 --- a/graphql_query.py +++ b/graphql_query.py @@ -1,14 +1,29 @@ """Query for github graphql api""" -from os import getenv +from os import getenv, linesep +from datetime import datetime -QUERY = f""" - {{ - user(login: "{getenv("INPUT_GITHUB_USERNAME")}") {{ - contributionsCollection {{ +# get the current year +current_year = datetime.now().year +contribution_list = [] + +# in a later version we can replace the 2015 date with user input +while current_year >= 2015: + + temp_query = f""" + {"_" + str(current_year)}: contributionsCollection(from: "{str(current_year)}-01-01T00:00:00") {{ contributionCalendar {{ totalContributions }} }} + """ + + contribution_list.append(temp_query) + current_year -= 1 + +QUERY = f""" + {{ + user(login: "{getenv("INPUT_GITHUB_USERNAME")}") {{ + {linesep.join(contribution_list)} followers {{ totalCount }} diff --git a/readme_level.py b/readme_level.py index 2cc9949..9fe8ffe 100644 --- a/readme_level.py +++ b/readme_level.py @@ -2,6 +2,7 @@ from os import getenv from logging import exception, info from requests import post +from datetime import datetime from graphql_query import QUERY from readme_data import ReadmeLevelData @@ -23,7 +24,7 @@ def fetch_user_data(self) -> dict[str, int] | None: if not getenv("INPUT_GITHUB_TOKEN"): exception("an error with the github token occurred") - + auth_header = {"Authorization": "Bearer " + getenv("INPUT_GITHUB_TOKEN")} response = post("https://api.github.com/graphql", @@ -34,8 +35,20 @@ def fetch_user_data(self) -> dict[str, int] | None: response_data = response.json() - user_data = (response_data["data"]["user"] - ["contributionsCollection"]["contributionCalendar"]) + current_year = datetime.now().year + total_contribution = [] + + while current_year >= 2015: + contribution_count = (response_data["data"]["user"] + ["_" + str(current_year)]["contributionCalendar"]["totalContributions"]) + + total_contribution.append(contribution_count) + current_year -= 1 + + + user_data = {} + + user_data["totalContributions"] = sum(total_contribution) user_data["totalFollowers"] = (response_data["data"]["user"] ["followers"]["totalCount"])