From fb6f043f7fe16c24fe95397564117604080aa3fc Mon Sep 17 00:00:00 2001 From: caelean Date: Thu, 16 Jan 2025 14:08:59 -0800 Subject: [PATCH] prompt for username instead of using configreader --- python/tach/filesystem/git_ops.py | 3 --- python/tach/modularity.py | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/python/tach/filesystem/git_ops.py b/python/tach/filesystem/git_ops.py index d0b91d16..19057628 100644 --- a/python/tach/filesystem/git_ops.py +++ b/python/tach/filesystem/git_ops.py @@ -18,7 +18,6 @@ class GitBranchInfo: name: str commit: str owner: str - user_name: str email: str @@ -127,7 +126,6 @@ def get_current_branch_info( url = repo.remotes.origin.url owner_name, repo_name = _get_owner_and_repo_name(url) config_reader = repo.config_reader() - user_name = str(config_reader.get_value("user", "name", default="")) email = str(config_reader.get_value("user", "email", default="")) branch = _get_branch_name(repo) commit = _get_commit(repo) @@ -140,7 +138,6 @@ def get_current_branch_info( owner=owner_name, name=branch, commit=commit, - user_name=user_name, email=email, ) diff --git a/python/tach/modularity.py b/python/tach/modularity.py index 748d9f33..864ffb73 100644 --- a/python/tach/modularity.py +++ b/python/tach/modularity.py @@ -57,7 +57,9 @@ def upload_report_to_gauge( GAUGE_UPLOAD_URL = f"{GAUGE_API_BASE_URL}/api/client/tach-upload/1.3" -def post_json_to_gauge_api(data: dict[str, Any]) -> dict[str, str]: +def post_json_to_gauge_api( + data: dict[str, Any], user_name: str | None = None +) -> dict[str, str]: if not GAUGE_API_KEY: raise TachClosedBetaError( f"{BCOLORS.WARNING}Modularity is currently in closed beta. Visit {GAUGE_API_BASE_URL}/closed-beta to request access.{BCOLORS.ENDC}" @@ -68,6 +70,8 @@ def post_json_to_gauge_api(data: dict[str, Any]) -> dict[str, str]: "Content-Type": "application/json", "Authorization": GAUGE_API_KEY, } + if user_name: + data["user_name"] = user_name json_data = json.dumps(data) conn = None try: @@ -79,7 +83,12 @@ def post_json_to_gauge_api(data: dict[str, Any]) -> dict[str, str]: conn.request("POST", url_parts.path, body=json_data, headers=headers) response = conn.getresponse() response_data = response.read().decode("utf-8") - # Check for non-200 status codes + # If key is unbound, prompt user to provide username to bind key + if response.status == 422 and not user_name: + # Prompt user to provide username + conn.close() + user_name = input("Enter your GitHub username: ").strip() + return post_json_to_gauge_api(data, user_name) if response.status != 200: raise TachError( f"API request failed with status {response.status}: {response_data}" @@ -299,7 +308,7 @@ def generate_modularity_report( print(f"{BCOLORS.OKCYAN} > Generating report...{BCOLORS.ENDC}") branch_info = get_current_branch_info(project_root, allow_dirty=force) report = Report( - user_name=branch_info.user_name, + user_name="", # only needed for binding a new api key email=branch_info.email, owner=branch_info.owner, repo=branch_info.repo,