feat(integrations): align linear parity

This commit is contained in:
2026-06-06 17:09:15 +08:00
parent 7b28f9227d
commit a6f97c8bbc
9 changed files with 739 additions and 112 deletions
@@ -1,7 +1,9 @@
package v1
import (
"errors"
"net/http"
"strings"
"github.com/gin-gonic/gin"
@@ -33,7 +35,7 @@ func (h *LinearIntegrationHandler) Delete(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"message": "Linear integration deleted"})
c.Status(http.StatusOK)
}
// GetTeams retrieves available Linear teams.
@@ -47,10 +49,10 @@ func (h *LinearIntegrationHandler) GetTeams(c *gin.Context) {
teams, svcErr := h.svc.GetTeams(c.Request.Context(), accountID)
if svcErr != nil {
handleServiceError(c, svcErr)
handleLinearServiceError(c, svcErr)
return
}
response.OK(c, teams)
c.JSON(http.StatusOK, teams)
}
// GetTeamEntities retrieves entities from a Linear team.
@@ -62,12 +64,12 @@ func (h *LinearIntegrationHandler) GetTeamEntities(c *gin.Context) {
return
}
entities, svcErr := h.svc.GetTeamEntities(c.Request.Context(), accountID)
entities, svcErr := h.svc.GetTeamEntities(c.Request.Context(), accountID, c.Query("team_id"))
if svcErr != nil {
handleServiceError(c, svcErr)
handleLinearServiceError(c, svcErr)
return
}
response.OK(c, entities)
c.JSON(http.StatusOK, entities)
}
// CreateIssue creates a Linear issue from a conversation.
@@ -80,17 +82,17 @@ func (h *LinearIntegrationHandler) CreateIssue(c *gin.Context) {
}
var req service.CreateIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := c.ShouldBind(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.CreateIssue(c.Request.Context(), accountID, req)
result, svcErr := h.svc.CreateIssue(c.Request.Context(), accountID, req, getUserID(c))
if svcErr != nil {
handleServiceError(c, svcErr)
handleLinearServiceError(c, svcErr)
return
}
response.OK(c, result)
c.JSON(http.StatusOK, result)
}
// LinkIssue links a Linear issue to a conversation.
@@ -103,17 +105,17 @@ func (h *LinearIntegrationHandler) LinkIssue(c *gin.Context) {
}
var req service.LinkIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := c.ShouldBind(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.LinkIssue(c.Request.Context(), accountID, req)
result, svcErr := h.svc.LinkIssue(c.Request.Context(), accountID, req, getUserID(c))
if svcErr != nil {
handleServiceError(c, svcErr)
handleLinearServiceError(c, svcErr)
return
}
response.OK(c, result)
c.JSON(http.StatusOK, result)
}
// UnlinkIssue unlinks a Linear issue from a conversation.
@@ -126,17 +128,17 @@ func (h *LinearIntegrationHandler) UnlinkIssue(c *gin.Context) {
}
var req service.UnlinkIssueRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := c.ShouldBind(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
return
}
result, svcErr := h.svc.UnlinkIssue(c.Request.Context(), accountID, req)
result, svcErr := h.svc.UnlinkIssue(c.Request.Context(), accountID, req, getUserID(c))
if svcErr != nil {
handleServiceError(c, svcErr)
handleLinearServiceError(c, svcErr)
return
}
response.OK(c, result)
c.JSON(http.StatusOK, result)
}
// SearchIssue searches Linear issues.
@@ -149,12 +151,16 @@ func (h *LinearIntegrationHandler) SearchIssue(c *gin.Context) {
}
query := c.Query("q")
issues, svcErr := h.svc.SearchIssue(c.Request.Context(), accountID, query)
if svcErr != nil {
handleServiceError(c, svcErr)
if strings.TrimSpace(query) == "" {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": "Specify search string with parameter q"})
return
}
response.OK(c, issues)
issues, svcErr := h.svc.SearchIssue(c.Request.Context(), accountID, query)
if svcErr != nil {
handleLinearServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, issues)
}
// GetLinkedIssues retrieves Linear issues linked to a conversation.
@@ -166,17 +172,22 @@ func (h *LinearIntegrationHandler) GetLinkedIssues(c *gin.Context) {
return
}
conversationID, _ := parseUintParam(c, "conversation_id")
issues, svcErr := h.svc.GetLinkedIssues(c.Request.Context(), accountID, conversationID)
if svcErr != nil {
handleServiceError(c, svcErr)
conversationID, err := parseOptionalUintQueryParam(c, "conversation_id")
if err != nil || conversationID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
return
}
response.OK(c, issues)
issues, svcErr := h.svc.GetLinkedIssues(c.Request.Context(), accountID, conversationID)
if svcErr != nil {
handleLinearServiceError(c, svcErr)
return
}
c.JSON(http.StatusOK, issues)
}
// RegisterLinearIntegrationRoutes registers Linear integration routes.
func RegisterLinearIntegrationRoutes(g *gin.RouterGroup, h *LinearIntegrationHandler) {
g.DELETE("/linear", h.Delete)
linear := g.Group("/linear")
{
linear.DELETE("/", h.Delete)
@@ -189,3 +200,12 @@ func RegisterLinearIntegrationRoutes(g *gin.RouterGroup, h *LinearIntegrationHan
linear.GET("/linked_issues", h.GetLinkedIssues)
}
}
func handleLinearServiceError(c *gin.Context, err error) {
var providerErr *service.LinearProviderError
if errors.As(err, &providerErr) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": providerErr.Message})
return
}
handleServiceError(c, err)
}
@@ -181,6 +181,20 @@ func TestLinearIntegration_SearchIssue_BadAccountID(t *testing.T) {
assert.Contains(t, errBody["message"], "invalid account_id")
}
func TestLinearIntegration_SearchIssue_BlankQuery(t *testing.T) {
r := setupLinearIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/linear/search_issue", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.Equal(t, "Specify search string with parameter q", resp["error"])
}
func TestLinearIntegration_GetLinkedIssues_BadAccountID(t *testing.T) {
r := setupLinearIntegrationRouter()