-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
128 lines (100 loc) · 3.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from dotenv import load_dotenv
load_dotenv()
import yaml
from yaml.loader import SafeLoader
from typing import Annotated
from fastapi import FastAPI, Request, Header, Query
from fastapi.middleware.cors import CORSMiddleware
from src.data_models import Story, StoryDesc, StoryEstimate
from src.constants import Constants
from src.logger import setup_logger
from src.jira_handler import JiraHandler
LOGGER = setup_logger(__name__)
with open(Constants.PROMPT_PATH.value, 'r') as f:
prompt_template_default = f.read()
# Installed libraries
def get_app() -> FastAPI:
"""
Returns the FastAPI app object
"""
try:
fast_app = FastAPI(
title="Jira Copilot Backend",
description="A simple FastAPI backend for Mr. Agile application.")
return fast_app
except Exception as e:
LOGGER.error('exception occured in get_app() - {0}'.format(e))
app = get_app()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health", tags=["health"])
async def health_check(request: Request):
"""
Health check endpoint
"""
return {"status": 200}
@app.get("/prompt_template", tags=["Estimation"])
async def get_prompt_template(request: Request):
"""
Health check endpoint
"""
return {"status": 200,
"prompt_template": prompt_template_default}
@app.post("/jira_authenticate", tags=["Authentication"],
summary="Jira authentication endpoint")
async def jira_authenticate(username: Annotated[str, Header()],
api_token: Annotated[str, Header()], jira_url: Annotated[str, Header()]):
"""
Jira authentication endpoint
"""
jira_handler = JiraHandler(username, api_token, jira_url)
if jira_handler.check_health():
response = {
"status": 200,
"message": "Successfully connected to JIRA"
}
return response
else:
response = {
"status": 400,
"message": "Failed to connect to JIRA"
}
return response
@app.post("/story_id", tags=["Estimation"],
summary="Estimate story subtasks with story id")
async def estimate_story(story_info: Story, username: Annotated[str, Header()],
api_token: Annotated[str, Header()], jira_url: Annotated[str, Header()],
prompt_template: Annotated[str | None, Query()] = None):
"""
Story estimation endpoint
"""
jira_handler = JiraHandler(username, api_token, jira_url)
if not jira_handler.check_health():
response = {
"status": 400,
"message": "Failed to connect to JIRA"
}
return response
result = jira_handler.get_story_estimate(story_info.story_id, prompt_template)
return StoryEstimate(**result)
@app.post("/create_subtasks", tags=["Estimation"],
summary="Creation of subtasks in JIRA for the given story id")
async def create_subtasks_for_story(story_info: StoryEstimate, username: Annotated[str, Header()],
api_token: Annotated[str, Header()], jira_url: Annotated[str, Header()]):
"""
Story estimation endpoint
"""
jira_handler = JiraHandler(username, api_token, jira_url)
if not jira_handler.check_health():
response = {
"status": 400,
"message": "Failed to connect to JIRA"
}
return response
response = jira_handler.create_tasks_from_estimate(story_info.dict())
return response