Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

endpoint to receive formData and post to stakwork #1921

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions handlers/features.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -12,8 +13,18 @@ import (
"github.com/rs/xid"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"os"
)

type PostData struct {
ProductBrief string `json:"productBrief"`
FeatureName string `json:"featureName"`
Description string `json:"description"`
Examples []string `json:"examples"`
WebhookURL string `json:"webhook_url"`
FeatureUUID string `json:"featureUUID"`
}

type featureHandler struct {
db db.Database
generateBountyHandler func(bounties []db.NewBounty) []db.BountyResponse
Expand Down Expand Up @@ -448,3 +459,70 @@ func (oh *featureHandler) GetFeatureStories(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("User stories added successfuly")
}

func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) {

body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
http.Error(w, "Failed to read requests body", http.StatusBadRequest)
return
}

var postData PostData
err = json.Unmarshal(body, &postData)
if err != nil {
fmt.Println("[StoriesSend] JSON Unmarshal error:", err)
http.Error(w, "Invalid JSON format", http.StatusNotAcceptable)
return
}

apiKey := os.Getenv("SWWFKEY")
if apiKey == "" {
http.Error(w, "API key not set in environment", http.StatusInternalServerError)
return
}

stakworkPayload := map[string]interface{}{
"name": "string",
"workflow_id": 35080,
"workflow_params": map[string]interface{}{
"set_var": map[string]interface{}{
"attributes": map[string]interface{}{
"vars": postData,
},
},
},
}

stakworkPayloadJSON, err := json.Marshal(stakworkPayload)
if err != nil {
http.Error(w, "Failed to encode payload", http.StatusInternalServerError)
return
}

req, err := http.NewRequest("POST", "https://api.stakwork.com/api/v1/projects", bytes.NewBuffer(stakworkPayloadJSON))
if err != nil {
http.Error(w, "Failed to create request to Stakwork API", http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", "Token token="+apiKey)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Failed to send request to Stakwork API", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Failed to read response from Stakwork API", http.StatusInternalServerError)
return
}

w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}
1 change: 1 addition & 0 deletions routes/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func FeatureRoutes() chi.Router {
r.Delete("/{feature_uuid}/phase/{phase_uuid}", featureHandlers.DeleteFeaturePhase)

r.Post("/story", featureHandlers.CreateOrEditStory)
r.Post("/stories/send", featureHandlers.StoriesSend)
r.Get("/{feature_uuid}/story", featureHandlers.GetStoriesByFeatureUuid)
r.Get("/{feature_uuid}/story/{story_uuid}", featureHandlers.GetStoryByUuid)
r.Delete("/{feature_uuid}/story/{story_uuid}", featureHandlers.DeleteStory)
Expand Down
Loading