Skip to content

Commit

Permalink
Fix up NoHeaders linter errors in project commands (cli#8223)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe authored Oct 20, 2023
1 parent 7738b61 commit d70d798
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 58 deletions.
15 changes: 7 additions & 8 deletions pkg/cmd/project/field-list/field_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type listOpts struct {

type listConfig struct {
io *iostreams.IOStreams
tp *tableprinter.TablePrinter
client *queries.Client
opts listOpts
}
Expand Down Expand Up @@ -52,10 +51,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(config listConfig) error) *cobra.C
opts.number = int32(num)
}

t := tableprinter.New(f.IOStreams, tableprinter.WithHeader("Name", "Data type", "ID"))
config := listConfig{
io: f.IOStreams,
tp: t,
client: client,
opts: opts,
}
Expand Down Expand Up @@ -109,14 +106,16 @@ func printResults(config listConfig, fields []queries.ProjectField, login string
return cmdutil.NewNoResultsError(fmt.Sprintf("Project %d for owner %s has no fields", config.opts.number, login))
}

tp := tableprinter.New(config.io, tableprinter.WithHeader("Name", "Data type", "ID"))

for _, f := range fields {
config.tp.AddField(f.Name())
config.tp.AddField(f.Type())
config.tp.AddField(f.ID(), tableprinter.WithTruncate(nil))
config.tp.EndRow()
tp.AddField(f.Name())
tp.AddField(f.Type())
tp.AddField(f.ID(), tableprinter.WithTruncate(nil))
tp.EndRow()
}

return config.tp.Render()
return tp.Render()
}

func printJSON(config listConfig, project *queries.Project) error {
Expand Down
105 changes: 96 additions & 9 deletions pkg/cmd/project/field-list/field_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fieldlist
import (
"testing"

"github.com/cli/cli/v2/internal/tableprinter"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
Expand Down Expand Up @@ -87,6 +87,101 @@ func TestNewCmdList(t *testing.T) {
}
}

func TestRunList_User_tty(t *testing.T) {
defer gock.Off()
// gock.Observe(gock.DumpRequest)

// get user ID
gock.New("https://api.github.com").
Post("/graphql").
MatchType("json").
JSON(map[string]interface{}{
"query": "query UserOrgOwner.*",
"variables": map[string]interface{}{
"login": "monalisa",
},
}).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"user": map[string]interface{}{
"id": "an ID",
},
},
"errors": []interface{}{
map[string]interface{}{
"type": "NOT_FOUND",
"path": []string{"organization"},
},
},
})

// list project fields
gock.New("https://api.github.com").
Post("/graphql").
JSON(map[string]interface{}{
"query": "query UserProject.*",
"variables": map[string]interface{}{
"login": "monalisa",
"number": 1,
"firstItems": queries.LimitMax,
"afterItems": nil,
"firstFields": queries.LimitDefault,
"afterFields": nil,
},
}).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"user": map[string]interface{}{
"projectV2": map[string]interface{}{
"fields": map[string]interface{}{
"nodes": []map[string]interface{}{
{
"__typename": "ProjectV2Field",
"name": "FieldTitle",
"id": "field ID",
},
{
"__typename": "ProjectV2SingleSelectField",
"name": "Status",
"id": "status ID",
},
{
"__typename": "ProjectV2IterationField",
"name": "Iterations",
"id": "iteration ID",
},
},
},
},
},
},
})

client := queries.NewTestClient()

ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := listConfig{
opts: listOpts{
number: 1,
owner: "monalisa",
},
client: client,
io: ios,
}

err := runList(config)
assert.NoError(t, err)
assert.Equal(t, heredoc.Doc(`
NAME DATA TYPE ID
FieldTitle ProjectV2Field field ID
Status ProjectV2SingleSelectField status ID
Iterations ProjectV2IterationField iteration ID
`), stdout.String())
}

func TestRunList_User(t *testing.T) {
defer gock.Off()
// gock.Observe(gock.DumpRequest)
Expand Down Expand Up @@ -163,8 +258,6 @@ func TestRunList_User(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "monalisa",
Expand Down Expand Up @@ -257,8 +350,6 @@ func TestRunList_Org(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "github",
Expand Down Expand Up @@ -341,8 +432,6 @@ func TestRunList_Me(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "@me",
Expand Down Expand Up @@ -409,8 +498,6 @@ func TestRunList_Empty(t *testing.T) {

ios, _, _, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "@me",
Expand Down
21 changes: 10 additions & 11 deletions pkg/cmd/project/item-list/item_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type listOpts struct {

type listConfig struct {
io *iostreams.IOStreams
tp *tableprinter.TablePrinter
client *queries.Client
opts listOpts
}
Expand Down Expand Up @@ -52,10 +51,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(config listConfig) error) *cobra.C
opts.number = int32(num)
}

t := tableprinter.New(f.IOStreams, tableprinter.WithHeader("Type", "Title", "Number", "Repository", "ID"))
config := listConfig{
io: f.IOStreams,
tp: t,
client: client,
opts: opts,
}
Expand Down Expand Up @@ -108,20 +105,22 @@ func printResults(config listConfig, items []queries.ProjectItem, login string)
return cmdutil.NewNoResultsError(fmt.Sprintf("Project %d for owner %s has no items", config.opts.number, login))
}

tp := tableprinter.New(config.io, tableprinter.WithHeader("Type", "Title", "Number", "Repository", "ID"))

for _, i := range items {
config.tp.AddField(i.Type())
config.tp.AddField(i.Title())
tp.AddField(i.Type())
tp.AddField(i.Title())
if i.Number() == 0 {
config.tp.AddField("")
tp.AddField("")
} else {
config.tp.AddField(strconv.Itoa(i.Number()))
tp.AddField(strconv.Itoa(i.Number()))
}
config.tp.AddField(i.Repo())
config.tp.AddField(i.ID(), tableprinter.WithTruncate(nil))
config.tp.EndRow()
tp.AddField(i.Repo())
tp.AddField(i.ID(), tableprinter.WithTruncate(nil))
tp.EndRow()
}

return config.tp.Render()
return tp.Render()
}

func printJSON(config listConfig, project *queries.Project) error {
Expand Down
117 changes: 110 additions & 7 deletions pkg/cmd/project/item-list/item_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package itemlist
import (
"testing"

"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"

"github.com/cli/cli/v2/internal/tableprinter"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
Expand Down Expand Up @@ -88,6 +88,115 @@ func TestNewCmdList(t *testing.T) {
}
}

func TestRunList_User_tty(t *testing.T) {
defer gock.Off()
// gock.Observe(gock.DumpRequest)

// get user ID
gock.New("https://api.github.com").
Post("/graphql").
MatchType("json").
JSON(map[string]interface{}{
"query": "query UserOrgOwner.*",
"variables": map[string]interface{}{
"login": "monalisa",
},
}).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"user": map[string]interface{}{
"id": "an ID",
},
},
"errors": []interface{}{
map[string]interface{}{
"type": "NOT_FOUND",
"path": []string{"organization"},
},
},
})

// list project items
gock.New("https://api.github.com").
Post("/graphql").
JSON(map[string]interface{}{
"query": "query UserProjectWithItems.*",
"variables": map[string]interface{}{
"firstItems": queries.LimitDefault,
"afterItems": nil,
"firstFields": queries.LimitMax,
"afterFields": nil,
"login": "monalisa",
"number": 1,
},
}).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"user": map[string]interface{}{
"projectV2": map[string]interface{}{
"items": map[string]interface{}{
"nodes": []map[string]interface{}{
{
"id": "issue ID",
"content": map[string]interface{}{
"__typename": "Issue",
"title": "an issue",
"number": 1,
"repository": map[string]string{
"nameWithOwner": "cli/go-gh",
},
},
},
{
"id": "pull request ID",
"content": map[string]interface{}{
"__typename": "PullRequest",
"title": "a pull request",
"number": 2,
"repository": map[string]string{
"nameWithOwner": "cli/go-gh",
},
},
},
{
"id": "draft issue ID",
"content": map[string]interface{}{
"title": "draft issue",
"__typename": "DraftIssue",
},
},
},
},
},
},
},
})

client := queries.NewTestClient()

ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := listConfig{
opts: listOpts{
number: 1,
owner: "monalisa",
},
client: client,
io: ios,
}

err := runList(config)
assert.NoError(t, err)
assert.Equal(t, heredoc.Doc(`
TYPE TITLE NUMBER REPOSITORY ID
Issue an issue 1 cli/go-gh issue ID
PullRequest a pull request 2 cli/go-gh pull request ID
DraftIssue draft issue draft issue ID
`), stdout.String())
}

func TestRunList_User(t *testing.T) {
defer gock.Off()
// gock.Observe(gock.DumpRequest)
Expand Down Expand Up @@ -178,8 +287,6 @@ func TestRunList_User(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "monalisa",
Expand Down Expand Up @@ -286,8 +393,6 @@ func TestRunList_Org(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "github",
Expand Down Expand Up @@ -384,8 +489,6 @@ func TestRunList_Me(t *testing.T) {

ios, _, stdout, _ := iostreams.Test()
config := listConfig{
//nolint:staticcheck // SA1019: tableprinter.NoHeaders temporarily allowed in project tests.
tp: tableprinter.New(ios, tableprinter.NoHeader),
opts: listOpts{
number: 1,
owner: "@me",
Expand Down
Loading

0 comments on commit d70d798

Please sign in to comment.