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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -295,7 +294,6 @@ x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
hooks:
- id: isort
name: isort
entry: poetry run isort --settings-path pyproject.toml
entry: poetry run isort --settings-path pyproject.toml .
types: [python]
language: system
stages: [commit, push]
Expand All @@ -28,13 +28,13 @@ repos:
stages: [commit, push]
- id: black
name: black
entry: poetry run black --config pyproject.toml
entry: poetry run black --config pyproject.toml .
types: [python]
language: system
stages: [commit, push]
- id: mypy
name: mypy
entry: poetry run mypy
entry: poetry run mypy .
require_serial: true
types: [python]
language: system
Expand Down
File renamed without changes.
67 changes: 67 additions & 0 deletions github_tests_validator_app/bin/github_event_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Any, Dict

import logging

from github_tests_validator_app.bin.github_repo_validation import (
get_event,
get_student_github_connector,
github_repo_validation,
)
from github_tests_validator_app.bin.student_challenge_results_validation import (
send_student_challenge_results,
)
from github_tests_validator_app.lib.connectors.google_sheet_connector import GSheet
from github_tests_validator_app.lib.users import GitHubUser
from github_tests_validator_app.lib.utils import init_github_user_from_github_event

process = {
"pull_request": github_repo_validation,
"pusher": github_repo_validation,
"workflow_job": send_student_challenge_results,
}


def validator(payload: Dict[str, Any]) -> Any:
"""
Validator function

Args:
payload Dict[str, Any]: information of new event

Returns:
None: Return nothing
"""
# Get event
event = get_event(payload)
if (
not event
or (event == "pull_request" and payload["action"] not in ["reopened", "opened"])
or (event == "workflow_job" and payload["action"] not in ["completed"])
):
return

# Init Google Sheet
gsheet = GSheet()

# Init GitHubUser
student_user = init_github_user_from_github_event(payload)
if not isinstance(student_user, GitHubUser):
# Logging
return

# Add user on Google Sheet
gsheet.add_new_user_on_sheet(student_user)

# Check valid repo
student_github_connector = get_student_github_connector(student_user, payload)
if not student_github_connector:
gsheet.add_new_repo_valid_result(
student_user,
False,
"[ERROR]: cannot get the student github repository.",
)
logging.error("[ERROR]: cannot get the student github repository.")
return

logging.info(f"Begin {event} process...")
process[event](student_github_connector, gsheet, payload)
124 changes: 124 additions & 0 deletions github_tests_validator_app/bin/github_repo_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from typing import Any, Dict, Union

import logging

from github import ContentFile
from github_tests_validator_app.config.config import (
GH_SOLUTION_OWNER,
GH_SOLUTION_REPO_NAME,
GH_SOLUTION_TESTS_ACCESS_TOKEN,
GH_TESTS_FOLDER_NAME,
commit_sha_path,
default_message,
)
from github_tests_validator_app.lib.connectors.github_connector import GitHubConnector
from github_tests_validator_app.lib.connectors.google_sheet_connector import GSheet
from github_tests_validator_app.lib.users import GitHubUser


def get_event(payload: Dict[str, Any]) -> Any:
for event in commit_sha_path:
if event in payload:
return event
return None


def get_student_branch(payload: Dict[str, Any], trigger: Union[str, None] = None) -> Any:
trigger = get_event(payload) if not trigger else trigger
if not trigger:
# Log error
# FIXME
# Archive the payload
# FIXME
logging.error("Couldn't find the student branch, maybe the trigger is not managed")
return None

path = commit_sha_path[trigger].copy()
branch = payload
while path:

try:
branch = branch[path.pop(0)]
except KeyError as key_err:
logging.error(key_err)
return None
except Exception as err:
logging.error(err)
return None

return branch


def get_student_github_connector(
student: GitHubUser, payload: Dict[str, Any]
) -> Union[GitHubConnector, None]:

if not student:
return None

github_student_branch = get_student_branch(payload)
if github_student_branch is None:
return None

repo_name = payload["repository"]["name"]
student.get_access_token(repo_name)
return GitHubConnector(student, repo_name, github_student_branch)


def compare_tests_folder(student_github: GitHubConnector, solution_repo: GitHubConnector) -> Any:

student_contents = student_github.repo.get_contents(
GH_TESTS_FOLDER_NAME, ref=student_github.BRANCH_NAME
)

if (
isinstance(student_contents, ContentFile.ContentFile)
and student_contents.type == "submodule"
):
solution_last_commit = solution_repo.get_last_hash_commit()
student_tests_commit = student_contents.sha
return solution_last_commit == student_tests_commit

student_hash_tests = student_github.get_tests_hash(GH_TESTS_FOLDER_NAME)
solution_hash_tests = solution_repo.get_tests_hash(GH_TESTS_FOLDER_NAME)
return student_hash_tests == solution_hash_tests


def github_repo_validation(
student_github_connector: GitHubConnector, gsheet: GSheet, payload: Dict[str, Any]
) -> None:

solution_user = GitHubUser(
LOGIN=str(GH_SOLUTION_OWNER), ACCESS_TOKEN=GH_SOLUTION_TESTS_ACCESS_TOKEN
)
solution_github_connector = GitHubConnector(solution_user, GH_SOLUTION_REPO_NAME, "main")
if not solution_github_connector:
gsheet.add_new_repo_valid_result(
solution_user,
False,
"[ERROR]: cannot get the solution github repository.",
)
logging.error("[ERROR]: cannot get the solution github repository.")
return

tests_havent_changed = compare_tests_folder(student_github_connector, solution_github_connector)

# Add valid repo result on Google Sheet
gsheet.add_new_repo_valid_result(
student_github_connector.user,
tests_havent_changed,
default_message["valid_repository"][str(tests_havent_changed)],
)

# Update Pull Request
if "pull_request" in payload:
issue = student_github_connector.repo.get_issue(number=payload["pull_request"]["number"])
message = default_message["valid_repository"][str(tests_havent_changed)]
issue.create_comment(message)
conclusion = "success" if tests_havent_changed else "failure"
student_github_connector.repo.create_check_run(
name=message,
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=conclusion,
)
Loading