package controller import ( "code.gitea.io/sdk/gitea" "fmt" "gitea-issue/giteaClient" "gitea-issue/model" "github.com/gin-gonic/gin" "github.com/savaki/swag/endpoint" "github.com/savaki/swag/swagger" "net/http" "strconv" ) func PostIssue(c *gin.Context) { var issueProxy model.PostCreateIssueProxy if err := c.BindJSON(&issueProxy); err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_ISSUE_ERROR", "error": err.Error(), }) return } sudo := c.GetHeader("X-Sudo") if (sudo != "") { giteaClient.SudoUser(sudo) } giteaIssue, err := issueProxy.TransformToGiteaCreateIssueOption() if err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_ISSUE_ERROR", "error": err.Error(), }) return } response, err := giteaClient.CreateIssue(giteaIssue) if err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_ISSUE_ERROR", "error": err.Error(), }) return } c.AsciiJSON(http.StatusOK, model.IssueTransformFromGitea(response)) } func PostIssueSwagger() (*swagger.Endpoint) { return endpoint.New("post", "/issues", "Create issue", endpoint.Handler(PostIssue), endpoint.Description("Post new issue"), endpoint.Body(model.PostCreateIssueProxy{}, "Issue object", true), endpoint.Tags("issues"), endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue list"), ) } func GetIssues(c *gin.Context) { giteaIssues, err := giteaClient.GetIssues(); proxyIssues := []model.GetCreateIssueProxy{} for _, issue := range giteaIssues { proxyIssues = append(proxyIssues, model.IssueTransformFromGitea(issue)) } if (err != nil) { c.AbortWithStatus(http.StatusNotFound) } c.AsciiJSON(http.StatusOK, proxyIssues) } func GetIssuesSwagger() (*swagger.Endpoint) { return endpoint.New("get", "/issues", "List project issues", endpoint.Handler(GetIssues), endpoint.Description("Get all issues"), endpoint.Security("token", "USER"), endpoint.Tags("issues"), endpoint.Response(http.StatusOK, []model.GetCreateIssueProxy{}, "Gitea Issue list"), ) } func GetIssue(c *gin.Context) { issueId, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { fmt.Println(fmt.Sprintf("ParseInt err: %+v", err)) c.AsciiJSON(http.StatusNotFound, err.Error()) } issue, err := giteaClient.GetIssue(issueId) if err != nil { c.AbortWithStatus(http.StatusNotFound) } c.AsciiJSON(http.StatusOK, model.IssueTransformFromGitea(issue)) } func GetIssueSwagger() (*swagger.Endpoint) { return endpoint.New("get", "/issues/:id", "Get one issue", endpoint.Handler(GetIssue), endpoint.Description("Get one issue"), endpoint.Security("token", "USER"), endpoint.Tags("issues"), endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue"), ) } func GetIssueComments(c *gin.Context) { issueId, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { fmt.Println(fmt.Sprintf("ParseInt err: %+v", err)) c.AsciiJSON(http.StatusNotFound, err.Error()) } issueComments, err := giteaClient.GetIssueComments(issueId) proxyComments := []model.GetCommentProxy{} for _, comment := range issueComments { proxyComments = append(proxyComments, model.CommentTransformFromGitea(comment)) } if (err != nil) { c.AbortWithStatus(http.StatusNotFound) } c.AsciiJSON(http.StatusOK, proxyComments) if err != nil { c.AbortWithStatus(http.StatusNotFound) } c.AsciiJSON(http.StatusOK, issueComments) } func GetIssueCommentsSwagger() (*swagger.Endpoint) { return endpoint.New("get", "/issues/:id/comments", "Get issue comments", endpoint.Handler(GetIssueComments), endpoint.Description("Get issue comments"), endpoint.Security("token", "USER"), endpoint.Tags("issues"), endpoint.Response(http.StatusOK, []gitea.Comment{}, "Gitea issue comments"), ) } func PostIssueComment(c *gin.Context) { var commentProxy model.PostCommentProxy if err := c.BindJSON(&commentProxy); err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_COMMENT_ERROR", "error": err.Error(), }) return } issueId, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { fmt.Println(fmt.Sprintf("ParseInt err: %+v", err)) c.AsciiJSON(http.StatusNotFound, err.Error()) } giteaComment, err := commentProxy.TransformToGiteaComment() if err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_COMMENT_ERROR", "error": err.Error(), }) return } sudo := c.GetHeader("X-Sudo") if (sudo != "") { giteaClient.SudoUser(sudo) } response, err := giteaClient.CreateComment(issueId, giteaComment) if err != nil { c.AsciiJSON(http.StatusBadRequest, gin.H{ "message": "CREATE_ISSUE_ERROR", "error": err.Error(), }) return } c.AsciiJSON(http.StatusOK, model.CommentTransformFromGitea(response)) } func PostIssueCommentSwagger() (*swagger.Endpoint) { return endpoint.New("post", "/issues/:id/comments", "Create issue comment", endpoint.Handler(PostIssueComment), endpoint.Description("Add issue comment"), endpoint.Body(model.PostCommentProxy{}, "Comment object", true), endpoint.Security("token", "USER"), endpoint.Tags("issues"), endpoint.Response(http.StatusOK, model.GetCommentProxy{}, "Gitea issue comments"), ) }