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 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
tests: tests on ci branch
  • Loading branch information
Marko MITROVIC committed Dec 10, 2024
commit 5a10e973020732aada7f80c2b37ac3eee4151c31
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ __pycache__/
.venv
venv

.env

.DS_Store
.AppleDouble
.LSOverride
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
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
@@ -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)
94 changes: 73 additions & 21 deletions github_tests_validator_app/bin/github_repo_validation.py
Original file line number Diff line number Diff line change
@@ -66,6 +66,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":
@@ -75,6 +76,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


@@ -84,26 +87,53 @@ def validate_github_repo(
payload: Dict[str, Any],
event: str,
) -> None:

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


# Fetch the test results JSON from GitHub Actions artifact
tests_results_json = user_github_connector.get_tests_results_json()
if tests_results_json is None:
logging.error("Validation failed due to missing or invalid test results artifact.")
tests_passed = False
failed_tests_summary = "No test results found."
else:
# Determine if tests have failed and prepare a summary
tests_failed = tests_results_json.get("tests_failed", 0)
tests_passed = tests_failed == 0
failed_tests_summary = ""
if tests_failed > 0:
failed_tests = tests_results_json.get("tests", [])
for test in failed_tests:
if test.get("outcome") == "failed":
failed_tests_summary += (
f"- Test: {test.get('nodeid')} failed with message: "
f"{test.get('call', {}).get('crash', {}).get('message', 'No message')}\n"
)


logging.info(f"Connecting to TEST 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,
@@ -114,6 +144,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,
@@ -128,9 +159,10 @@ 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
)
# tests_havent_changed = compare_folder(
# user_github_connector, tests_github_connector, GH_TESTS_FOLDER_NAME
# ) #TODO: Uncomment this line to enable tests FOLDER validation
tests_havent_changed = tests_passed

# Add valid repo result on Google Sheet
sql_client.add_new_repository_validation(
@@ -140,6 +172,7 @@ def validate_github_repo(
event,
default_message["valid_repository"]["workflows"][str(workflows_havent_changed)],
)

sql_client.add_new_repository_validation(
user_github_connector.user_data,
tests_havent_changed,
@@ -154,33 +187,52 @@ def validate_github_repo(
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)
pull_request = user_github_connector.repo.get_pull(number=payload["pull_request"]["number"])
# Create a Check Run with detailed test results in case of failure
user_github_connector.repo.create_check_run(
name=tests_message,
name="Validation Tests Result",
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=tests_conclusion,
output={
"title": "Validation Tests Result",
"summary": tests_message,
"text": failed_tests_summary if not tests_havent_changed else "",
}
)
user_github_connector.repo.create_check_run(
name=workflows_message,
name="Workflow Validation",
head_sha=payload["pull_request"]["head"]["sha"],
status="completed",
conclusion=workflows_conclusion,
output={
"title": "Workflow Validation Result",
"summary": workflows_message,
}
)
issue.create_comment(workflows_message)
pull_request.create_issue_comment(tests_message)
pull_request.create_issue_comment(workflows_message)
elif event == "pusher":
user_github_connector.repo.create_check_run(
name=tests_message,
name="Validation Tests Result",
head_sha=payload["after"],
status="completed",
conclusion=tests_conclusion,
output={
"title": "Validation Tests Result",
"summary": tests_message,
"text": failed_tests_summary if not tests_havent_changed else "",
}
)
user_github_connector.repo.create_check_run(
name=workflows_message,
name="Workflow Validation",
head_sha=payload["after"],
status="completed",
conclusion=workflows_conclusion,
output={
"title": "Workflow Validation Result",
"summary": workflows_message,
}
)
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
11 changes: 6 additions & 5 deletions github_tests_validator_app/config.py
Original file line number Diff line number Diff line change
@@ -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"
@@ -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": "All tests in the `validation_tests` folder passed successfully!",
"False": "Some tests in the `validation_tests` folder failed:\n{failed_tests_summary}",
},
"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.",
},
},
Loading