Skip to content

Commit

Permalink
feat: Update GitHub functions (#368)
Browse files Browse the repository at this point in the history
* refactor: replace pygithub by githubkit for async HTTP requests

* refactor: move to AsyncTxnCtx in Background Tasks and fix Github export

* refactor: refactor edit_project controller

* feat: export to Github from correct SHA

* refactor: move file_cleanup closer to unparser

* fix: run dump_taxonomy in thread_pool

* fix: raise 422 error if project not from github

* fix: linting

* tooling: Update isort and flake8 settings

* fix: linting

* feat: create new commit when exporting an exported taxonomy

* refactor: refactor ExportTaxonomyToGithub

* chore: update frontend messages

* test: remove flaky tests

* test: fix failing test

* fix: fix bnrach uniqueness for upload case

* fix: delete unused file

* fix: resolve comments
  • Loading branch information
eric-nguyen-cs authored Feb 1, 2024
1 parent fdde28c commit 3fefb1d
Show file tree
Hide file tree
Showing 16 changed files with 487 additions and 690 deletions.
51 changes: 14 additions & 37 deletions backend/editor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
Taxonomy Editor Backend API
"""
import logging
import os

# Required imports
# ------------------------------------------------------------------------------------#
from datetime import datetime
from enum import Enum
from typing import Optional

# FastAPI
Expand All @@ -29,13 +27,17 @@

# DB helper imports
from . import graph_db

# Controller imports
from .controllers.project_controller import edit_project
from .entries import TaxonomyGraph

# Custom exceptions
from .exceptions import GithubBranchExistsError, GithubUploadError

# Data model imports
from .models import Footer, Header
from .models.node_models import Footer, Header
from .models.project_models import ProjectEdit, ProjectStatus

# -----------------------------------------------------------------------------------#

Expand Down Expand Up @@ -103,27 +105,6 @@ def check_single(id):
raise HTTPException(status_code=500, detail="Multiple entries found")


def file_cleanup(filepath):
"""
Helper function to delete a taxonomy file from local storage
"""
try:
os.remove(filepath)
except FileNotFoundError:
log.warn(f"Taxonomy file {filepath} not found for deletion")


class StatusFilter(str, Enum):
"""
Enum for project status filter
"""

OPEN = "OPEN"
CLOSED = "CLOSED"
LOADING = "LOADING"
FAILED = "FAILED"


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
reformatted_errors = []
Expand Down Expand Up @@ -168,7 +149,7 @@ async def pong(response: Response):


@app.get("/projects")
async def list_all_projects(response: Response, status: Optional[StatusFilter] = None):
async def list_all_projects(response: Response, status: Optional[ProjectStatus] = None):
"""
List projects created in the Taxonomy Editor with a status filter
"""
Expand All @@ -180,13 +161,13 @@ async def list_all_projects(response: Response, status: Optional[StatusFilter] =

@app.get("{taxonomy_name}/{branch}/set-project-status")
async def set_project_status(
response: Response, branch: str, taxonomy_name: str, status: Optional[StatusFilter] = None
response: Response, branch: str, taxonomy_name: str, status: Optional[ProjectStatus] = None
):
"""
Set the status of a Taxonomy Editor project
"""
taxonomy = TaxonomyGraph(branch, taxonomy_name)
result = await taxonomy.set_project_status(status)
result = await edit_project(taxonomy.project_name, ProjectEdit(status=status))
return result


Expand Down Expand Up @@ -343,10 +324,8 @@ async def export_to_text_file(
response: Response, branch: str, taxonomy_name: str, background_tasks: BackgroundTasks
):
taxonomy = TaxonomyGraph(branch, taxonomy_name)
file = await taxonomy.file_export()
file = await taxonomy.file_export(background_tasks)

# Add a background task for removing exported taxonomy file
background_tasks.add_task(file_cleanup, file)
return FileResponse(file)


Expand All @@ -356,9 +335,7 @@ async def export_to_github(
):
taxonomy = TaxonomyGraph(branch, taxonomy_name)
try:
url, file = await taxonomy.github_export()
# Add a background task for removing exported taxonomy file
background_tasks.add_task(file_cleanup, file)
url = await taxonomy.github_export(background_tasks)
return url

except GithubBranchExistsError:
Expand Down Expand Up @@ -387,10 +364,10 @@ async def import_from_github(
raise HTTPException(status_code=422, detail="branch_name: Enter a valid branch name!")
if await taxonomy.does_project_exist():
raise HTTPException(status_code=409, detail="Project already exists!")
if not await taxonomy.is_branch_unique():
if not await taxonomy.is_branch_unique(from_github=True):
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

status = await taxonomy.import_from_github(description, background_tasks)
status = await taxonomy.import_taxonomy(description, background_tasks)
return status


Expand All @@ -410,10 +387,10 @@ async def upload_taxonomy(
raise HTTPException(status_code=422, detail="branch_name: Enter a valid branch name!")
if await taxonomy.does_project_exist():
raise HTTPException(status_code=409, detail="Project already exists!")
if not await taxonomy.is_branch_unique():
if not await taxonomy.is_branch_unique(from_github=False):
raise HTTPException(status_code=409, detail="branch_name: Branch name should be unique!")

result = await taxonomy.import_from_github(description, background_tasks, file)
result = await taxonomy.import_taxonomy(description, background_tasks, file)

return result

Expand Down
41 changes: 41 additions & 0 deletions backend/editor/controllers/project_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ..graph_db import get_current_transaction
from ..models.project_models import Project, ProjectCreate, ProjectEdit


async def get_project(project_id: str) -> Project:
"""
Get project by id
"""
query = """
MATCH (p:PROJECT {id: $project_id})
RETURN p
"""
params = {"project_id": project_id}
result = await get_current_transaction().run(query, params)
return Project(**(await result.single())["p"])


async def create_project(project: ProjectCreate):
"""
Create project
"""
query = """
CREATE (p:PROJECT $project) SET p.created_at = datetime()
"""
params = {"project": project.model_dump()}
await get_current_transaction().run(query, params)


async def edit_project(project_id: str, project_edit: ProjectEdit):
"""
Edit project
"""
query = """
MATCH (p:PROJECT {id: $project_id})
SET p += $project_edit
"""
params = {
"project_id": project_id,
"project_edit": project_edit.model_dump(exclude_unset=True),
}
await get_current_transaction().run(query, params)
Loading

0 comments on commit 3fefb1d

Please sign in to comment.