transfer from monorepo

This commit is contained in:
Laura Abro
2025-04-24 10:24:42 -03:00
parent a2175785d5
commit 1c6fc5540b
95 changed files with 7110 additions and 0 deletions

View File

@ -0,0 +1,62 @@
from flask import Blueprint, jsonify, request
from src.server.services.github_service import verify_pr_ownership
from src.server.services.audit_service import audit_repo
import logging
logger = logging.getLogger(__name__)
bp = Blueprint("audit", __name__)
@bp.post("/audit/<round_number>")
def audit_submission(round_number: int):
logger.info("Auditing submission")
data = request.get_json()
submission = data.get("submission")
if not submission:
return jsonify({"error": "Missing submission"}), 400
# submission_round_number = submission.get("roundNumber")
task_id = submission.get("taskId")
pr_url = submission.get("prUrl")
github_username = submission.get("githubUsername")
# Extract repo owner and name from PR URL
try:
pr_url_parts = pr_url.split('github.com/')[1].split('/')
repo_owner = pr_url_parts[0]
repo_name = pr_url_parts[1]
except (IndexError, AttributeError):
return jsonify({"error": "Invalid PR URL format"}), 400
print(f"Repo owner: {repo_owner}, Repo name: {repo_name}")
# This is commented out because the round number might be different due to we put the audit logic in the distribution part
# if int(round_number) != submission_round_number:
# return jsonify({"error": "Round number mismatch"}), 400
if (
not task_id
or not pr_url
or not github_username
or not repo_owner
or not repo_name
):
return jsonify({"error": "Missing submission data"}), 400
is_valid = verify_pr_ownership(
pr_url=pr_url,
expected_username=github_username,
expected_owner=repo_owner,
expected_repo=repo_name,
)
if not is_valid:
return jsonify(False)
try:
is_approved = audit_repo(pr_url)
return jsonify(is_approved), 200
except Exception as e:
logger.error(f"Error auditing PR: {str(e)}")
return jsonify(True), 200

View File

@ -0,0 +1,14 @@
from flask import Blueprint, jsonify
from prometheus_swarm.database import get_db
import logging
logger = logging.getLogger(__name__)
bp = Blueprint("healthz", __name__)
@bp.post("/healthz")
def healthz():
# Test database connection
_ = get_db()
return jsonify({"status": "ok"})

View File

@ -0,0 +1,54 @@
from flask import Blueprint, jsonify, request
from src.server.services import repo_summary_service
bp = Blueprint("repo_summary", __name__)
@bp.post("/repo_summary/<round_number>")
def start_task(round_number):
logger = repo_summary_service.logger
logger.info(f"Task started for round: {round_number}")
data = request.get_json()
logger.info(f"Task data: {data}")
required_fields = [
"taskId",
"round_number",
"repo_url"
]
if any(data.get(field) is None for field in required_fields):
return jsonify({"error": "Missing data"}), 401
result = repo_summary_service.handle_task_creation(
task_id=data["taskId"],
round_number=int(round_number),
repo_url=data["repo_url"],
)
return result
if __name__ == "__main__":
from flask import Flask
# Create a Flask app instance
app = Flask(__name__)
app.register_blueprint(bp)
# Test data
test_data = {
"taskId": "fake",
"round_number": "1",
"repo_url": "https://github.com/koii-network/docs"
}
# Set up test context
with app.test_client() as client:
# Make a POST request to the endpoint
response = client.post(
"/repo_summary/1",
json=test_data
)
# Print the response
print(f"Status Code: {response.status_code}")
print(f"Response: {response.get_json()}")

View File

@ -0,0 +1,39 @@
from prometheus_swarm.utils.logging import log_key_value
from flask import Blueprint, jsonify, request
from src.server.services import star_service
bp = Blueprint("star", __name__)
@bp.post("/star/<round_number>")
def start_task(round_number):
logger = star_service.logger
logger.info(f"Task started for round: {round_number}")
data = request.get_json()
logger.info(f"Task data: {data}")
required_fields = [
"taskId",
"round_number",
"github_urls",
]
if any(data.get(field) is None for field in required_fields):
return jsonify({"error": "Missing data"}), 401
try:
# Log incoming data
print("Received data:", data)
print("Round number:", round_number)
result = star_service.handle_star_task(
task_id=data["taskId"],
round_number=int(round_number),
github_urls=data["github_urls"],
)
return result
except Exception as e:
print(f"Error in star endpoint: {str(e)}")
print(f"Error type: {type(e)}")
import traceback
print(f"Traceback: {traceback.format_exc()}")
return jsonify({'error': str(e)}), 500

View File

@ -0,0 +1,38 @@
from flask import Blueprint, jsonify
from prometheus_swarm.database import get_db
from src.dababase.models import Submission
import logging
import os
logger = logging.getLogger(__name__)
bp = Blueprint("submission", __name__)
@bp.get("/submission/<roundNumber>")
def fetch_submission(roundNumber):
logger.info(f"Fetching submission for round: {roundNumber}")
db = get_db()
submission = (
db.query(Submission)
.filter(
Submission.round_number == int(roundNumber),
)
.first()
)
logger.info(f"Submission: {submission}")
logger.info(f"Submission: {submission}")
if submission:
github_username = os.getenv("GITHUB_USERNAME")
return jsonify(
{
"taskId": submission.task_id,
"roundNumber": submission.round_number,
"status": submission.status,
"prUrl": submission.pr_url,
"githubUsername": github_username,
}
)
else:
return jsonify({"error": "Submission not found"}), 409