Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1322 languages gql #148

Merged
merged 8 commits into from
Mar 8, 2024
23 changes: 6 additions & 17 deletions shared/torngit/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -38,12 +37,6 @@
METRICS_PREFIX = "services.torngit.github"


@dataclass
class RepoWithLanguages:
name: str
languages: List[str]


class GitHubGraphQLQueries(object):
_queries = dict(
REPO_TOTALCOUNT="""
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions tests/integration/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-----
Expand Down Expand Up @@ -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
Expand Down
Loading