From f06bd03d3435b1fd384744b74ed967680c2892a3 Mon Sep 17 00:00:00 2001 From: Harsh Vardhan Date: Fri, 1 Dec 2023 13:03:02 +0530 Subject: [PATCH 1/2] Bug fix --- starcli/search.py | 28 ++++++++++++++++++++++++---- tests/test_search.py | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/starcli/search.py b/starcli/search.py index a327e24..03b7346 100644 --- a/starcli/search.py +++ b/starcli/search.py @@ -28,6 +28,13 @@ "monthly": ("this month", "month"), } +# Key: Most accepted language symbol identifier +# Value: list of language title which are correct but throws error with gtrending.fetch_repos method +LANGUAGE_MAPPING = { + 'c#': ['csharp'], + 'c++': ['cpp'], +} + STATUS_ACTIONS = { "retry": "Failed to retrieve data. Retrying in ", "invalid": "The server was unable to process the request.", @@ -241,7 +248,7 @@ def search( query += f"stars:{stars}+created:{created_str}" # construct query query += f"+pushed:{pushed_str}" # add pushed info to query query += "".join( - [f"+language:{language}" for language in languages] + [f"+language:{requests.utils.quote(language)}" for language in languages] ) # add language to query query += "".join(["+topic:" + i for i in topics]) # add topics to query @@ -279,9 +286,22 @@ def search_github_trending( debug, f"gtrending: fetching repos: language={language}, spoken_language={spoken_language}, since={since}", ) - gtrending_repo_list += gtrending.fetch_repos( - language, spoken_language, since - ) + + for alternate_lang_name, invalid_lang_name in LANGUAGE_MAPPING.items(): + if language.lower() in invalid_lang_name: + language = alternate_lang_name + break + + try: + gtrending_repo_list += gtrending.fetch_repos( + language, spoken_language, since + ) + except: + secho( + f"Invalid language \"{language}\" passed...", + fg="bright_red", + ) + return None else: debug_logger( debug, diff --git a/tests/test_search.py b/tests/test_search.py index 1cad573..49393ac 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -10,7 +10,7 @@ def test_search_language(): """Test searching by language""" - for language in ["python", "Python", "JavaScript", "c"]: + for language in ["python", "Python", "JavaScript", "c", "cpp", "csharp"]: repos = search([language]) for repo in repos: assert repo["stargazers_count"] >= 0 From a6d3ba8747d18c380b71988be8dd9274d1233cef Mon Sep 17 00:00:00 2001 From: Harsh Vardhan Date: Fri, 1 Dec 2023 13:16:02 +0530 Subject: [PATCH 2/2] black formatting --- starcli/layouts.py | 1 - starcli/search.py | 14 +++++++------- tests/test_cli.py | 6 +++--- tests/test_search.py | 1 + 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/starcli/layouts.py b/starcli/layouts.py index 40fc35a..ccdd6a5 100644 --- a/starcli/layouts.py +++ b/starcli/layouts.py @@ -147,7 +147,6 @@ def grid_layout(repos): panels = [] for repo in repos: - stats = format_stats(repo["stargazers_count"], repo["forks"]) # '\n' added here as it would group both text and new line together # hence if date_range isn't present the new line will also not be displayed diff --git a/starcli/search.py b/starcli/search.py index 03b7346..cd8ec35 100644 --- a/starcli/search.py +++ b/starcli/search.py @@ -31,16 +31,16 @@ # Key: Most accepted language symbol identifier # Value: list of language title which are correct but throws error with gtrending.fetch_repos method LANGUAGE_MAPPING = { - 'c#': ['csharp'], - 'c++': ['cpp'], + "c#": ["csharp"], + "c++": ["cpp"], } STATUS_ACTIONS = { "retry": "Failed to retrieve data. Retrying in ", "invalid": "The server was unable to process the request.", - "unauthorized": "The server did not accept the credentials.\n" \ - "See: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token\n" # - "Maybe you did not give enough scopes?", + "unauthorized": "The server did not accept the credentials.\n" + "See: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token\n" # + "Maybe you did not give enough scopes?", "not_found": "The server indicated no data was found.", "unsupported": "The request is not supported.", "unknown": "An unknown error occurred.", @@ -291,14 +291,14 @@ def search_github_trending( if language.lower() in invalid_lang_name: language = alternate_lang_name break - + try: gtrending_repo_list += gtrending.fetch_repos( language, spoken_language, since ) except: secho( - f"Invalid language \"{language}\" passed...", + f'Invalid language "{language}" passed...', fg="bright_red", ) return None diff --git a/tests/test_cli.py b/tests/test_cli.py index 9a93fdc..4603f53 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,7 @@ from random import randint from sys import maxsize from time import time + # import re import os import shutil @@ -40,8 +41,8 @@ def test_auth(self, auth): # def test_no_auth(self): # """Test without --auth""" # result = self.cli_result(auth="") - # Testing rich logger output doesn't work - # self.assertions(result, in_stderr=("DEBUG: auth: off",)) + # Testing rich logger output doesn't work + # self.assertions(result, in_stderr=("DEBUG: auth: off",)) # def test_incorrect_auth(self): # """Test incorrect credentials provided to --auth""" @@ -292,7 +293,6 @@ def cli_result( if nop: cli_params.append("--nop") - return runner.invoke(cli, cli_params) if cli_params else runner.invoke(cli) def assertions( diff --git a/tests/test_search.py b/tests/test_search.py index 49393ac..bd8f5ad 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -145,6 +145,7 @@ def test_spoken_language(): assert repo["full_name"].count("/") >= 1 assert repo["html_url"] == "https://github.com/" + repo["full_name"] + @pytest.mark.xfail() def test_date_range(): """Test search by date range"""