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

Feat/db convert #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ __pycache__/
.venv
venv

.env

.DS_Store
.AppleDouble
.LSOverride
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ lint: test check-safety check-style
docker:
@echo Building docker $(IMAGE):$(VERSION) ...
docker build \
--platform=linux/amd64 \
-t $(IMAGE):$(VERSION) . \
-f ./docker/Dockerfile

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-slim-buster
FROM --platform=linux/amd64 python:3.9-slim-buster

ENV LANG=C.UTF-8 \
LC_ALL=C.UTF-8
Expand Down
6 changes: 4 additions & 2 deletions github_tests_validator_app/bin/github_event_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def run(payload: Dict[str, Any]) -> None:
return

sql_client = SQLAlchemyConnector()

sql_client.add_new_user(user_data)
try:
sql_client.add_new_user(user_data)
except Exception as e:
logging.error(f"[ERROR]: {e}")

# Check valid repo
user_github_connector = get_user_github_connector(user_data, payload)
Expand Down
141 changes: 114 additions & 27 deletions github_tests_validator_app/bin/github_repo_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
commit_ref_path,
default_message,
)
from github_tests_validator_app.lib.utils import pull_requested_test_results
from github_tests_validator_app.lib.connectors.github_client import GitHubConnector
from github_tests_validator_app.lib.connectors.sqlalchemy_client import SQLAlchemyConnector, User

Expand All @@ -25,10 +26,6 @@ def get_event(payload: Dict[str, Any]) -> str:
def get_user_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 user branch, maybe the trigger is not managed")
return None

Expand Down Expand Up @@ -66,6 +63,7 @@ def compare_folder(
user_github: GitHubConnector, solution_repo: GitHubConnector, folder: str
) -> Any:

logging.info(f"BRANCH NAME: {user_github.BRANCH_NAME}")
user_contents = user_github.repo.get_contents(folder, ref=user_github.BRANCH_NAME)

if isinstance(user_contents, ContentFile.ContentFile) and user_contents.type == "submodule":
Expand All @@ -75,6 +73,8 @@ def compare_folder(

user_hash = user_github.get_hash(folder)
solution_hash = solution_repo.get_hash(folder)
logging.info(f"user_hash = {user_hash}")
logging.info(f"solution_hash = {solution_hash}")
return user_hash == solution_hash


Expand All @@ -85,25 +85,31 @@ def validate_github_repo(
event: str,
) -> None:

logging.info(f"Connecting to repo : {GH_TESTS_REPO_NAME}")
logging.info(f"Connecting to TESTS repo : {GH_TESTS_REPO_NAME}")

if user_github_connector.repo.parent:
original_repo_name = user_github_connector.repo.parent.full_name
logging.info(f"Connecting to ORIGINAL repo : {original_repo_name}")
else:
original_repo_name = user_github_connector.repo.full_name
logging.info(f"Repository '{original_repo_name}' is not a fork, connecting to the same repository.")

tests_github_connector = GitHubConnector(
user_data=user_github_connector.user_data,
repo_name=GH_TESTS_REPO_NAME
if GH_TESTS_REPO_NAME
else user_github_connector.repo.parent.full_name,
branch_name="main",
else original_repo_name,
branch_name="feat/ci_workflow",
access_token=GH_PAT,
)

logging.info(f"Connecting to repo : {user_github_connector.repo.parent.full_name}")


original_github_connector = GitHubConnector(
user_data=user_github_connector.user_data,
repo_name=user_github_connector.repo.parent.full_name,
branch_name="main",
repo_name=original_repo_name,
branch_name="feat/ci_workflow",
access_token=GH_PAT,
)

if not tests_github_connector:
sql_client.add_new_repository_validation(
user_github_connector.user_data,
Expand All @@ -114,6 +120,7 @@ def validate_github_repo(
)
logging.error("[ERROR]: cannot get the tests github repository.")
return

if not original_github_connector:
sql_client.add_new_repository_validation(
user_github_connector.user_data,
Expand All @@ -128,18 +135,48 @@ def validate_github_repo(
workflows_havent_changed = compare_folder(
user_github_connector, original_github_connector, GH_WORKFLOWS_FOLDER_NAME
)

tests_havent_changed = compare_folder(
user_github_connector, tests_github_connector, GH_TESTS_FOLDER_NAME
)

# Add valid repo result on Google Sheet
tests_conclusion = "success" if tests_havent_changed else "failure"
tests_message = default_message["valid_repository"]["tests"][str(tests_havent_changed)]

workflows_conclusion = "success" if workflows_havent_changed else "failure"
workflows_message = default_message["valid_repository"]["workflows"][
str(workflows_havent_changed)
]

# Fetch the test results JSON from GitHub Actions artifact
pytests_results_json = user_github_connector.get_tests_results_json()

if pytests_results_json is None:
logging.error("Validation failed due to missing or invalid test results artifact.")
pytest_result_message = "No test results found."
pytest_result_conclusion = "faillure"
else:
failed_tests = pull_requested_test_results(
tests_results_json=pytests_results_json,
payload=payload,
github_event=event,
user_github_connector=user_github_connector
)
logging.info(f"failed_test : {failed_tests[1]}")
pytest_result_conclusion = "failure" if failed_tests[1] > 0 else "success"
logging.info(f"pytest_result_conclusion 01 = {pytest_result_conclusion}")

logging.info(f"pytest_result_conclusion = {pytest_result_conclusion}")


sql_client.add_new_repository_validation(
user_github_connector.user_data,
workflows_havent_changed,
payload,
event,
default_message["valid_repository"]["workflows"][str(workflows_havent_changed)],
)

sql_client.add_new_repository_validation(
user_github_connector.user_data,
tests_havent_changed,
Expand All @@ -148,39 +185,89 @@ def validate_github_repo(
default_message["valid_repository"]["tests"][str(tests_havent_changed)],
)

tests_conclusion = "success" if tests_havent_changed else "failure"
tests_message = default_message["valid_repository"]["tests"][str(tests_havent_changed)]
workflows_conclusion = "success" if workflows_havent_changed else "failure"
workflows_message = default_message["valid_repository"]["workflows"][
str(workflows_havent_changed)
]

if event == "pull_request":
issue = user_github_connector.repo.get_issue(number=payload["pull_request"]["number"])
issue.create_comment(tests_message)
# Create a Check Run with detailed test results in case of failure
user_github_connector.repo.create_check_run(
name=tests_message,
name="[Integrity] Test Folder Validation",
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=tests_conclusion,
output={
"title": "Test Folder Validation Result",
"summary": tests_message,
}
)
user_github_connector.repo.create_check_run(
name=workflows_message,
name="[Integrity] Workflow Folder Validation",
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=workflows_conclusion,
output={
"title": "Workflow Folder Validation Result",
"summary": workflows_message,
}
)
pytest_result_message = pull_requested_test_results(
tests_results_json=pytests_results_json,
payload=payload,
github_event=event,
user_github_connector=user_github_connector
)
user_github_connector.repo.create_check_run(
name="[Pytest] Pytest Result Validation",
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=pytest_result_conclusion,
output={
"title": "Pytest Validation Result",
"summary": pytest_result_message[0],
}
)
issue.create_comment(workflows_message)
elif event == "pusher":
# Check if there is already an open PR
gh_branch = payload["ref"].replace("refs/heads/", "")
gh_prs = user_github_connector.repo.get_pulls(
state="open",
head=f"{user_github_connector.repo.owner.login}:{gh_branch}"
)
if gh_prs.totalCount > 0:
gh_pr = gh_prs[0] # Get first matching PR
if gh_pr.head.sha == payload["after"]:
return

user_github_connector.repo.create_check_run(
name=tests_message,
name="[Integrity] Test Folder Validation",
head_sha=payload["after"],
status="completed",
conclusion=tests_conclusion,
output={
"title": "Test Folder Validation Result",
"summary": tests_message,
}
)
user_github_connector.repo.create_check_run(
name=workflows_message,
name="[Integrity] Workflow Folder Validation",
head_sha=payload["after"],
status="completed",
conclusion=workflows_conclusion,
output={
"title": "Workflow Folder Validation Result",
"summary": workflows_message,
}
)
pytest_result_message = pull_requested_test_results(
tests_results_json=pytests_results_json,
payload=payload,
github_event=event,
user_github_connector=user_github_connector
)
user_github_connector.repo.create_check_run(
name="[Pytest] Pytest Result Validation",
head_sha=payload["after"],
status="completed",
conclusion=pytest_result_conclusion,
output={
"title": "Pytest Validation Result",
"summary": pytest_result_message[0],
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def send_user_pytest_summaries(

# Get user artifact
artifact = get_user_artifact(user_github_connector, sql_client, all_user_artifact, payload)
logging.info(f"User artifact: {artifact}")
if not artifact:
logging.info("[ERROR]: Cannot get user artifact.")
return

# Send summary user results to Google Sheet
Expand All @@ -122,7 +124,8 @@ def send_user_pytest_summaries(

# Parsing artifact / challenge results
pytest_summaries = parsing_pytest_summaries(artifact["tests"])
# Send new detail results to Google Sheet
logging.info(f'Tests user artifact: {artifact["tests"]}')
# Send new results to Google Sheet
sql_client.add_new_pytest_detail(
repository=user_github_connector.REPO_NAME,
branch=user_github_connector.BRANCH_NAME,
Expand Down
11 changes: 6 additions & 5 deletions github_tests_validator_app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
GH_APP_ID = cast(str, os.getenv("GH_APP_ID", "")).replace("\r\n", "").replace("\r", "")
GH_APP_KEY = cast(str, os.getenv("GH_APP_KEY", ""))
GH_PAT = cast(str, os.getenv("GH_PAT", "")).replace("\r\n", "").replace("\r", "")
SQLALCHEMY_URI = cast(str, os.getenv("SQLALCHEMY_URI", "")).replace("\r\n", "").replace("\r", "")

SQLALCHEMY_URI = cast(str, os.getenv("SQLALCHEMY_URI", "")).replace("\r\n", "").replace("\r", "").replace('"', '')
GH_TESTS_REPO_NAME = (
cast(str, os.getenv("GH_TESTS_REPO_NAME", "")).replace("\r\n", "").replace("\r", "")
)
GH_TESTS_FOLDER_NAME = "tests"
GH_TESTS_FOLDER_NAME = "validation_tests"
GH_WORKFLOWS_FOLDER_NAME = ".github/workflows"
GH_API = "https://api.github.com/repos"
GH_ALL_ARTIFACT_ENDPOINT = "actions/artifacts"
Expand All @@ -48,11 +49,11 @@
default_message: Dict[str, Dict[str, Dict[str, str]]] = {
"valid_repository": {
"tests": {
"True": "Your folder `Test` is valid",
"False": "Your folder `Test` has been modified and is no longer valid.",
"True": "Your folder `.validation_tests/` is valid.",
"False": "Your folder `.validation_tests/` has been modified and is no longer valid.",
},
"workflows": {
"True": "Your folder `.github/workflows` is valid",
"True": "Your folder `.github/workflows` is valid.",
"False": "Your folder `.github/workflows` has been modified and is no longer valid.",
},
},
Expand Down
Loading
Loading