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
95 changes: 81 additions & 14 deletions github_tests_validator_app/bin/github_event_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
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.config.config import (
GDRIVE_MAIN_DIRECTORY_NAME,
GDRIVE_SUMMARY_SPREADSHEET,
GSHEET_DETAILS_SPREADSHEET,
USER_SHARE,
)
from github_tests_validator_app.lib.connectors.gddrive import GoogleDriveConnector
from github_tests_validator_app.lib.connectors.gsheet import GSheetConnector
from github_tests_validator_app.lib.models.file import GSheetDetailFile, GSheetFile, WorkSheetFile
from github_tests_validator_app.lib.models.users import GitHubUser
from github_tests_validator_app.lib.utils import init_github_user_from_github_event

process = {
Expand All @@ -21,7 +29,56 @@
}


def validator(payload: Dict[str, Any]) -> Any:
def handle_process(payload: Dict[str, Any]) -> str:
# 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"]
or payload["workflow_job"]["conclusion"] != "success"
)
)
):
return ""
return event


def init_gsheet_file(
google_drive: GoogleDriveConnector, info: Dict[str, Any], parent_id: str, user_share: str
) -> GSheetFile:

gsheet = google_drive.get_gsheet(info["name"], parent_id, user_share)

list_worksheets = [
WorkSheetFile(NAME=worksheet["name"], HEADERS=worksheet["headers"])
for _, worksheet in info["worksheets"].items()
]
return GSheetFile(
NAME=info["name"],
MIMETYPE=gsheet.get("mimeType", ""),
ID=gsheet.get("id", ""),
WORKSHEETS=list_worksheets,
)


def init_gsheet_detail_file(
google_drive: GoogleDriveConnector, info: Dict[str, Any], parent_id: str, user_share: str
) -> GSheetDetailFile:

gsheet = google_drive.get_gsheet(info["name"], parent_id, user_share)
return GSheetDetailFile(
NAME=info["name"],
MIMETYPE=gsheet.get("mimeType", ""),
ID=gsheet.get("id", ""),
HEADERS=info["headers"],
)


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

Expand All @@ -31,25 +88,33 @@ def validator(payload: Dict[str, Any]) -> Any:
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"])
):

event = handle_process(payload)
if not event:
return

# Init Google Sheet
gsheet = GSheet()
# Init Google Drive connector and folders
googe_drive = GoogleDriveConnector()
folder_school_of_data = googe_drive.get_gdrive_folder(GDRIVE_MAIN_DIRECTORY_NAME, USER_SHARE)

# Init Google sheets
gsheet_summary_file = init_gsheet_file(
googe_drive, GDRIVE_SUMMARY_SPREADSHEET, folder_school_of_data["id"], USER_SHARE
)
gsheet_details_file = init_gsheet_detail_file(
googe_drive, GSHEET_DETAILS_SPREADSHEET, folder_school_of_data["id"], USER_SHARE
)

# Init Google sheet connector and worksheets
gsheet = GSheetConnector(gsheet_summary_file, gsheet_details_file)

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

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

# Check valid repo
Expand All @@ -63,5 +128,7 @@ def validator(payload: Dict[str, Any]) -> Any:
logging.error("[ERROR]: cannot get the student github repository.")
return

logging.info(f"Begin {event} process...")
logging.info(f'Begin process: "{event}"...')
# Run the process
process[event](student_github_connector, gsheet, payload)
logging.info(f'End of process: "{event}".')
32 changes: 19 additions & 13 deletions github_tests_validator_app/bin/github_repo_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Union
from typing import Any, Dict, List, Union

import logging

Expand All @@ -8,19 +8,24 @@
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
from github_tests_validator_app.lib.connectors.gsheet import GSheetConnector
from github_tests_validator_app.lib.models.users import GitHubUser

commit_sha_path: Dict[str, List[str]] = {
"pull_request": ["pull_request", "head", "ref"],
"pusher": ["ref"],
"workflow_job": [],
}

def get_event(payload: Dict[str, Any]) -> Any:

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


def get_student_branch(payload: Dict[str, Any], trigger: Union[str, None] = None) -> Any:
Expand Down Expand Up @@ -60,9 +65,7 @@ def get_student_github_connector(
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)
return GitHubConnector(student, payload["repository"]["name"], github_student_branch)


def compare_tests_folder(student_github: GitHubConnector, solution_repo: GitHubConnector) -> Any:
Expand All @@ -85,13 +88,16 @@ def compare_tests_folder(student_github: GitHubConnector, solution_repo: GitHubC


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

solution_user = GitHubUser(
LOGIN=str(GH_SOLUTION_OWNER), ACCESS_TOKEN=GH_SOLUTION_TESTS_ACCESS_TOKEN
solution_user = GitHubUser(LOGIN=str(GH_SOLUTION_OWNER))
solution_github_connector = GitHubConnector(
user=solution_user,
repo_name=GH_SOLUTION_REPO_NAME,
branch_name="main",
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,
Expand Down
Loading