From 99606b29e5fb8caa2795bb5a8848c6aeaa02b628 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Thu, 13 Feb 2025 11:38:09 +0500 Subject: [PATCH] Update Ticket Builder Send to Stakwork Payload to include code graph details --- db/code_graphs.go | 10 +++++----- db/interface.go | 2 +- handlers/chat.go | 16 ++++++++-------- handlers/ticket.go | 4 ++-- handlers/workspaces.go | 6 +++--- handlers/workspaces_test.go | 11 +++++------ mocks/Database.go | 32 +++++++++++++++----------------- routes/workspaces.go | 2 +- 8 files changed, 40 insertions(+), 43 deletions(-) diff --git a/db/code_graphs.go b/db/code_graphs.go index 20b44dd40..dfca61e90 100644 --- a/db/code_graphs.go +++ b/db/code_graphs.go @@ -16,15 +16,15 @@ func (db database) GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error) { return codeGraph, nil } -func (db database) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error) { - var codeGraphs []WorkspaceCodeGraph - result := db.db.Where("workspace_uuid = ?", workspace_uuid).Find(&codeGraphs) +func (db database) GetCodeGraphByWorkspaceUuid(workspace_uuid string) (WorkspaceCodeGraph, error) { + var codeGraph WorkspaceCodeGraph + result := db.db.Where("workspace_uuid = ?", workspace_uuid).Order("created DESC").First(&codeGraph) if result.Error != nil { - return nil, result.Error + return WorkspaceCodeGraph{}, result.Error } - return codeGraphs, nil + return codeGraph, nil } func (db database) CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error) { diff --git a/db/interface.go b/db/interface.go index d8f4bf3ab..7cb2148de 100644 --- a/db/interface.go +++ b/db/interface.go @@ -214,7 +214,7 @@ type Database interface { GetChatMessagesForChatID(chatID string) ([]ChatMessage, error) GetChatsForWorkspace(workspaceID string, chatStatus string) ([]Chat, error) GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error) - GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error) + GetCodeGraphByWorkspaceUuid(workspace_uuid string) (WorkspaceCodeGraph, error) CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error) DeleteCodeGraph(workspace_uuid string, uuid string) error GetTicketsWithoutGroup() ([]Tickets, error) diff --git a/handlers/chat.go b/handlers/chat.go index 5f4c073ae..377f5fbc6 100644 --- a/handlers/chat.go +++ b/handlers/chat.go @@ -70,11 +70,11 @@ type FileResponse struct { } type SendMessageRequest struct { - ChatID string `json:"chat_id"` - Message string `json:"message"` - PDFURL string `json:"pdf_url,omitempty"` - ModelSelection string `json:"modelSelection,omitempty"` - ContextTags []struct { + ChatID string `json:"chat_id"` + Message string `json:"message"` + PDFURL string `json:"pdf_url,omitempty"` + ModelSelection string `json:"modelSelection,omitempty"` + ContextTags []struct { Type string `json:"type"` ID string `json:"id"` } `json:"contextTags"` @@ -355,9 +355,9 @@ func (ch *ChatHandler) SendMessage(w http.ResponseWriter, r *http.Request) { var codeGraph *db.WorkspaceCodeGraph if workspaceID := request.WorkspaceUUID; workspaceID != "" { - codeGraphs, err := ch.db.GetCodeGraphsByWorkspaceUuid(workspaceID) - if err == nil && len(codeGraphs) > 0 { - codeGraph = &codeGraphs[0] + codeGraphResult, err := ch.db.GetCodeGraphByWorkspaceUuid(workspaceID) + if err == nil { + codeGraph = &codeGraphResult } } diff --git a/handlers/ticket.go b/handlers/ticket.go index 0ada75610..6fc8a2c42 100644 --- a/handlers/ticket.go +++ b/handlers/ticket.go @@ -383,7 +383,7 @@ func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http var ( productBrief, featureBrief, featureArchitecture, codeGraphURL, codeGraphAlias string - feature db.WorkspaceFeatures + feature db.WorkspaceFeatures ) if ticket.FeatureUUID != "" { @@ -461,7 +461,7 @@ func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http schematicURL = workspace.SchematicUrl - codeGraph, err := th.db.GetCodeGraphByUUID(feature.WorkspaceUuid) + codeGraph, err := th.db.GetCodeGraphByWorkspaceUuid(feature.WorkspaceUuid) if err == nil { codeGraphURL = codeGraph.Url codeGraphAlias = codeGraph.SecretAlias diff --git a/handlers/workspaces.go b/handlers/workspaces.go index 4c477104a..b3a0b35de 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -1230,7 +1230,7 @@ func (oh *workspaceHandler) GetWorkspaceCodeGraphByUUID(w http.ResponseWriter, r json.NewEncoder(w).Encode(codeGraph) } -func (oh *workspaceHandler) GetCodeGraphsByWorkspaceUuid(w http.ResponseWriter, r *http.Request) { +func (oh *workspaceHandler) GetCodeGraphByWorkspaceUuid(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) if pubKeyFromAuth == "" { @@ -1240,7 +1240,7 @@ func (oh *workspaceHandler) GetCodeGraphsByWorkspaceUuid(w http.ResponseWriter, } workspace_uuid := chi.URLParam(r, "workspace_uuid") - codeGraphs, err := oh.db.GetCodeGraphsByWorkspaceUuid(workspace_uuid) + codeGraph, err := oh.db.GetCodeGraphByWorkspaceUuid(workspace_uuid) if err != nil { w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(map[string]string{"error": "Failed to get code graphs"}) @@ -1248,7 +1248,7 @@ func (oh *workspaceHandler) GetCodeGraphsByWorkspaceUuid(w http.ResponseWriter, } w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(codeGraphs) + json.NewEncoder(w).Encode(codeGraph) } func (oh *workspaceHandler) DeleteWorkspaceCodeGraph(w http.ResponseWriter, r *http.Request) { diff --git a/handlers/workspaces_test.go b/handlers/workspaces_test.go index c6737d831..f5f845138 100644 --- a/handlers/workspaces_test.go +++ b/handlers/workspaces_test.go @@ -2185,7 +2185,7 @@ func TestGetCodeGraphsByWorkspaceUuid(t *testing.T) { t.Run("should return error if user is not authorized", func(t *testing.T) { rr := httptest.NewRecorder() - handler := http.HandlerFunc(oHandler.GetCodeGraphsByWorkspaceUuid) + handler := http.HandlerFunc(oHandler.GetCodeGraphByWorkspaceUuid) rctx := chi.NewRouteContext() rctx.URLParams.Add("workspace_uuid", workspace.Uuid) @@ -2201,7 +2201,7 @@ func TestGetCodeGraphsByWorkspaceUuid(t *testing.T) { t.Run("should return workspace code graphs if user is authorized", func(t *testing.T) { rr := httptest.NewRecorder() - handler := http.HandlerFunc(oHandler.GetCodeGraphsByWorkspaceUuid) + handler := http.HandlerFunc(oHandler.GetCodeGraphByWorkspaceUuid) rctx := chi.NewRouteContext() rctx.URLParams.Add("workspace_uuid", workspace.Uuid) @@ -2215,12 +2215,11 @@ func TestGetCodeGraphsByWorkspaceUuid(t *testing.T) { handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) - var returnedCodeGraphs []db.WorkspaceCodeGraph + var returnedCodeGraphs db.WorkspaceCodeGraph err = json.Unmarshal(rr.Body.Bytes(), &returnedCodeGraphs) assert.NoError(t, err) - assert.Equal(t, 1, len(returnedCodeGraphs)) - assert.Equal(t, codeGraph.Name, returnedCodeGraphs[0].Name) - assert.Equal(t, codeGraph.Url, returnedCodeGraphs[0].Url) + assert.Equal(t, codeGraph.Name, returnedCodeGraphs.Name) + assert.Equal(t, codeGraph.Url, returnedCodeGraphs.Url) }) } diff --git a/mocks/Database.go b/mocks/Database.go index be24263dc..9f26b97b7 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -4731,25 +4731,23 @@ func (_c *Database_GetCodeGraphByUUID_Call) RunAndReturn(run func(string) (db.Wo return _c } -// GetCodeGraphsByWorkspaceUuid provides a mock function with given fields: workspace_uuid -func (_m *Database) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]db.WorkspaceCodeGraph, error) { +// GetCodeGraphByWorkspaceUuid provides a mock function with given fields: workspace_uuid +func (_m *Database) GetCodeGraphByWorkspaceUuid(workspace_uuid string) (db.WorkspaceCodeGraph, error) { ret := _m.Called(workspace_uuid) if len(ret) == 0 { - panic("no return value specified for GetCodeGraphsByWorkspaceUuid") + panic("no return value specified for GetCodeGraphByWorkspaceUuid") } - var r0 []db.WorkspaceCodeGraph + var r0 db.WorkspaceCodeGraph var r1 error - if rf, ok := ret.Get(0).(func(string) ([]db.WorkspaceCodeGraph, error)); ok { + if rf, ok := ret.Get(0).(func(string) (db.WorkspaceCodeGraph, error)); ok { return rf(workspace_uuid) } - if rf, ok := ret.Get(0).(func(string) []db.WorkspaceCodeGraph); ok { + if rf, ok := ret.Get(0).(func(string) db.WorkspaceCodeGraph); ok { r0 = rf(workspace_uuid) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]db.WorkspaceCodeGraph) - } + r0 = ret.Get(0).(db.WorkspaceCodeGraph) } if rf, ok := ret.Get(1).(func(string) error); ok { @@ -4761,30 +4759,30 @@ func (_m *Database) GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]db.Wo return r0, r1 } -// Database_GetCodeGraphsByWorkspaceUuid_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetCodeGraphsByWorkspaceUuid' -type Database_GetCodeGraphsByWorkspaceUuid_Call struct { +// Database_GetCodeGraphByWorkspaceUuid_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetCodeGraphByWorkspaceUuid' +type Database_GetCodeGraphByWorkspaceUuid_Call struct { *mock.Call } -// GetCodeGraphsByWorkspaceUuid is a helper method to define mock.On call +// GetCodeGraphByWorkspaceUuid is a helper method to define mock.On call // - workspace_uuid string -func (_e *Database_Expecter) GetCodeGraphsByWorkspaceUuid(workspace_uuid interface{}) *Database_GetCodeGraphsByWorkspaceUuid_Call { - return &Database_GetCodeGraphsByWorkspaceUuid_Call{Call: _e.mock.On("GetCodeGraphsByWorkspaceUuid", workspace_uuid)} +func (_e *Database_Expecter) GetCodeGraphByWorkspaceUuid(workspace_uuid interface{}) *Database_GetCodeGraphByWorkspaceUuid_Call { + return &Database_GetCodeGraphByWorkspaceUuid_Call{Call: _e.mock.On("GetCodeGraphByWorkspaceUuid", workspace_uuid)} } -func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) Run(run func(workspace_uuid string)) *Database_GetCodeGraphsByWorkspaceUuid_Call { +func (_c *Database_GetCodeGraphByWorkspaceUuid_Call) Run(run func(workspace_uuid string)) *Database_GetCodeGraphByWorkspaceUuid_Call { _c.Call.Run(func(args mock.Arguments) { run(args[0].(string)) }) return _c } -func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) Return(_a0 []db.WorkspaceCodeGraph, _a1 error) *Database_GetCodeGraphsByWorkspaceUuid_Call { +func (_c *Database_GetCodeGraphByWorkspaceUuid_Call) Return(_a0 db.WorkspaceCodeGraph, _a1 error) *Database_GetCodeGraphByWorkspaceUuid_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *Database_GetCodeGraphsByWorkspaceUuid_Call) RunAndReturn(run func(string) ([]db.WorkspaceCodeGraph, error)) *Database_GetCodeGraphsByWorkspaceUuid_Call { +func (_c *Database_GetCodeGraphByWorkspaceUuid_Call) RunAndReturn(run func(string) (db.WorkspaceCodeGraph, error)) *Database_GetCodeGraphByWorkspaceUuid_Call { _c.Call.Return(run) return _c } diff --git a/routes/workspaces.go b/routes/workspaces.go index da2bb2491..c0dbde515 100644 --- a/routes/workspaces.go +++ b/routes/workspaces.go @@ -58,7 +58,7 @@ func WorkspaceRoutes() chi.Router { r.Post("/codegraph", workspaceHandlers.CreateOrEditWorkspaceCodeGraph) r.Get("/codegraph/{uuid}", workspaceHandlers.GetWorkspaceCodeGraphByUUID) - r.Get("/{workspace_uuid}/codegraph", workspaceHandlers.GetCodeGraphsByWorkspaceUuid) + r.Get("/{workspace_uuid}/codegraph", workspaceHandlers.GetCodeGraphByWorkspaceUuid) r.Delete("/{workspace_uuid}/codegraph/{uuid}", workspaceHandlers.DeleteWorkspaceCodeGraph) }) return r