From 64aefd76408f58dc4be1377d39bf1aacb8882fab Mon Sep 17 00:00:00 2001 From: gmguarino Date: Wed, 31 Jan 2024 15:42:28 +0100 Subject: [PATCH] change to season wide stats --- config/projects.json | 14 ++++----- github_repo/github_handler.py | 4 +-- update_dashboards.py | 58 +++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/config/projects.json b/config/projects.json index 68152b9..31c18f7 100644 --- a/config/projects.json +++ b/config/projects.json @@ -1,9 +1,9 @@ { - "season_12": [], - "test": [], - "offseason":[ - {"github_org": "dataforgoodfr", "github_repo": "Bloom", "notion_page_id": "6402dd0a4a104694a5d095a896234b18"}, - {"github_org": "dataforgoodfr", "github_repo": "CarbonBombs", "notion_page_id": "4ea70a69962b42ea87b98164dc242756"}, - {"github_org": "dataforgoodfr", "github_repo": "CarbonBombsApp", "notion_page_id": "99a0233d2d444ba69a37f3936e13d471"} - ] + "offseason": { + "notion_page_id": "73dfe37b0bbd4ff1915147eb55a0bcac", + "repos": [ + {"github_org": "dataforgoodfr", "github_repo": "Bloom"}, + {"github_org": "dataforgoodfr", "github_repo": "CarbonBombs"} + ] + } } \ No newline at end of file diff --git a/github_repo/github_handler.py b/github_repo/github_handler.py index 082f9a3..91428a1 100644 --- a/github_repo/github_handler.py +++ b/github_repo/github_handler.py @@ -26,6 +26,7 @@ class GithubGenericError(Exception): """ def __init__(self, response): + print(response.json()) message = f"Generic error was returned with status code: {response.status_code}." super().__init__(message) @@ -70,12 +71,11 @@ def get_total_contributors(self): response = self.__get_request_generic(request_url) return len(response.json()) - def get_top_contributors(self, n=10): + def get_top_contributors(self): request_url=f"https://api.github.com/repos/{self.github_owner}/{self.github_repo}/stats/contributors" response = self.__get_request_generic(request_url) data = response.json() contributor_records = list(map(lambda d: {"Name": d["author"]["login"], "Commits": d["total"]}, data)) - contributor_records = sorted(contributor_records, key=lambda d: d['Commits'], reverse=True)[:n] return contributor_records def __get_all_issues(self): diff --git a/update_dashboards.py b/update_dashboards.py index 1559c5b..d338d9e 100644 --- a/update_dashboards.py +++ b/update_dashboards.py @@ -34,14 +34,27 @@ def initialize_overview_dashboard(args, parent_page_id): return dashboard -def update_overview_dashboard(repo, dashboard): +def update_overview_dashboard(stats, dashboard): + # Updates with new values + dashboard.update_dashboard(commits=stats["n_commits"], pr=stats["open_prs"], contributors=stats["n_contributors"], open_issues=stats["open_issues"]) + + +def get_overview_dashboard_stats(repo): n_commits = repo.get_total_commits() n_contributors = repo.get_total_contributors() - contributor_records = repo.get_top_contributors() open_issues = repo.get_open_issues() open_prs = repo.get_open_pull_requests() # Updates with new values - dashboard.update_dashboard(commits=n_commits, pr=open_prs, contributors=n_contributors, open_issues=open_issues) + return dict(n_commits=n_commits, n_contributors=n_contributors, open_issues=open_issues, open_prs=open_prs) + + +def update_repo_stats(overview_stats, repo_stats): + for key in overview_stats: + if key in repo_stats: + repo_stats[key] = repo_stats[key] + overview_stats[key] + else: + repo_stats[key] = overview_stats[key] + return repo_stats def initialize_contributor_dashboard(args, parent_page_id): @@ -51,10 +64,23 @@ def initialize_contributor_dashboard(args, parent_page_id): dashboard.create_dashboard(dashboard.parse_contributors(start_records, as_records=True)) return dashboard - -def update_contributor_dashboard(repo, dashboard): +def get_contributor_dashboard_stats(repo): contributor_records = repo.get_top_contributors() # Updates with new values + return contributor_records + +def update_contributor_stats(contributor_stats, records): + for record in records: + if record["Name"] in contributor_stats: + contributor_stats[record["Name"]] = contributor_stats[record["Name"]] + record["Commits"] + else: + contributor_stats[record["Name"]] = record["Commits"] + return contributor_stats + +def update_contributor_dashboard(contributor_stats, dashboard, n=10): + contributor_records = [{"Name": key, "Commits": value} for key, value in contributor_stats.items()] + contributor_records = sorted(contributor_records, key=lambda d: d['Commits'], reverse=True)[:n] + # Updates with new values dashboard.update_dashboard(dashboard.parse_contributors(contributor_records, as_records=False)) @@ -71,14 +97,22 @@ def update_contributor_dashboard(repo, dashboard): config = get_config(args.config) for project_group in config: - for project in config[project_group]: - print(project) - repo = GithubHandler(args.github_token, project["github_org"], project["github_repo"]) - overview_dashboard = initialize_overview_dashboard(args, project["notion_page_id"]) - update_overview_dashboard(repo, overview_dashboard) + contributor_stats = {} + repo_stats = {} + overview_dashboard = initialize_overview_dashboard(args, config[project_group]["notion_page_id"]) + contributor_dashboard = initialize_contributor_dashboard(args, config[project_group]["notion_page_id"]) - contributor_dashboard = initialize_contributor_dashboard(args, project["notion_page_id"]) - update_contributor_dashboard(repo, contributor_dashboard) + for project in config[project_group]["repos"]: + repo = GithubHandler(args.github_token, project["github_org"], project["github_repo"]) + + overview_stats = get_overview_dashboard_stats(repo) + repo_stats = update_repo_stats(overview_stats, repo_stats) + + contributors = get_contributor_dashboard_stats(repo) + contributor_stats = update_contributor_stats(contributor_stats, contributors) + + update_overview_dashboard(repo_stats, overview_dashboard) + update_contributor_dashboard(contributor_stats, contributor_dashboard)