Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions github_tests_validator_app/constants.py
Original file line number Diff line number Diff line change
@@ -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"
55 changes: 54 additions & 1 deletion github_tests_validator_app/utils.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 18 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/units/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_length_hash():
assert len(get_hash_files("")) == 64
assert len(get_hash_files([])) == 64