feat: add project code generation + remove issues/milestones from nav
This commit is contained in:
@@ -139,6 +139,11 @@ def update_issue(issue_id: int, issue_update: schemas.IssueUpdate, db: Session =
|
||||
check_project_role(db, current_user.id, issue.project_id, min_role="dev")
|
||||
if not issue:
|
||||
raise HTTPException(status_code=404, detail="Issue not found")
|
||||
update_data = issue_update.model_dump(exclude_unset=True)
|
||||
if "issue_type" in update_data or issue_subtype in update_data:
|
||||
new_type = update_data.get("issue_type", issue.issue_type)
|
||||
new_subtype = update_data.get("issue_subtype", issue.issue_subtype)
|
||||
_validate_issue_type_subtype(new_type, new_subtype, require_subtype="issue_type" in update_data)
|
||||
for field, value in update_data.items():
|
||||
setattr(issue, field, value)
|
||||
db.commit()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Projects router with RBAC."""
|
||||
import re
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -11,10 +12,116 @@ from app.api.rbac import check_project_role
|
||||
|
||||
router = APIRouter(prefix="/projects", tags=["Projects"])
|
||||
|
||||
WORD_SEGMENT_RE = re.compile(r"[A-Za-z]+")
|
||||
CAMEL_RE = re.compile(r"[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+")
|
||||
|
||||
|
||||
def _split_words(name: str):
|
||||
segments = WORD_SEGMENT_RE.findall(name or '')
|
||||
words = []
|
||||
for seg in segments:
|
||||
parts = CAMEL_RE.findall(seg)
|
||||
for part in parts:
|
||||
if part.isupper() and len(part) > 1:
|
||||
words.extend(list(part))
|
||||
else:
|
||||
words.append(part)
|
||||
return words
|
||||
|
||||
|
||||
def _code_exists(db, code: str) -> bool:
|
||||
return db.query(models.Project).filter(models.Project.project_code == code).first() is not None
|
||||
|
||||
|
||||
def _next_counter(db, prefix: str, width: int) -> str:
|
||||
if width <= 0:
|
||||
return ''
|
||||
counter = db.query(models.ProjectCodeCounter).filter(models.ProjectCodeCounter.prefix == prefix).first()
|
||||
if not counter:
|
||||
counter = models.ProjectCodeCounter(prefix=prefix, next_value=0)
|
||||
db.add(counter)
|
||||
db.flush()
|
||||
value = counter.next_value
|
||||
counter.next_value += 1
|
||||
db.flush()
|
||||
return format(value, 'x').upper().zfill(width)
|
||||
|
||||
|
||||
def _generate_with_counter(db, prefix: str, width: int) -> str:
|
||||
if prefix.upper() == 'UN':
|
||||
prefix = 'UN'
|
||||
while True:
|
||||
suffix = _next_counter(db, prefix, width)
|
||||
code = (prefix + suffix).upper()
|
||||
if not _code_exists(db, code):
|
||||
return code
|
||||
|
||||
|
||||
def _generate_project_code(db, name: str) -> str:
|
||||
words = _split_words(name)
|
||||
if not words:
|
||||
return _generate_with_counter(db, 'UN', 4)
|
||||
|
||||
if len(words) == 1:
|
||||
letters = ''.join([c for c in words[0] if c.isalpha()]).upper()
|
||||
if not letters:
|
||||
return _generate_with_counter(db, 'UN', 4)
|
||||
if len(letters) >= 6:
|
||||
code = letters[:6]
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
prefix = letters
|
||||
width = 6 - len(prefix)
|
||||
return _generate_with_counter(db, prefix, width)
|
||||
|
||||
total_letters = sum(len(w) for w in words)
|
||||
if len(words) > 6:
|
||||
code = ''.join([w[0] for w in words[:6]]).upper()
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
|
||||
if total_letters < 6:
|
||||
prefix = ''.join(words).upper()
|
||||
width = 6 - len(prefix)
|
||||
return _generate_with_counter(db, prefix, width)
|
||||
|
||||
if total_letters == 6:
|
||||
code = ''.join(words).upper()
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
|
||||
word_count = len(words)
|
||||
needed = 6 - word_count
|
||||
for idx in range(word_count - 1, -1, -1):
|
||||
extra_letters = list(words[idx][1:])
|
||||
if needed > len(extra_letters):
|
||||
continue
|
||||
indices = list(range(len(extra_letters)))
|
||||
def combos(start, depth, path):
|
||||
if depth == 0:
|
||||
yield path
|
||||
return
|
||||
for i in range(start, len(indices) - depth + 1):
|
||||
yield from combos(i + 1, depth - 1, path + [indices[i]])
|
||||
for combo in combos(0, needed, []):
|
||||
pieces = []
|
||||
for wi, w in enumerate(words):
|
||||
pieces.append(w[0])
|
||||
if wi == idx:
|
||||
pieces.extend([extra_letters[i] for i in combo])
|
||||
code = ''.join(pieces)[:6].upper()
|
||||
if not _code_exists(db, code):
|
||||
return code
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
|
||||
@router.post("", response_model=schemas.ProjectResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_project(project: schemas.ProjectCreate, db: Session = Depends(get_db)):
|
||||
db_project = models.Project(**project.model_dump())
|
||||
payload = project.model_dump()
|
||||
payload["project_code"] = _generate_project_code(db, project.name)
|
||||
db_project = models.Project(**payload)
|
||||
db.add(db_project)
|
||||
db.commit()
|
||||
db.refresh(db_project)
|
||||
|
||||
27
app/main.py
27
app/main.py
@@ -45,11 +45,36 @@ app.include_router(webhooks_router)
|
||||
app.include_router(misc_router)
|
||||
app.include_router(monitor_router)
|
||||
|
||||
|
||||
# Auto schema migration for lightweight deployments
|
||||
def _migrate_schema():
|
||||
from sqlalchemy import text
|
||||
from app.core.config import SessionLocal
|
||||
db = SessionLocal()
|
||||
try:
|
||||
# issues.issue_subtype
|
||||
result = db.execute(text("SHOW COLUMNS FROM issues LIKE 'issue_subtype'")).fetchone()
|
||||
if not result:
|
||||
db.execute(text("ALTER TABLE issues ADD COLUMN issue_subtype VARCHAR(64) NULL"))
|
||||
# issues.issue_type enum -> varchar
|
||||
result = db.execute(text("SHOW COLUMNS FROM issues WHERE Field='issue_type'")).fetchone()
|
||||
if result and 'enum' in result[1].lower():
|
||||
db.execute(text("ALTER TABLE issues MODIFY issue_type VARCHAR(32) DEFAULT 'issue'"))
|
||||
# projects.project_code
|
||||
result = db.execute(text("SHOW COLUMNS FROM projects LIKE 'project_code'")).fetchone()
|
||||
if not result:
|
||||
db.execute(text("ALTER TABLE projects ADD COLUMN project_code VARCHAR(16) NULL"))
|
||||
db.execute(text("CREATE UNIQUE INDEX idx_projects_project_code ON projects (project_code)"))
|
||||
except Exception as e:
|
||||
print(f"Migration warning: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# Run database migration on startup
|
||||
@app.on_event("startup")
|
||||
def startup():
|
||||
from app.core.config import Base, engine, SessionLocal
|
||||
from app.models import webhook, apikey, activity, milestone, notification, worklog, monitor
|
||||
from app.models import models, webhook, apikey, activity, milestone, notification, worklog, monitor
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_migrate_schema()
|
||||
|
||||
|
||||
@@ -92,6 +92,7 @@ class Project(Base):
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String(100), unique=True, nullable=False)
|
||||
project_code = Column(String(16), unique=True, index=True, nullable=True)
|
||||
description = Column(Text, nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
@@ -131,3 +132,11 @@ class ProjectMember(Base):
|
||||
|
||||
project = relationship("Project", back_populates="members")
|
||||
user = relationship("User", back_populates="project_memberships")
|
||||
|
||||
|
||||
class ProjectCodeCounter(Base):
|
||||
__tablename__ = project_code_counters
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
prefix = Column(String(16), unique=True, index=True, nullable=False)
|
||||
next_value = Column(Integer, default=0)
|
||||
|
||||
@@ -134,6 +134,7 @@ class ProjectUpdate(BaseModel):
|
||||
class ProjectResponse(ProjectBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
project_code: str | None = None
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
|
||||
Reference in New Issue
Block a user