mirror of
https://gitea.com/gitea/tea.git
synced 2025-12-12 17:32:09 +01:00
feat(issue): Add JSON output and file redirection (#841)
This change enhances the 'issue' command functionality by enabling structured JSON output for single issue views and introducing a method for output redirection. **Changes Implemented:** 1. Enables the existing `--output json` flag for single issue commands (e.g., 'tea issue 17'). This flag was previously ignored in this context. 2. Introduces the new `--out <filename>` flag, which redirects the marshaled JSON output from stdout to the specified file. Feeback more then welcome. Co-authored-by: Jonas Toth <development@jonas-toth.eu> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/841 Reviewed-by: TheFox0x7 <thefox0x7@noreply.gitea.com> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Riccardo Förster <riccardo.foerster@sarad.de> Co-committed-by: Riccardo Förster <riccardo.foerster@sarad.de>
This commit is contained in:
committed by
Lunny Xiao
parent
f6d4b5fa4f
commit
1d1d9197ee
@@ -5,8 +5,12 @@ package cmd
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/cmd/issues"
|
||||
"code.gitea.io/tea/modules/context"
|
||||
"code.gitea.io/tea/modules/interact"
|
||||
@@ -16,6 +20,34 @@ import (
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
type labelData struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type issueData struct {
|
||||
ID int64 `json:"id"`
|
||||
Index int64 `json:"index"`
|
||||
Title string `json:"title"`
|
||||
State gitea.StateType `json:"state"`
|
||||
Created time.Time `json:"created"`
|
||||
Labels []labelData `json:"labels"`
|
||||
User string `json:"user"`
|
||||
Body string `json:"body"`
|
||||
Assignees []string `json:"assignees"`
|
||||
URL string `json:"url"`
|
||||
ClosedAt *time.Time `json:"closedAt"`
|
||||
Comments []commentData `json:"comments"`
|
||||
}
|
||||
|
||||
type commentData struct {
|
||||
ID int64 `json:"id"`
|
||||
Author string `json:"author"`
|
||||
Created time.Time `json:"created"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// CmdIssues represents to login a gitea server.
|
||||
var CmdIssues = cli.Command{
|
||||
Name: "issues",
|
||||
@@ -64,6 +96,14 @@ func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ctx.IsSet("output") {
|
||||
switch ctx.String("output") {
|
||||
case "json":
|
||||
return runIssueDetailAsJSON(ctx, issue)
|
||||
}
|
||||
}
|
||||
|
||||
print.IssueDetails(issue, reactions)
|
||||
|
||||
if issue.Comments > 0 {
|
||||
@@ -75,3 +115,61 @@ func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runIssueDetailAsJSON(ctx *context.TeaContext, issue *gitea.Issue) error {
|
||||
c := ctx.Login.Client()
|
||||
opts := gitea.ListIssueCommentOptions{ListOptions: flags.GetListOptions()}
|
||||
|
||||
labelSlice := make([]labelData, 0, len(issue.Labels))
|
||||
for _, label := range issue.Labels {
|
||||
labelSlice = append(labelSlice, labelData{label.Name, label.Color, label.Description})
|
||||
}
|
||||
|
||||
assigneesSlice := make([]string, 0, len(issue.Assignees))
|
||||
for _, assignee := range issue.Assignees {
|
||||
assigneesSlice = append(assigneesSlice, assignee.UserName)
|
||||
}
|
||||
|
||||
issueSlice := issueData{
|
||||
ID: issue.ID,
|
||||
Index: issue.Index,
|
||||
Title: issue.Title,
|
||||
State: issue.State,
|
||||
Created: issue.Created,
|
||||
User: issue.Poster.UserName,
|
||||
Body: issue.Body,
|
||||
Labels: labelSlice,
|
||||
Assignees: assigneesSlice,
|
||||
URL: issue.HTMLURL,
|
||||
ClosedAt: issue.Closed,
|
||||
Comments: make([]commentData, 0),
|
||||
}
|
||||
|
||||
if ctx.Bool("comments") {
|
||||
comments, _, err := c.ListIssueComments(ctx.Owner, ctx.Repo, issue.Index, opts)
|
||||
issueSlice.Comments = make([]commentData, 0, len(comments))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, comment := range comments {
|
||||
issueSlice.Comments = append(issueSlice.Comments, commentData{
|
||||
ID: comment.ID,
|
||||
Author: comment.Poster.UserName,
|
||||
Body: comment.Body, // Selected Field
|
||||
Created: comment.Created,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(issueSlice, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(ctx.Writer, "%s\n", jsonData)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user