Skip to content

Commit

Permalink
Merge pull request #2123 from saithsab877/feat-stakwork-run-websocket
Browse files Browse the repository at this point in the history
feat: Post Stakwork Run ID to Frontend on Successful Send
  • Loading branch information
humansinstitute authored Dec 6, 2024
2 parents 5e13ad9 + 2056b1b commit 7ff1f2d
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type TicketResponse struct {
Errors []string `json:"errors,omitempty"`
}

type StakworkResponse struct {
Success bool `json:"success"`
Data struct {
ProjectID int64 `json:"project_id"`
} `json:"data"`
}

func NewTicketHandler(httpClient HttpClient, database db.Database) *ticketHandler {
return &ticketHandler{
httpClient: httpClient,
Expand Down Expand Up @@ -396,7 +403,18 @@ func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http
return
}

if resp.StatusCode != http.StatusOK {
var stakworkResp StakworkResponse
if err := json.Unmarshal(respBody, &stakworkResp); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(TicketResponse{
Success: false,
Message: "Error parsing Stakwork response",
Errors: []string{err.Error()},
})
return
}

if resp.StatusCode != http.StatusOK || !stakworkResp.Success {
w.WriteHeader(resp.StatusCode)
json.NewEncoder(w).Encode(TicketResponse{
Success: false,
Expand Down Expand Up @@ -429,6 +447,24 @@ func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http
})
return
}

projectMsg := websocket.TicketMessage{
BroadcastType: "direct",
SourceSessionID: ticketRequest.Metadata.ID,
Message: fmt.Sprintf("https://jobs.stakwork.com/admin/projects/%d", stakworkResp.Data.ProjectID),
Action: "swrun",
TicketDetails: websocket.TicketData{},
}

if err := websocket.WebsocketPool.SendTicketMessage(projectMsg); err != nil {
log.Printf("Failed to send project ID websocket message: %v", err)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"ticket": ticketRequest,
"websocket_error": err.Error(),
})
return
}
}

w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit 7ff1f2d

Please sign in to comment.