diff --git a/shared/torngit/github.py b/shared/torngit/github.py index bf0d645cc..49af277b2 100644 --- a/shared/torngit/github.py +++ b/shared/torngit/github.py @@ -4,7 +4,6 @@ import logging import os from base64 import b64decode -from dataclasses import dataclass from typing import Dict, List, Optional from urllib.parse import parse_qs, urlencode @@ -38,12 +37,6 @@ METRICS_PREFIX = "services.torngit.github" -@dataclass -class RepoWithLanguages: - name: str - languages: List[str] - - class GitHubGraphQLQueries(object): _queries = dict( REPO_TOTALCOUNT=""" @@ -1621,7 +1614,7 @@ async def get_check_suites(self, git_sha, token=None): ) return res - # TODO: deprecated - favour the get_languages_graphql() method instead + # TODO: deprecated - favour the get_repos_with_languages_graphql() method instead async def get_repo_languages(self, token=None) -> List[str]: """ Gets the languages belonging to this repository. @@ -1636,21 +1629,21 @@ async def get_repo_languages(self, token=None) -> List[str]: ) return list(k.lower() for k in res.keys()) - async def get_languages_graphql( + async def get_repos_with_languages_graphql( self, owner_username: str, token=None, first=100 - ) -> List[RepoWithLanguages]: + ) -> dict[str, List[str]]: """ Gets the languages belonging to repositories of a specific owner. Reference: https://docs.github.com/en/graphql/reference/objects#repository Returns: - List[RepoWithLanguages]: A list of repositories and their languages names + dict[str, str]: A dictionary with repo_name: [languages] """ token = self.get_token_by_type_if_none(token, TokenType.read) # Initially set to none and true endCursor = None hasNextPage = True - all_repositories = [] + all_repositories = {} async with self.get_client() as client: while hasNextPage: @@ -1683,11 +1676,7 @@ async def get_languages_graphql( for language in languages: res_languages.append(language["node"]["name"]) - all_repositories.append( - RepoWithLanguages( - name=repo["name"], languages=res_languages - ) - ) + all_repositories[repo["name"]] = res_languages return all_repositories diff --git a/tests/integration/cassetes/test_github/TestGithubTestCase/test_get_languages_graphql.yaml b/tests/integration/cassetes/test_github/TestGithubTestCase/test_get_repo_with_languages_graphql.yaml similarity index 96% rename from tests/integration/cassetes/test_github/TestGithubTestCase/test_get_languages_graphql.yaml rename to tests/integration/cassetes/test_github/TestGithubTestCase/test_get_repo_with_languages_graphql.yaml index 59a973f45..f961a7f41 100644 --- a/tests/integration/cassetes/test_github/TestGithubTestCase/test_get_languages_graphql.yaml +++ b/tests/integration/cassetes/test_github/TestGithubTestCase/test_get_repo_with_languages_graphql.yaml @@ -41,7 +41,7 @@ interactions: Content-Type: - application/json; charset=utf-8 Date: - - Wed, 06 Mar 2024 20:22:24 GMT + - Fri, 08 Mar 2024 00:59:29 GMT Referrer-Policy: - origin-when-cross-origin, strict-origin-when-cross-origin Server: @@ -61,19 +61,19 @@ interactions: X-GitHub-Media-Type: - github.v4 X-GitHub-Request-Id: - - DB8A:54C4:313396:5D9263:65E8D080 + - DC4E:4ECA:91EE42:115CF24:65EA62F1 X-OAuth-Scopes: - admin:enterprise, admin:gpg_key, admin:org X-RateLimit-Limit: - '5000' X-RateLimit-Remaining: - - '4592' + - '4472' X-RateLimit-Reset: - - '1709758667' + - '1709860075' X-RateLimit-Resource: - graphql X-RateLimit-Used: - - '408' + - '528' X-XSS-Protection: - '0' http_version: HTTP/1.1 diff --git a/tests/integration/test_github.py b/tests/integration/test_github.py index e3f768204..67674fcf1 100644 --- a/tests/integration/test_github.py +++ b/tests/integration/test_github.py @@ -6,7 +6,7 @@ TorngitObjectNotFoundError, TorngitRepoNotFoundError, ) -from shared.torngit.github import Github, RepoWithLanguages +from shared.torngit.github import Github # This is a fake key fake_private_key = """-----BEGIN RSA PRIVATE KEY----- @@ -963,19 +963,17 @@ async def test_get_repository(self, valid_handler, codecov_vcr): assert res == expected_result @pytest.mark.asyncio - async def test_get_languages_graphql(self, valid_handler, codecov_vcr): - expected_result = [ - RepoWithLanguages( - name="another-test", languages=["JavaScript", "HTML", "CSS"] - ), - RepoWithLanguages( - name="new-test-repo", languages=["HTML", "CSS", "JavaScript"] - ), - RepoWithLanguages(name="test-no-languages", languages=[]), - ] + async def test_get_repo_with_languages_graphql(self, valid_handler, codecov_vcr): + expected_result = { + "another-test": ["JavaScript", "HTML", "CSS"], + "new-test-repo": ["HTML", "CSS", "JavaScript"], + "test-no-languages": [], + } owner_username = "adrian-codecov" - res = await valid_handler.get_languages_graphql(owner_username=owner_username) + res = await valid_handler.get_repos_with_languages_graphql( + owner_username=owner_username + ) assert res == expected_result @pytest.mark.asyncio