Skip to content

Commit 67b6775

Browse files
committed
feat: Support Conflicting JSON Fields
- Adds support for --filter with --json so you can specify conflicting customfields - Adds --no-warnings with --json if you don't care about conflicts Co-authored by Claude AI
1 parent af88f5a commit 67b6775

File tree

5 files changed

+528
-80
lines changed

5 files changed

+528
-80
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ $ jira issue list --plain
195195
# List recent issues in raw JSON format
196196
$ jira issue list --raw
197197

198+
# List issues in JSON with human-readable custom field names
199+
$ jira issue list --json
200+
201+
# Filter JSON output to specific fields
202+
$ jira issue list --json --filter "key,fields.summary,fields.status.name,fields.storyPoints"
203+
204+
# Use customfield IDs in filter to bypass naming collisions
205+
$ jira issue list --json --filter "key,fields.customfield_10001"
206+
207+
# Suppress collision warnings if you don't care about skipped fields
208+
$ jira issue list --json --no-warnings
209+
198210
# List recent issues in csv format
199211
$ jira issue list --csv
200212

@@ -437,6 +449,15 @@ not be the latest one if you for some reason have more than 5k comments in a tic
437449
```sh
438450
# Show 5 recent comments when viewing the issue
439451
$ jira issue view ISSUE-1 --comments 5
452+
453+
# Get JSON output with human-readable custom field names
454+
$ jira issue view ISSUE-1 --json
455+
456+
# Filter JSON to specific fields
457+
$ jira issue view ISSUE-1 --json --filter "key,fields.summary,fields.storyPoints,fields.status.name"
458+
459+
# Suppress collision warnings
460+
$ jira issue view ISSUE-1 --json --no-warnings
440461
```
441462

442463
#### Link

internal/cmd/issue/list/list.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,23 @@ func loadList(cmd *cobra.Command, args []string) {
144144
cmdutil.ExitIfError(err)
145145

146146
if jsonOutput {
147-
outputJSON(issues)
147+
// Get filter fields
148+
filterStr, err := cmd.Flags().GetString("filter")
149+
cmdutil.ExitIfError(err)
150+
151+
var filterFields []string
152+
if filterStr != "" {
153+
filterFields = strings.Split(filterStr, ",")
154+
// Trim whitespace from each field
155+
for i := range filterFields {
156+
filterFields[i] = strings.TrimSpace(filterFields[i])
157+
}
158+
}
159+
160+
noWarnings, err := cmd.Flags().GetBool("no-warnings")
161+
cmdutil.ExitIfError(err)
162+
163+
outputJSON(issues, filterFields, noWarnings)
148164
return
149165
}
150166

@@ -220,7 +236,7 @@ func outputRawJSON(issues []*jira.Issue) {
220236
fmt.Println(string(data))
221237
}
222238

223-
func outputJSON(issues []*jira.Issue) {
239+
func outputJSON(issues []*jira.Issue, filter []string, noWarnings bool) {
224240
// Marshal issues to JSON first
225241
rawJSON, err := json.Marshal(issues)
226242
if err != nil {
@@ -235,14 +251,21 @@ func outputJSON(issues []*jira.Issue) {
235251
fieldMappings = []jira.IssueTypeField{}
236252
}
237253

238-
// Convert custom field IDs to readable names
239-
jsonOutput, err := jira.TransformIssueFields(rawJSON, fieldMappings)
254+
// Convert custom field IDs to readable names and apply filter
255+
result, err := jira.TransformIssueFields(rawJSON, fieldMappings, filter)
240256
if err != nil {
241257
cmdutil.Failed("Failed to format JSON output: %s", err)
242258
return
243259
}
244260

245-
fmt.Println(string(jsonOutput))
261+
// Display warnings if any (unless suppressed)
262+
if !noWarnings {
263+
for _, warning := range result.Warnings {
264+
cmdutil.Warn(warning)
265+
}
266+
}
267+
268+
fmt.Println(string(result.Data))
246269
}
247270

248271
// SetFlags sets flags supported by a list command.
@@ -283,6 +306,8 @@ func SetFlags(cmd *cobra.Command) {
283306
cmd.Flags().Uint("comments", 1, "Show N comments when viewing the issue")
284307
cmd.Flags().Bool("raw", false, "Print raw JSON output")
285308
cmd.Flags().Bool("json", false, "Print JSON output with human-readable custom field names")
309+
cmd.Flags().String("filter", "", "Comma-separated list of fields to include in JSON output (e.g., 'key,fields.summary,fields.status.name'). Only works with --json")
310+
cmd.Flags().Bool("no-warnings", false, "Suppress warnings about field name collisions. Only works with --json")
286311
cmd.Flags().Bool("csv", false, "Print output in CSV format")
287312

288313
if cmd.HasParent() && cmd.Parent().Name() != "sprint" {

internal/cmd/issue/view/view.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package view
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/spf13/cobra"
78
"github.com/spf13/viper"
@@ -27,11 +28,12 @@ $ jira issue view ISSUE-1 --raw
2728
# Get JSON output with human-readable custom field names
2829
$ jira issue view ISSUE-1 --json`
2930

30-
flagRaw = "raw"
31-
flagJSON = "json"
32-
flagDebug = "debug"
33-
flagComments = "comments"
34-
flagPlain = "plain"
31+
flagRaw = "raw"
32+
flagJSON = "json"
33+
flagDebug = "debug"
34+
flagComments = "comments"
35+
flagPlain = "plain"
36+
flagNoWarnings = "no-warnings"
3537

3638
configProject = "project.key"
3739
configServer = "server"
@@ -58,6 +60,8 @@ func NewCmdView() *cobra.Command {
5860
cmd.Flags().Bool(flagPlain, false, "Display output in plain mode")
5961
cmd.Flags().Bool(flagRaw, false, "Print raw Jira API response")
6062
cmd.Flags().Bool(flagJSON, false, "Print JSON output with human-readable custom field names")
63+
cmd.Flags().String("filter", "", "Comma-separated list of fields to include in JSON output (e.g., 'key,fields.summary,fields.status.name'). Only works with --json")
64+
cmd.Flags().Bool(flagNoWarnings, false, "Suppress warnings about field name collisions. Only works with --json")
6165

6266
return &cmd
6367
}
@@ -121,14 +125,37 @@ func viewJSON(cmd *cobra.Command, args []string) {
121125
fieldMappings = []jira.IssueTypeField{}
122126
}
123127

124-
// Convert custom field IDs to readable names
125-
jsonOutput, err := jira.TransformIssueFields([]byte(apiResp), fieldMappings)
128+
// Get filter fields
129+
filterStr, err := cmd.Flags().GetString("filter")
130+
cmdutil.ExitIfError(err)
131+
132+
var filterFields []string
133+
if filterStr != "" {
134+
filterFields = strings.Split(filterStr, ",")
135+
// Trim whitespace from each field
136+
for i := range filterFields {
137+
filterFields[i] = strings.TrimSpace(filterFields[i])
138+
}
139+
}
140+
141+
// Convert custom field IDs to readable names and apply filter
142+
result, err := jira.TransformIssueFields([]byte(apiResp), fieldMappings, filterFields)
126143
if err != nil {
127144
cmdutil.Failed("Failed to format JSON output: %s", err)
128145
return
129146
}
130147

131-
fmt.Println(string(jsonOutput))
148+
// Display warnings if any (unless suppressed)
149+
noWarnings, err := cmd.Flags().GetBool(flagNoWarnings)
150+
cmdutil.ExitIfError(err)
151+
152+
if !noWarnings {
153+
for _, warning := range result.Warnings {
154+
cmdutil.Warn(warning)
155+
}
156+
}
157+
158+
fmt.Println(string(result.Data))
132159
}
133160

134161
func viewPretty(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)