feat: webhook event firing on issue creation (background task)

This commit is contained in:
Zhi
2026-02-22 02:43:34 +00:00
parent a0d81ec9f5
commit 1a76de7c50
3 changed files with 62 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi import FastAPI, Depends, HTTPException, status, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from typing import List
@@ -11,6 +11,7 @@ from pydantic import BaseModel
from app.core.config import get_db, settings
from app.models import models
from app.schemas import schemas
from app.services.webhook import fire_webhooks_sync
app = FastAPI(
title="HarborForge API",
@@ -99,11 +100,13 @@ async def get_me(current_user: models.User = Depends(get_current_user)):
# ============ Issues API ============
@app.post("/issues", response_model=schemas.IssueResponse, status_code=status.HTTP_201_CREATED)
def create_issue(issue: schemas.IssueCreate, db: Session = Depends(get_db)):
def create_issue(issue: schemas.IssueCreate, bg: BackgroundTasks, db: Session = Depends(get_db)):
db_issue = models.Issue(**issue.model_dump())
db.add(db_issue)
db.commit()
db.refresh(db_issue)
event = "resolution.created" if db_issue.issue_type == "resolution" else "issue.created"
bg.add_task(fire_webhooks_sync, event, {"issue_id": db_issue.id, "title": db_issue.title, "type": db_issue.issue_type, "status": db_issue.status}, db_issue.project_id, db)
return db_issue