diff --git a/github_tests_validator_app/constants.py b/github_tests_validator_app/constants.py index 8bd192d..61130d4 100644 --- a/github_tests_validator_app/constants.py +++ b/github_tests_validator_app/constants.py @@ -1,4 +1,10 @@ +from typing import cast + import os -APP_ID = os.getenv("GH_APP_ID") -APP_KEY = os.getenv("GH_APP_KEY") +APP_ID = cast(str, os.getenv("GH_APP_ID")) +APP_KEY = cast(str, os.getenv("GH_APP_KEY")) +SOLUTION_TESTS_ACCESS_TOKEN = cast(str, os.getenv("SOLUTION_TESTS_ACCESS_TOKEN")) +SOLUTION_OWNER = "artefactory-fr" +SOLUTION_REPO_NAME = "school_of_data_tests" +TESTS_FOLDER_NAME = "tests" diff --git a/github_tests_validator_app/utils.py b/github_tests_validator_app/utils.py index 18b93f7..949b81b 100644 --- a/github_tests_validator_app/utils.py +++ b/github_tests_validator_app/utils.py @@ -1,10 +1,63 @@ +from typing import Any, List + import hashlib +from github import ContentFile, Github, Repository +from github_tests_validator_app.constants import TESTS_FOLDER_NAME + -def get_hash_files(contents): +def get_hash_files(contents: List[ContentFile.ContentFile]) -> str: hash_sum = "" for content in contents: hash_sum += content.sha hash = hashlib.sha256() hash.update(hash_sum.encode()) return str(hash.hexdigest()) + + +def get_tests_hash(repo: Repository.Repository) -> str: + contents = repo.get_contents(TESTS_FOLDER_NAME) + files_content = get_files_content(contents, repo) + hash = get_hash_files(files_content) + return hash + + +def get_files_content(contents: Any, repo: Repository.Repository) -> List[ContentFile.ContentFile]: + files_content = [] + while contents: + file_content = contents.pop(0) + if file_content.type == "dir": + contents.extend(repo.get_contents(file_content.path)) + else: + files_content.append(file_content) + return files_content + + +def get_repo(token: str, owner: str, repo_name: str) -> Repository.Repository: + git_connection = Github(login_or_token=token) + repo = git_connection.get_repo(f"{owner}/{repo_name}") + return repo + + +def get_last_hash_commit(repo: Repository.Repository, branch_name: str) -> str: + branch = repo.get_branch(branch_name) + return branch.commit.sha + + +def compare_tests_folder( + student_repo: Repository.Repository, solution_repo: Repository.Repository +) -> bool: + student_contents = student_repo.get_contents(TESTS_FOLDER_NAME) + + if ( + isinstance(student_contents, ContentFile.ContentFile) + and student_contents.type == "submodule" + ): + solution_last_commit = get_last_hash_commit(solution_repo, "main") + student_tests_commit = student_contents.sha + return solution_last_commit == student_tests_commit + + student_hash_tests = get_tests_hash(student_repo) + solution_hash_tests = get_tests_hash(solution_repo) + + return student_hash_tests == solution_hash_tests diff --git a/server.py b/server.py index 1c199ef..59c30ea 100644 --- a/server.py +++ b/server.py @@ -1,8 +1,15 @@ import uvicorn from fastapi import FastAPI, Request -from github import Github, GithubIntegration -from github_tests_validator_app.constants import APP_ID, APP_KEY -from github_tests_validator_app.utils import get_hash_files +from github import GithubIntegration +from github_tests_validator_app.constants import ( + APP_ID, + APP_KEY, + SOLUTION_OWNER, + SOLUTION_REPO_NAME, + SOLUTION_TESTS_ACCESS_TOKEN, + TESTS_FOLDER_NAME, +) +from github_tests_validator_app.utils import compare_tests_folder, get_repo app = FastAPI() @@ -21,17 +28,16 @@ async def main(request: Request) -> None: owner = payload["repository"]["owner"]["login"] repo_name = payload["repository"]["name"] + token = git_integration.get_access_token( + git_integration.get_installation(owner, repo_name).id + ).token + + student_repo = get_repo(token, owner, repo_name) + solution_repo = get_repo(SOLUTION_TESTS_ACCESS_TOKEN, SOLUTION_OWNER, SOLUTION_REPO_NAME) - git_connection = Github( - login_or_token=git_integration.get_access_token( - git_integration.get_installation(owner, repo_name).id - ).token - ) + tests_havent_changed = compare_tests_folder(student_repo, solution_repo) + print(tests_havent_changed) - repo = git_connection.get_repo(f"{owner}/{repo_name}") - contents = repo.get_contents("tests") - hashes = get_hash_files(contents) - print(hashes) return diff --git a/tests/units/basic_test.py b/tests/units/basic_test.py index 8ff336f..f8dbcf1 100644 --- a/tests/units/basic_test.py +++ b/tests/units/basic_test.py @@ -2,4 +2,4 @@ def test_length_hash(): - assert len(get_hash_files("")) == 64 + assert len(get_hash_files([])) == 64