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

feat: Post Stakwork Run ID to Frontend on Successful Send #2123

Merged
Merged
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
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
Loading