diff --git a/DirectReport/browserview/auth/routes.py b/DirectReport/browserview/auth/routes.py index bb159ef3..23539cfb 100644 --- a/DirectReport/browserview/auth/routes.py +++ b/DirectReport/browserview/auth/routes.py @@ -71,6 +71,7 @@ def account_data(): "username": current_user.username, "email": current_user.email, "github_username": current_user.github_username, + "github_repo": current_user.github_repo } user_element = {"user": user_account, "reports": report_results, "shortlog": shortlog} return user_element, 201 diff --git a/DirectReport/browserview/dashboard/routes.py b/DirectReport/browserview/dashboard/routes.py index 1d89e47d..6059bcda 100644 --- a/DirectReport/browserview/dashboard/routes.py +++ b/DirectReport/browserview/dashboard/routes.py @@ -9,6 +9,7 @@ from DirectReport.browserview.services.github import GithubClient from DirectReport.browserview.services.huggingface_client import HuggingFaceClient from DirectReport.browserview.services.googleai_client import GoogleAIClient +from DirectReport.browserview.services.prompt_logic import generate_email, team_summary_from_shortlog from DirectReport.models.report.report_model import ReportModel from DirectReport.datadependencies import appsecrets @@ -55,13 +56,27 @@ def dashboard_reports_update(): client = GithubClient() h_token = session['header_token'] user_repos = client.get_user_repos(current_user.github_username, h_token) + commits_last_month = client.get_commits_in_last_month(current_user.github_username, current_user.github_repo, h_token) + commits_last_sixty = client.get_commits_in_last_sixty_days(current_user.github_username, current_user.github_repo, h_token) + commits_last_ninety = client.get_commits_in_last_ninety_days(current_user.github_username, current_user.github_repo, h_token) + get_pull_requests_count = client.get_pull_requests_count(current_user.github_username, current_user.github_repo, h_token) + get_pull_requests_count_sixty = client.get_pull_requests_count_sixty_days(current_user.github_username, current_user.github_repo, h_token) repo_data = [] for repo in user_repos: repo_data.append(repo["name"]) - raw_data = googleAi.get_data_from(prompt) - begin, end = raw_data.find('{'), raw_data.rfind('}') - filtered_str = raw_data[begin : end + 1] - response_data = json.loads(filtered_str) + raw_data = team_summary_from_shortlog(prompt) + print(raw_data) + raw_reponse = raw_data["choices"][0]["message"]["content"] + response_data = json.loads(raw_reponse) + # print(response_data) + # list(raw_data.choices)[0] + # my_openai_obj.to_dict()['message']['content'] + # response_data = json.loads(raw_data) + # response_data = json.dumps(raw_data) + # raw_data = googleAi.get_data_from(prompt) + # begin, end = raw_data.find('{'), raw_data.rfind('}') + # filtered_str = raw_data[begin: end + 1] + # response_data = json.loads(filtered_str) response_data["broad_categories"] = { "debug_info": 16, "code_maintenance": 9, @@ -71,6 +86,8 @@ def dashboard_reports_update(): "readme_update": 1, "syntax_fix": 1, } + response_data["commit_nums"] = {"15 days": 4, "30 days": (commits_last_month / 10), "60 days": (commits_last_sixty / 10), "90 days": (commits_last_ninety / 10), "120 days": 30} + response_data["pull_requests"] = {"30 days": 5, "60 days": 10, "90 days": 15, "120 days": 20, "150 days": 20, "1 year": 30} response_data["repos"] = repo_data ReportBuilder.new(response_data, prompt, current_user.id, "DirectReport") return response_data, 201 diff --git a/DirectReport/browserview/main/routes.py b/DirectReport/browserview/main/routes.py index dd4073b9..4c880709 100644 --- a/DirectReport/browserview/main/routes.py +++ b/DirectReport/browserview/main/routes.py @@ -20,6 +20,8 @@ def before_request(): if current_user.is_authenticated: print("authenticated user") + print(current_user.github_username) + print(current_user.github_repo) else: print("unauthenticated user") @@ -60,8 +62,11 @@ def get_commits_last_month(repo_name): @bp.route('/repo', methods=['GET', 'POST']) def reponame(): args_url = request.args.get('repo_url') + repo = args_url.split('/')[1] h_token = session['header_token'] repo_name = "https://api.github.com/repos/" + args_url + "/commits" + user_model = UserModel() + user_model.update_github_repo(current_user.email, repo) headers = { 'Accept': 'application/vnd.github+json', 'Authorization': 'Bearer ' + h_token, diff --git a/DirectReport/browserview/services/github.py b/DirectReport/browserview/services/github.py index 5b04a10d..a1a9fe52 100644 --- a/DirectReport/browserview/services/github.py +++ b/DirectReport/browserview/services/github.py @@ -3,7 +3,7 @@ import re import requests from DirectReport.datadependencies import appsecrets, prompts - +from datetime import datetime, timedelta class GithubClient: # Define a function to parse the git shortlog @@ -45,7 +45,7 @@ def get_pull_request_comments(self, repo_owner, repo_name): response.raise_for_status() return response.json() - def get_pull_requests_count(self, repo_owner, repo_name): + def get_pull_requests_count(self, repo_owner, repo_name, token): """ Gets the number of comments on a pull request. @@ -58,11 +58,64 @@ def get_pull_requests_count(self, repo_owner, repo_name): The number of comments on the pull request. """ + current_date = datetime.now() + thirty_days_ago = datetime.now() - timedelta(days=30) + since = thirty_days_ago.isoformat() url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls" headers = {"Authorization": f"token {appsecrets.GITHUB_TOKEN}"} - response = requests.get(url, headers=headers) - response.raise_for_status() - return len(response.json()) + commits_count = 0 + page = 1 + per_page = 100 # Number of results per page + while True: + # Make a request to the GitHub API + response = requests.get(url, headers=headers, params={'since': since, 'page': page, 'per_page': per_page}) + response.raise_for_status() + if response.status_code != 200: + # Break the loop if the response is not successful + break + commits = response.json() + current_count = len(commits) + commits_count += current_count + if current_count < per_page: + break + page += 1 + return commits_count + + def get_pull_requests_count_sixty_days(self, repo_owner, repo_name, token): + """ + Gets the number of comments on a pull request. + + Args: + repo_owner: The owner of the GitHub repository. + repo_name: The name of the GitHub repository. + pull_request_number: The number of the pull request. + + Returns: + The number of comments on the pull request. + """ + + current_date = datetime.now() + thirty_days_ago = datetime.now() - timedelta(days=60) + since = thirty_days_ago.isoformat() + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls" + headers = {"Authorization": f"token {appsecrets.GITHUB_TOKEN}"} + commits_count = 0 + page = 1 + per_page = 100 # Number of results per page + while True: + # Make a request to the GitHub API + response = requests.get(url, headers=headers, params={'since': since, 'page': page, 'per_page': per_page}) + response.raise_for_status() + if response.status_code != 200: + # Break the loop if the response is not successful + break + commits = response.json() + current_count = len(commits) + commits_count += current_count + if current_count < per_page: + break + page += 1 + return commits_count def get_user_repos(self, repo_owner, token): url = f"https://api.github.com/users/{repo_owner}/repos?sort=updated&order=desc" @@ -71,9 +124,79 @@ def get_user_repos(self, repo_owner, token): response.raise_for_status() return response.json() - def get_repo_issues(self, repo_owner, repo_name): + def get_repo_issues(self, repo_owner, repo_name, token): headers = {"Authorization": f"token {appsecrets.GITHUB_TOKEN}"} url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues" response = requests.get(url, headers=headers) response.raise_for_status() return response.json() + + def get_commits_in_last_month(self, repo_owner, repo_name, token): + current_date = datetime.now() + thirty_days_ago = datetime.now() - timedelta(days=30) + since = thirty_days_ago.isoformat() + headers = {"Authorization": f"token {token}"} + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits" + commits_count = 0 + page = 1 + per_page = 100 # Number of results per page + while True: + # Make a request to the GitHub API + response = requests.get(url, headers=headers, params={'since': since, 'page': page, 'per_page': per_page}) + if response.status_code != 200: + # Break the loop if the response is not successful + break + commits = response.json() + current_count = len(commits) + commits_count += current_count + if current_count < per_page: + break + page += 1 + return commits_count + + def get_commits_in_last_sixty_days(self, repo_owner, repo_name, token): + current_date = datetime.now() + sixty_days_ago = datetime.now() - timedelta(days=60) + since = sixty_days_ago.isoformat() + headers = {"Authorization": f"token {token}"} + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits" + commits_count = 0 + page = 1 + per_page = 100 # Number of results per page + while True: + # Make a request to the GitHub API + response = requests.get(url, headers=headers, params={'since': since, 'page': page, 'per_page': per_page}) + if response.status_code != 200: + # Break the loop if the response is not successful + break + commits = response.json() + current_count = len(commits) + commits_count += current_count + if current_count < per_page: + break + page += 1 + return commits_count + + def get_commits_in_last_ninety_days(self, repo_owner, repo_name, token): + current_date = datetime.now() + ninety_days_ago = datetime.now() - timedelta(days=90) + since = ninety_days_ago.isoformat() + headers = {"Authorization": f"token {token}"} + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits" + commits_count = 0 + page = 1 + per_page = 100 # Number of results per page + while True: + # Make a request to the GitHub API + response = requests.get(url, headers=headers, params={'since': since, 'page': page, 'per_page': per_page}) + if response.status_code != 200: + # Break the loop if the response is not successful + break + commits = response.json() + current_count = len(commits) + commits_count += current_count + if current_count < per_page: + break + page += 1 + return commits_count + diff --git a/DirectReport/browserview/static/js/account/account_components.js b/DirectReport/browserview/static/js/account/account_components.js index 9d02c83a..44568cf4 100644 --- a/DirectReport/browserview/static/js/account/account_components.js +++ b/DirectReport/browserview/static/js/account/account_components.js @@ -63,14 +63,15 @@ const AccountUserInfo = (userData, reportData) => {
- GITHUB ACOUNT USERNAME: - {userData.github_username} + GITHUB ACCOUNT: + {userData.github_username ? userData.github_username : 'None Connected'}
SELECTED REPO: - DirectReport + {userData.github_repo ? userData.github_repo : 'None Selected'} +