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

Add Webhook Endpoint and ProcessTicketReview Handler for Stackwork Ticket Review Automation #1995

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 52 additions & 7 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/utils"
)

type ticketHandler struct {
Expand All @@ -21,6 +23,13 @@ func NewTicketHandler(database db.Database) *ticketHandler {
}
}

type TicketReviewRequest struct {
FeatureUUID string `json:"featureUUID" validate:"required"`
PhaseUUID string `json:"phaseUUID" validate:"required"`
TicketUUID string `json:"ticketUUID" validate:"required"`
TicketDescription string `json:"ticketDescription" validate:"required"`
}

func (th *ticketHandler) GetTicket(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
if uuid == "" {
Expand All @@ -38,7 +47,7 @@ func (th *ticketHandler) GetTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, ticket)
utils.RespondWithJSON(w, http.StatusOK, ticket)
}

func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -78,7 +87,7 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, updatedTicket)
utils.RespondWithJSON(w, http.StatusOK, updatedTicket)
}

func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
Expand All @@ -98,11 +107,47 @@ func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
return
}

respondWithJSON(w, http.StatusOK, map[string]string{"status": "success"})
utils.RespondWithJSON(w, http.StatusOK, map[string]string{"status": "success"})
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(payload)
func (th *ticketHandler) ProcessTicketReview(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}

var reviewReq utils.TicketReviewRequest
if err := json.Unmarshal(body, &reviewReq); err != nil {
log.Printf("Error parsing request JSON: %v", err)
http.Error(w, "Error parsing request body", http.StatusBadRequest)
return
}

if err := utils.ValidateTicketReviewRequest(&reviewReq); err != nil {
log.Printf("Invalid request data: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

ticket, err := th.db.GetTicket(reviewReq.TicketUUID)
if err != nil {
log.Printf("Error fetching ticket: %v", err)
http.Error(w, "Ticket not found", http.StatusNotFound)
return
}

ticket.Description = reviewReq.TicketDescription

updatedTicket, err := th.db.UpdateTicket(ticket)
if err != nil {
log.Printf("Error updating ticket: %v", err)
http.Error(w, "Failed to update ticket", http.StatusInternalServerError)
return
}

log.Printf("Successfully updated ticket %s", reviewReq.TicketUUID)

utils.RespondWithJSON(w, http.StatusOK, updatedTicket)
}
1 change: 1 addition & 0 deletions routes/ticket_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TicketRoutes() chi.Router {
r.Get("/{uuid}", ticketHandler.GetTicket)
r.Put("/{uuid}", ticketHandler.UpdateTicket)
r.Delete("/{uuid}", ticketHandler.DeleteTicket)
r.Post("/review", ticketHandler.ProcessTicketReview)
})

return r
Expand Down
36 changes: 36 additions & 0 deletions utils/ticket_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"encoding/json"
"errors"
"net/http"
)

type TicketReviewRequest struct {
FeatureUUID string `json:"featureUUID" validate:"required"`
PhaseUUID string `json:"phaseUUID" validate:"required"`
TicketUUID string `json:"ticketUUID" validate:"required"`
TicketDescription string `json:"ticketDescription" validate:"required"`
}

func ValidateTicketReviewRequest(req *TicketReviewRequest) error {
if req.FeatureUUID == "" {
return errors.New("featureUUID is required")
}
if req.PhaseUUID == "" {
return errors.New("phaseUUID is required")
}
if req.TicketUUID == "" {
return errors.New("ticketUUID is required")
}
if req.TicketDescription == "" {
return errors.New("ticketDescription is required")
}
return nil
}

func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(payload)
}