Skip to content

Commit

Permalink
Merge pull request #1921 from MahtabBukhari/endpoint-to-receive-formD…
Browse files Browse the repository at this point in the history
…ata-and-post-to-stakwork

endpoint to receive formData and post to stakwork
  • Loading branch information
humansinstitute authored Nov 8, 2024
2 parents 3433b8c + cba1ca3 commit 53872cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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

0 comments on commit 53872cb

Please sign in to comment.