make IDs cyan, add prompter test

This commit is contained in:
vaindil 2023-07-06 14:56:55 -04:00
parent ab921f96e6
commit d82c1193b3
5 changed files with 116 additions and 36 deletions

View file

@ -158,7 +158,7 @@ func listRun(opts *ListOptions) error {
tp.HeaderRow("ID", "NAME", "SOURCE", "STATUS", "RULES")
for _, rs := range result.Rulesets {
tp.AddField(strconv.Itoa(rs.DatabaseId))
tp.AddField(strconv.Itoa(rs.DatabaseId), tableprinter.WithColor(cs.Cyan))
tp.AddField(rs.Name, tableprinter.WithColor(cs.Bold))
tp.AddField(shared.RulesetSource(rs))
tp.AddField(strings.ToLower(rs.Enforcement))

View file

@ -0,0 +1,41 @@
{
"data": {
"level": {
"rulesets": {
"totalCount": 2,
"nodes": [
{
"databaseId": 74,
"name": "My Org Ruleset",
"target": "BRANCH",
"enforcement": "EVALUATE",
"source": {
"__typename": "Organization",
"owner": "my-owner"
},
"rules": {
"totalCount": 3
}
},
{
"databaseId": 42,
"name": "Test Ruleset",
"target": "BRANCH",
"enforcement": "ACTIVE",
"source": {
"__typename": "Repository",
"owner": "my-owner/repo-name"
},
"rules": {
"totalCount": 3
}
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "Mg"
}
}
}
}
}

View file

@ -4,7 +4,7 @@
"target": "branch",
"source_type": "Organization",
"source": "my-owner",
"enforcement": "disabled",
"enforcement": "evaluate",
"conditions": {
"ref_name": {
"exclude": [],

View file

@ -119,6 +119,7 @@ func viewRun(opts *ViewOptions) error {
}
hostname, _ := ghAuth.DefaultHost()
cs := opts.IO.ColorScheme()
if opts.InteractiveMode {
var rsList *shared.RulesetList
@ -137,7 +138,7 @@ func viewRun(opts *ViewOptions) error {
return shared.NoRulesetsFoundError(opts.Organization, repoI, opts.IncludeParents)
}
rs, err := selectRulesetID(rsList, opts.Prompter)
rs, err := selectRulesetID(rsList, opts.Prompter, cs)
if err != nil {
return err
}
@ -163,7 +164,6 @@ func viewRun(opts *ViewOptions) error {
return err
}
cs := opts.IO.ColorScheme()
w := opts.IO.Out
if opts.WebMode {
@ -179,7 +179,7 @@ func viewRun(opts *ViewOptions) error {
}
fmt.Fprintf(w, "\n%s\n", cs.Bold(rs.Name))
fmt.Fprintf(w, "ID: %d\n", rs.Id)
fmt.Fprintf(w, "ID: %s\n", cs.Cyan(strconv.Itoa(rs.Id)))
fmt.Fprintf(w, "Source: %s (%s)\n", rs.Source, rs.SourceType)
fmt.Fprint(w, "Enforcement: ")
@ -247,12 +247,12 @@ func viewRun(opts *ViewOptions) error {
return nil
}
func selectRulesetID(rsList *shared.RulesetList, p prompter.Prompter) (*shared.RulesetGraphQL, error) {
func selectRulesetID(rsList *shared.RulesetList, p prompter.Prompter, cs *iostreams.ColorScheme) (*shared.RulesetGraphQL, error) {
rulesets := make([]string, len(rsList.Rulesets))
for i, rs := range rsList.Rulesets {
s := fmt.Sprintf(
"%d: %s | %s | contains %s | configured in %s",
rs.DatabaseId,
"%s: %s | %s | contains %s | configured in %s",
cs.Cyan(strconv.Itoa(rs.DatabaseId)),
rs.Name,
strings.ToLower(rs.Enforcement),
text.Pluralize(rs.Rules.TotalCount, "rule"),

View file

@ -9,6 +9,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
@ -149,15 +150,36 @@ func Test_NewCmdView(t *testing.T) {
}
func Test_viewRun(t *testing.T) {
repoRulesetStdout := heredoc.Doc(`
Test Ruleset
ID: 42
Source: my-owner/repo-name (Repository)
Enforcement: Active
Bypass List
- OrganizationAdmin (ID: 1), mode: always
- RepositoryRole (ID: 5), mode: always
Conditions
- ref_name: [exclude: []] [include: [~ALL]]
Rules
- commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com]
- commit_message_pattern: [name: ] [negate: false] [operator: contains] [pattern: asdf]
- creation
`)
tests := []struct {
name string
isTTY bool
opts ViewOptions
httpStubs func(*httpmock.Registry)
wantErr string
wantStdout string
wantStderr string
wantBrowse string
name string
isTTY bool
opts ViewOptions
httpStubs func(*httpmock.Registry)
prompterStubs func(*prompter.MockPrompter)
wantErr string
wantStdout string
wantStderr string
wantBrowse string
}{
{
name: "view repo ruleset",
@ -165,25 +187,7 @@ func Test_viewRun(t *testing.T) {
opts: ViewOptions{
ID: "42",
},
wantStdout: heredoc.Doc(`
Test Ruleset
ID: 42
Source: my-owner/repo-name (Repository)
Enforcement: Active
Bypass List
- OrganizationAdmin (ID: 1), mode: always
- RepositoryRole (ID: 5), mode: always
Conditions
- ref_name: [exclude: []] [include: [~ALL]]
Rules
- commit_author_email_pattern: [name: ] [negate: false] [operator: ends_with] [pattern: @example.com]
- commit_message_pattern: [name: ] [negate: false] [operator: contains] [pattern: asdf]
- creation
`),
wantStdout: repoRulesetStdout,
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"),
@ -205,7 +209,7 @@ func Test_viewRun(t *testing.T) {
My Org Ruleset
ID: 74
Source: my-owner (Organization)
Enforcement: Disabled
Enforcement: Evaluate Mode (not enforced)
Bypass List
This ruleset cannot be bypassed
@ -228,6 +232,35 @@ func Test_viewRun(t *testing.T) {
wantStderr: "",
wantBrowse: "",
},
{
name: "prompter",
isTTY: true,
opts: ViewOptions{
InteractiveMode: true,
},
wantStdout: repoRulesetStdout,
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepoRulesetList\b`),
httpmock.FileResponse("./fixtures/rulesetViewMultiple.json"),
)
reg.Register(
httpmock.REST("GET", "repos/my-owner/repo-name/rulesets/42"),
httpmock.FileResponse("./fixtures/rulesetViewRepo.json"),
)
},
prompterStubs: func(pm *prompter.MockPrompter) {
const repoRuleset = "42: Test Ruleset | active | contains 3 rules | configured in my-owner/repo-name (repo)"
pm.RegisterSelect("Which ruleset would you like to view?",
[]string{
"74: My Org Ruleset | evaluate | contains 3 rules | configured in my-owner (org)",
repoRuleset,
},
func(_, _ string, opts []string) (int, error) {
return prompter.IndexFor(opts, repoRuleset)
})
},
},
{
name: "web mode, TTY, repo",
isTTY: true,
@ -289,6 +322,12 @@ func Test_viewRun(t *testing.T) {
ios.SetStdinTTY(tt.isTTY)
ios.SetStderrTTY(tt.isTTY)
pm := prompter.NewMockPrompter(t)
if tt.prompterStubs != nil {
tt.prompterStubs(pm)
}
tt.opts.Prompter = pm
reg := &httpmock.Registry{}
defer reg.Verify(t)
if tt.httpStubs != nil {