From abbc6c2a6624a8cbd1c8d65bbe18e6e98bb9e0ff Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 25 Feb 2025 10:45:36 +0100 Subject: [PATCH 1/4] feat: change stakwork key for chat --- handlers/chat.go | 4 +-- handlers/features.go | 9 ++--- routes/index.go | 86 ++------------------------------------------ 3 files changed, 6 insertions(+), 93 deletions(-) diff --git a/handlers/chat.go b/handlers/chat.go index cc0334b67..fd1bda02d 100644 --- a/handlers/chat.go +++ b/handlers/chat.go @@ -439,9 +439,9 @@ func (ch *ChatHandler) sendToStakwork(payload StakworkChatPayload) (int64, error return 0, fmt.Errorf("error creating request: %v", err) } - apiKey := os.Getenv("SWWFKEY") + apiKey := os.Getenv("SWWFSWKEY") if apiKey == "" { - return 0, fmt.Errorf("SWWFKEY environment variable not set") + return 0, fmt.Errorf("SWWFSWKEY environment variable not set") } req.Header.Set("Authorization", "Token token="+apiKey) diff --git a/handlers/features.go b/handlers/features.go index 94b773df0..2faa78ca5 100644 --- a/handlers/features.go +++ b/handlers/features.go @@ -650,7 +650,6 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { apiKey := os.Getenv("SWWFKEY") if apiKey == "" { panic("API key not set in environment") - return } postData.Alias = user.OwnerAlias @@ -670,13 +669,11 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { stakworkPayloadJSON, err := json.Marshal(stakworkPayload) if err != nil { panic("Failed to encode payload") - return } req, err := http.NewRequest("POST", "https://api.stakwork.com/api/v1/projects", bytes.NewBuffer(stakworkPayloadJSON)) if err != nil { panic("Failed to create request to Stakwork API") - return } req.Header.Set("Authorization", "Token token="+apiKey) req.Header.Set("Content-Type", "application/json") @@ -685,14 +682,12 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) { resp, err := client.Do(req) if err != nil { panic("Failed to send request to Stakwork API") - return } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { panic("Failed to read response from Stakwork API") - return } w.WriteHeader(resp.StatusCode) @@ -911,7 +906,7 @@ func (oh *featureHandler) GetQuickBounties(w http.ResponseWriter, r *http.Reques } status := calculateBountyStatus(bounty) - + var phaseID *string if bounty.PhaseUuid != "" { phaseUUID := bounty.PhaseUuid @@ -923,7 +918,7 @@ func (oh *featureHandler) GetQuickBounties(w http.ResponseWriter, r *http.Reques BountyTitle: bounty.Title, Status: status, AssignedAlias: assignedAlias, - PhaseID: phaseID, + PhaseID: phaseID, } if status == db.StatusTodo { diff --git a/routes/index.go b/routes/index.go index 87128abb6..5fadda61d 100644 --- a/routes/index.go +++ b/routes/index.go @@ -2,9 +2,7 @@ package routes import ( "bytes" - "compress/gzip" "context" - "encoding/hex" "encoding/json" "fmt" "io" @@ -28,8 +26,6 @@ import ( "github.com/stakwork/sphinx-tribes/logger" customMiddleware "github.com/stakwork/sphinx-tribes/middlewares" "github.com/stakwork/sphinx-tribes/utils" - - "github.com/posthog/posthog-go" ) // NewRouter creates a chi router @@ -57,8 +53,6 @@ func NewRouter() *http.Server { r.Mount("/hivechat", ChatRoutes()) r.Mount("/test", TestRoutes()) r.Mount("/feature-flags", FeatureFlagRoutes()) - r.Mount("/snippet", SnippetRoutes()) - r.Mount("/activities", ActivityRoutes()) r.Group(func(r chi.Router) { r.Get("/tribe_by_feed", tribeHandlers.GetFirstTribeByFeed) @@ -134,7 +128,6 @@ type extractResponse struct { } func getFromAuth(path string) (*extractResponse, error) { - authURL := "http://auth:9090" resp, err := http.Get(authURL + path) if err != nil { @@ -202,37 +195,11 @@ func sendEdgeListToJarvis(edgeList utils.EdgeList) error { return fmt.Errorf("jarvis returned non-success status: %d, body: %s", resp.StatusCode, string(body)) } -func compressToHex(input string) (string, error) { - // Create a buffer to hold the compressed data - var compressedBuffer bytes.Buffer - - // Create a gzip writer - gzipWriter := gzip.NewWriter(&compressedBuffer) - - // Write the input string to the gzip writer - _, err := gzipWriter.Write([]byte(input)) - if err != nil { - return "", fmt.Errorf("failed to write to gzip writer: %w", err) - } - - // Close the gzip writer to flush the data - err = gzipWriter.Close() - if err != nil { - return "", fmt.Errorf("failed to close gzip writer: %w", err) - } - - // Encode the compressed data to hex - hexEncoded := hex.EncodeToString(compressedBuffer.Bytes()) - return hexEncoded, nil -} - // Middleware to handle InternalServerError func internalServerErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rr := negroni.NewResponseWriter(w) - var elements_chain strings.Builder - isExceedingLimit := false trap.AddInterceptor(&trap.Interceptor{ Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { index := strings.Index(f.File, "sphinx-tribes") @@ -240,60 +207,11 @@ func internalServerErrorHandler(next http.Handler) http.Handler { if index != -1 { trimmed = f.File[index:] } - - newContent := fmt.Sprintf("%s:%d %s,\n", trimmed, f.Line, f.Name) - maxByteSize := 177000 - if elements_chain.Len()+len(newContent) <= maxByteSize && isExceedingLimit == false { - elements_chain.WriteString(newContent) - if args != nil && args.NumField() != 0 { - for i := 0; i < args.NumField(); i++ { - thingWeWantToPrint := args.GetFieldIndex(i) - argNameAndValue := fmt.Sprintf("Name: %s Value: %#v\n", thingWeWantToPrint.Name(), thingWeWantToPrint.Value()) - if elements_chain.Len()+len(argNameAndValue) <= maxByteSize { - elements_chain.WriteString(argNameAndValue) - } else { - fmt.Printf("elements_chain length exceeded 500KB, skipping further additions.\n") - isExceedingLimit = true - } - } - } - } else if isExceedingLimit == false { - fmt.Printf("elements_chain length exceeded 500KB, skipping further additions.\n") - isExceedingLimit = true - } + logger.Log.Machine("%s:%d %s\n", trimmed, f.Line, f.Name) return nil, nil }, }) - - defer func() { - posthog_key := os.Getenv("POSTHOG_KEY") - posthog_url := os.Getenv("POSTHOG_URL") - session_id := r.Header.Get("x-session-id") - if posthog_key != "" && posthog_url != "" && session_id != "" { - logger.Log.Info("Sending to Posthog") - client, _ := posthog.NewWithConfig( - posthog_key, - posthog.Config{ - Endpoint: posthog_url, - }, - ) - defer client.Close() - elements_chain_string := elements_chain.String() - hexCompressed, _ := compressToHex(elements_chain_string) - _ = client.Enqueue(posthog.Capture{ - DistinctId: session_id, // Unique ID for the user - Event: "backend_api_call", // The event name - Properties: map[string]interface{}{ - "$session_id": session_id, - "$event_type": "backend_api_call", - "$elements_chain": hexCompressed, - "$current_url": r.URL.Path, - }, - }) - } - }() - defer func() { if err := recover(); err != nil { // Get stack trace @@ -336,7 +254,7 @@ func initChi() *chi.Mux { r.Use(customMiddleware.FeatureFlag(db.DB)) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, - AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-User", "authorization", "x-jwt", "Referer", "User-Agent", "x-session-id"}, AllowCredentials: true, MaxAge: 300, From 755cb6e632b7d97fd77d15cbaef4efd294ba5049 Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 25 Feb 2025 11:11:36 +0100 Subject: [PATCH 2/4] update SendBuildMessge API key --- handlers/chat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/chat.go b/handlers/chat.go index fd1bda02d..d6a1dce64 100644 --- a/handlers/chat.go +++ b/handlers/chat.go @@ -439,9 +439,9 @@ func (ch *ChatHandler) sendToStakwork(payload StakworkChatPayload) (int64, error return 0, fmt.Errorf("error creating request: %v", err) } - apiKey := os.Getenv("SWWFSWKEY") + apiKey := os.Getenv("SWWFKEY") if apiKey == "" { - return 0, fmt.Errorf("SWWFSWKEY environment variable not set") + return 0, fmt.Errorf("SWWFKEY environment variable not set") } req.Header.Set("Authorization", "Token token="+apiKey) @@ -851,7 +851,7 @@ func (ch *ChatHandler) SendBuildMessage(w http.ResponseWriter, r *http.Request) return } - apiKey := os.Getenv("SWWFKEY") + apiKey := os.Getenv("SWWFSWKEY") if apiKey == "" { http.Error(w, "API key not set in environment", http.StatusInternalServerError) return From 2bba0cb5b187824b1734055108fab7aff596c0e1 Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 25 Feb 2025 11:19:43 +0100 Subject: [PATCH 3/4] added removed routes --- routes/index.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routes/index.go b/routes/index.go index 5fadda61d..a50acdc2b 100644 --- a/routes/index.go +++ b/routes/index.go @@ -53,6 +53,8 @@ func NewRouter() *http.Server { r.Mount("/hivechat", ChatRoutes()) r.Mount("/test", TestRoutes()) r.Mount("/feature-flags", FeatureFlagRoutes()) + r.Mount("/snippet", SnippetRoutes()) + r.Mount("/activities", ActivityRoutes()) r.Group(func(r chi.Router) { r.Get("/tribe_by_feed", tribeHandlers.GetFirstTribeByFeed) From 049cfa83b2c94f45209ee9381f99f19150666d0a Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 25 Feb 2025 11:24:12 +0100 Subject: [PATCH 4/4] added ommited changes manually --- routes/index.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/routes/index.go b/routes/index.go index a50acdc2b..f3b4059b5 100644 --- a/routes/index.go +++ b/routes/index.go @@ -2,7 +2,9 @@ package routes import ( "bytes" + "compress/gzip" "context" + "encoding/hex" "encoding/json" "fmt" "io" @@ -14,6 +16,7 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + "github.com/posthog/posthog-go" "github.com/rs/cors" "github.com/urfave/negroni" "github.com/xhd2015/xgo/runtime/core" @@ -197,11 +200,37 @@ func sendEdgeListToJarvis(edgeList utils.EdgeList) error { return fmt.Errorf("jarvis returned non-success status: %d, body: %s", resp.StatusCode, string(body)) } +func compressToHex(input string) (string, error) { + // Create a buffer to hold the compressed data + var compressedBuffer bytes.Buffer + + // Create a gzip writer + gzipWriter := gzip.NewWriter(&compressedBuffer) + + // Write the input string to the gzip writer + _, err := gzipWriter.Write([]byte(input)) + if err != nil { + return "", fmt.Errorf("failed to write to gzip writer: %w", err) + } + + // Close the gzip writer to flush the data + err = gzipWriter.Close() + if err != nil { + return "", fmt.Errorf("failed to close gzip writer: %w", err) + } + + // Encode the compressed data to hex + hexEncoded := hex.EncodeToString(compressedBuffer.Bytes()) + return hexEncoded, nil +} + // Middleware to handle InternalServerError func internalServerErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rr := negroni.NewResponseWriter(w) + var elements_chain strings.Builder + isExceedingLimit := false trap.AddInterceptor(&trap.Interceptor{ Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { index := strings.Index(f.File, "sphinx-tribes") @@ -209,11 +238,60 @@ func internalServerErrorHandler(next http.Handler) http.Handler { if index != -1 { trimmed = f.File[index:] } - logger.Log.Machine("%s:%d %s\n", trimmed, f.Line, f.Name) + + newContent := fmt.Sprintf("%s:%d %s,\n", trimmed, f.Line, f.Name) + maxByteSize := 177000 + if elements_chain.Len()+len(newContent) <= maxByteSize && isExceedingLimit == false { + elements_chain.WriteString(newContent) + if args != nil && args.NumField() != 0 { + for i := 0; i < args.NumField(); i++ { + thingWeWantToPrint := args.GetFieldIndex(i) + argNameAndValue := fmt.Sprintf("Name: %s Value: %#v\n", thingWeWantToPrint.Name(), thingWeWantToPrint.Value()) + if elements_chain.Len()+len(argNameAndValue) <= maxByteSize { + elements_chain.WriteString(argNameAndValue) + } else { + fmt.Printf("elements_chain length exceeded 500KB, skipping further additions.\n") + isExceedingLimit = true + } + } + } + } else if isExceedingLimit == false { + fmt.Printf("elements_chain length exceeded 500KB, skipping further additions.\n") + isExceedingLimit = true + } return nil, nil }, }) + + defer func() { + posthog_key := os.Getenv("POSTHOG_KEY") + posthog_url := os.Getenv("POSTHOG_URL") + session_id := r.Header.Get("x-session-id") + if posthog_key != "" && posthog_url != "" && session_id != "" { + logger.Log.Info("Sending to Posthog") + client, _ := posthog.NewWithConfig( + posthog_key, + posthog.Config{ + Endpoint: posthog_url, + }, + ) + defer client.Close() + elements_chain_string := elements_chain.String() + hexCompressed, _ := compressToHex(elements_chain_string) + _ = client.Enqueue(posthog.Capture{ + DistinctId: session_id, // Unique ID for the user + Event: "backend_api_call", // The event name + Properties: map[string]interface{}{ + "$session_id": session_id, + "$event_type": "backend_api_call", + "$elements_chain": hexCompressed, + "$current_url": r.URL.Path, + }, + }) + } + }() + defer func() { if err := recover(); err != nil { // Get stack trace