diff options
author | Michael Muré <batolettre@gmail.com> | 2022-12-29 13:17:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-29 13:17:23 +0100 |
commit | c8ad1576b1472cb5723def810dfbcc30b40c70b7 (patch) | |
tree | e0a22258e29c12bec617b18298bea6bb377aede6 /commands/bug | |
parent | 211a038c6057c5cfdcbf4c7d4cc8b4e93d8dc8db (diff) | |
parent | 5844dd0a6a08f496e6018c0bad0b38d82a8846fc (diff) | |
download | git-bug-c8ad1576b1472cb5723def810dfbcc30b40c70b7.tar.gz |
Merge pull request #966 from MichaelMure/cmdjson
commands: share JSON creation
Diffstat (limited to 'commands/bug')
-rw-r--r-- | commands/bug/bug.go | 58 | ||||
-rw-r--r-- | commands/bug/bug_show.go | 65 | ||||
-rw-r--r-- | commands/bug/bug_test.go | 3 |
3 files changed, 8 insertions, 118 deletions
diff --git a/commands/bug/bug.go b/commands/bug/bug.go index bab040d8..a5ce11ed 100644 --- a/commands/bug/bug.go +++ b/commands/bug/bug.go @@ -1,7 +1,6 @@ package bugcmd import ( - "encoding/json" "fmt" "regexp" "strings" @@ -187,67 +186,16 @@ func repairQuery(args []string) string { return strings.Join(args, " ") } -type JSONBugExcerpt struct { - Id string `json:"id"` - HumanId string `json:"human_id"` - CreateTime cmdjson.Time `json:"create_time"` - EditTime cmdjson.Time `json:"edit_time"` - - Status string `json:"status"` - Labels []bug.Label `json:"labels"` - Title string `json:"title"` - Actors []cmdjson.Identity `json:"actors"` - Participants []cmdjson.Identity `json:"participants"` - Author cmdjson.Identity `json:"author"` - - Comments int `json:"comments"` - Metadata map[string]string `json:"metadata"` -} - func bugsJsonFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error { - jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts)) + jsonBugs := make([]cmdjson.BugExcerpt, len(bugExcerpts)) for i, b := range bugExcerpts { - jsonBug := JSONBugExcerpt{ - Id: b.Id().String(), - HumanId: b.Id().Human(), - CreateTime: cmdjson.NewTime(b.CreateTime(), b.CreateLamportTime), - EditTime: cmdjson.NewTime(b.EditTime(), b.EditLamportTime), - Status: b.Status.String(), - Labels: b.Labels, - Title: b.Title, - Comments: b.LenComments, - Metadata: b.CreateMetadata, - } - - author, err := env.Backend.Identities().ResolveExcerpt(b.AuthorId) + jsonBug, err := cmdjson.NewBugExcerpt(env.Backend, b) if err != nil { return err } - jsonBug.Author = cmdjson.NewIdentityFromExcerpt(author) - - jsonBug.Actors = make([]cmdjson.Identity, len(b.Actors)) - for i, element := range b.Actors { - actor, err := env.Backend.Identities().ResolveExcerpt(element) - if err != nil { - return err - } - jsonBug.Actors[i] = cmdjson.NewIdentityFromExcerpt(actor) - } - - jsonBug.Participants = make([]cmdjson.Identity, len(b.Participants)) - for i, element := range b.Participants { - participant, err := env.Backend.Identities().ResolveExcerpt(element) - if err != nil { - return err - } - jsonBug.Participants[i] = cmdjson.NewIdentityFromExcerpt(participant) - } - jsonBugs[i] = jsonBug } - jsonObject, _ := json.MarshalIndent(jsonBugs, "", " ") - env.Out.Printf("%s\n", jsonObject) - return nil + return env.Out.PrintJSON(jsonBugs) } func bugsCompactFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error { diff --git a/commands/bug/bug_show.go b/commands/bug/bug_show.go index 6cf50015..9f80120c 100644 --- a/commands/bug/bug_show.go +++ b/commands/bug/bug_show.go @@ -1,7 +1,6 @@ package bugcmd import ( - "encoding/json" "errors" "fmt" "strings" @@ -186,67 +185,9 @@ func showDefaultFormatter(env *execenv.Env, snapshot *bug.Snapshot) error { return nil } -type JSONBugSnapshot struct { - Id string `json:"id"` - HumanId string `json:"human_id"` - CreateTime cmdjson.Time `json:"create_time"` - EditTime cmdjson.Time `json:"edit_time"` - Status string `json:"status"` - Labels []bug.Label `json:"labels"` - Title string `json:"title"` - Author cmdjson.Identity `json:"author"` - Actors []cmdjson.Identity `json:"actors"` - Participants []cmdjson.Identity `json:"participants"` - Comments []JSONBugComment `json:"comments"` -} - -type JSONBugComment struct { - Id string `json:"id"` - HumanId string `json:"human_id"` - Author cmdjson.Identity `json:"author"` - Message string `json:"message"` -} - -func NewJSONComment(comment bug.Comment) JSONBugComment { - return JSONBugComment{ - Id: comment.CombinedId().String(), - HumanId: comment.CombinedId().Human(), - Author: cmdjson.NewIdentity(comment.Author), - Message: comment.Message, - } -} - -func showJsonFormatter(env *execenv.Env, snapshot *bug.Snapshot) error { - jsonBug := JSONBugSnapshot{ - Id: snapshot.Id().String(), - HumanId: snapshot.Id().Human(), - CreateTime: cmdjson.NewTime(snapshot.CreateTime, 0), - EditTime: cmdjson.NewTime(snapshot.EditTime(), 0), - Status: snapshot.Status.String(), - Labels: snapshot.Labels, - Title: snapshot.Title, - Author: cmdjson.NewIdentity(snapshot.Author), - } - - jsonBug.Actors = make([]cmdjson.Identity, len(snapshot.Actors)) - for i, element := range snapshot.Actors { - jsonBug.Actors[i] = cmdjson.NewIdentity(element) - } - - jsonBug.Participants = make([]cmdjson.Identity, len(snapshot.Participants)) - for i, element := range snapshot.Participants { - jsonBug.Participants[i] = cmdjson.NewIdentity(element) - } - - jsonBug.Comments = make([]JSONBugComment, len(snapshot.Comments)) - for i, comment := range snapshot.Comments { - jsonBug.Comments[i] = NewJSONComment(comment) - } - - jsonObject, _ := json.MarshalIndent(jsonBug, "", " ") - env.Out.Printf("%s\n", jsonObject) - - return nil +func showJsonFormatter(env *execenv.Env, snap *bug.Snapshot) error { + jsonBug := cmdjson.NewBugSnapshot(snap) + return env.Out.PrintJSON(jsonBug) } func showOrgModeFormatter(env *execenv.Env, snapshot *bug.Snapshot) error { diff --git a/commands/bug/bug_test.go b/commands/bug/bug_test.go index aef0346d..cb6a4373 100644 --- a/commands/bug/bug_test.go +++ b/commands/bug/bug_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/commands/bug/testenv" + "github.com/MichaelMure/git-bug/commands/cmdjson" ) func Test_repairQuery(t *testing.T) { @@ -95,7 +96,7 @@ $` require.NoError(t, runBug(env, opts, []string{})) - var bugs []JSONBugExcerpt + var bugs []cmdjson.BugExcerpt require.NoError(t, json.Unmarshal(env.Out.Bytes(), &bugs)) require.Len(t, bugs, 1) |