Files
gochat/internal/handler/api/v1/linear_integration_handler.go
T
2026-06-04 15:44:48 +08:00

192 lines
6.0 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// LinearIntegrationHandler handles Linear integration endpoints.
// Reference: Chatwoot Integrations::LinearController
type LinearIntegrationHandler struct {
svc *service.LinearIntegrationService
}
// NewLinearIntegrationHandler creates a new LinearIntegrationHandler.
func NewLinearIntegrationHandler(svc *service.LinearIntegrationService) *LinearIntegrationHandler {
return &LinearIntegrationHandler{svc: svc}
}
// Delete removes a Linear integration for an account.
// DELETE /api/v1/accounts/:account_id/integrations/linear
func (h *LinearIntegrationHandler) Delete(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
if svcErr := h.svc.Delete(c.Request.Context(), accountID); svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"message": "Linear integration deleted"})
}
// GetTeams retrieves available Linear teams.
// GET /api/v1/accounts/:account_id/integrations/linear/teams
func (h *LinearIntegrationHandler) GetTeams(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
teams, svcErr := h.svc.GetTeams(c.Request.Context(), accountID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, teams)
}
// GetTeamEntities retrieves entities from a Linear team.
// GET /api/v1/accounts/:account_id/integrations/linear/team_entities
func (h *LinearIntegrationHandler) GetTeamEntities(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
entities, svcErr := h.svc.GetTeamEntities(c.Request.Context(), accountID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, entities)
}
// CreateIssue creates a Linear issue from a conversation.
// POST /api/v1/accounts/:account_id/integrations/linear/create_issue
func (h *LinearIntegrationHandler) CreateIssue(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.CreateIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.CreateIssue(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, result)
}
// LinkIssue links a Linear issue to a conversation.
// POST /api/v1/accounts/:account_id/integrations/linear/link_issue
func (h *LinearIntegrationHandler) LinkIssue(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.LinkIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.LinkIssue(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, result)
}
// UnlinkIssue unlinks a Linear issue from a conversation.
// POST /api/v1/accounts/:account_id/integrations/linear/unlink_issue
func (h *LinearIntegrationHandler) UnlinkIssue(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req service.UnlinkIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.UnlinkIssue(c.Request.Context(), accountID, req)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, result)
}
// SearchIssue searches Linear issues.
// GET /api/v1/accounts/:account_id/integrations/linear/search_issue
func (h *LinearIntegrationHandler) SearchIssue(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
query := c.Query("q")
issues, svcErr := h.svc.SearchIssue(c.Request.Context(), accountID, query)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, issues)
}
// GetLinkedIssues retrieves Linear issues linked to a conversation.
// GET /api/v1/accounts/:account_id/integrations/linear/linked_issues
func (h *LinearIntegrationHandler) GetLinkedIssues(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
conversationID, _ := parseUintParam(c, "conversation_id")
issues, svcErr := h.svc.GetLinkedIssues(c.Request.Context(), accountID, conversationID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.OK(c, issues)
}
// RegisterLinearIntegrationRoutes registers Linear integration routes.
func RegisterLinearIntegrationRoutes(g *gin.RouterGroup, h *LinearIntegrationHandler) {
linear := g.Group("/linear")
{
linear.DELETE("/", h.Delete)
linear.GET("/teams", h.GetTeams)
linear.GET("/team_entities", h.GetTeamEntities)
linear.POST("/create_issue", h.CreateIssue)
linear.POST("/link_issue", h.LinkIssue)
linear.POST("/unlink_issue", h.UnlinkIssue)
linear.GET("/search_issue", h.SearchIssue)
linear.GET("/linked_issues", h.GetLinkedIssues)
}
}