Skip to content

Commit b3cd548

Browse files
Fix platform-independent paths and add file cleanup mechanism
Co-authored-by: Harmanpreet-Microsoft <175086414+Harmanpreet-Microsoft@users.noreply.github.com>
1 parent cbf8e6e commit b3cd548

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/backend/generate_sample_branch_report.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import sys
99
import os
10+
import tempfile
1011
from datetime import datetime
1112

1213
# Add the parent directory to the path
@@ -50,9 +51,9 @@ def create_sample_report():
5051
# Format the data
5152
branch_data = generator.format_branch_data(branches, prs)
5253

53-
# Generate output filename
54+
# Generate output filename (platform-independent)
5455
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
55-
output_file = f"/tmp/sample_branch_report_{timestamp}.xlsx"
56+
output_file = os.path.join(tempfile.gettempdir(), f"sample_branch_report_{timestamp}.xlsx")
5657

5758
# Generate Excel file
5859
print(f"Generating sample branch report...")

src/backend/v4/api/router.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ async def get_plan_by_id(
14301430

14311431
@app_v4.get("/generate_branch_report")
14321432
async def generate_branch_report(
1433+
background_tasks: BackgroundTasks,
14331434
request: Request,
14341435
owner: str = Query(..., description="Repository owner (username or organization)"),
14351436
repo: str = Query(..., description="Repository name"),
@@ -1513,12 +1514,26 @@ async def generate_branch_report(
15131514
},
15141515
)
15151516

1517+
# Create a background task to clean up the file after a delay
1518+
async def cleanup_file():
1519+
"""Delete the temporary file after a delay to ensure download completes."""
1520+
import asyncio
1521+
await asyncio.sleep(300) # Wait 5 minutes before cleanup
1522+
try:
1523+
if os.path.exists(output_path):
1524+
os.remove(output_path)
1525+
logger.info(f"Cleaned up temporary file: {output_path}")
1526+
except Exception as cleanup_error:
1527+
logger.warning(f"Failed to cleanup temporary file {output_path}: {cleanup_error}")
1528+
1529+
# Schedule the cleanup task
1530+
background_tasks.add_task(cleanup_file)
1531+
15161532
# Return the file
15171533
return FileResponse(
15181534
path=output_path,
15191535
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
15201536
filename=filename,
1521-
background=None, # Don't delete the file immediately to ensure download completes
15221537
)
15231538

15241539
except HTTPException:

0 commit comments

Comments
 (0)