diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 2929ca720..6957ff7b6 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -31,6 +31,7 @@ import ( releaseCmd "github.com/cli/cli/v2/pkg/cmd/release" repoCmd "github.com/cli/cli/v2/pkg/cmd/repo" creditsCmd "github.com/cli/cli/v2/pkg/cmd/repo/credits" + rulesetCmd "github.com/cli/cli/v2/pkg/cmd/ruleset" runCmd "github.com/cli/cli/v2/pkg/cmd/run" searchCmd "github.com/cli/cli/v2/pkg/cmd/search" secretCmd "github.com/cli/cli/v2/pkg/cmd/secret" @@ -152,6 +153,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) (*cobra.Command, cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory)) cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory)) cmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory)) + cmd.AddCommand(rulesetCmd.NewCmdRuleset(&repoResolvingCmdFactory)) cmd.AddCommand(runCmd.NewCmdRun(&repoResolvingCmdFactory)) cmd.AddCommand(workflowCmd.NewCmdWorkflow(&repoResolvingCmdFactory)) cmd.AddCommand(labelCmd.NewCmdLabel(&repoResolvingCmdFactory)) diff --git a/pkg/cmd/ruleset/check/check.go b/pkg/cmd/ruleset/check/check.go index a143981b1..34c708c69 100644 --- a/pkg/cmd/ruleset/check/check.go +++ b/pkg/cmd/ruleset/check/check.go @@ -1,9 +1,14 @@ package check import ( + "context" + "fmt" "net/http" + "net/url" "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmdutil" @@ -16,8 +21,10 @@ type CheckOptions struct { IO *iostreams.IOStreams Config func() (config.Config, error) BaseRepo func() (ghrepo.Interface, error) + Git *git.Client - Branch string + Branch string + Default bool } func NewCmdCheck(f *cmdutil.Factory, runF func(*CheckOptions) error) *cobra.Command { @@ -25,6 +32,7 @@ func NewCmdCheck(f *cmdutil.Factory, runF func(*CheckOptions) error) *cobra.Comm IO: f.IOStreams, Config: f.Config, HttpClient: f.HttpClient, + Git: f.GitClient, } cmd := &cobra.Command{ Use: "check []", @@ -48,18 +56,65 @@ func NewCmdCheck(f *cmdutil.Factory, runF func(*CheckOptions) error) *cobra.Comm return runF(opts) } + if opts.Branch != "" && opts.Default { + return cmdutil.FlagErrorf( + "branch argument '%s' and --default mutually exclusive", opts.Branch) + } + return checkRun(opts) }, } + cmd.Flags().BoolVar(&opts.Default, "default", false, "Check rules on default branch") + return cmd } func checkRun(opts *CheckOptions) error { - // TODO sniff local branch if opts.Branch is empty // TODO ask about pushing (if interactive) // TODO error if not interactive and --push not specified + // TODO parsing for errors on push + + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + client := api.NewClientFromHTTP(httpClient) + + repoI, err := opts.BaseRepo() + if err != nil { + return err + } + + git := opts.Git + + if opts.Default { + repo, err := api.GitHubRepo(client, repoI) + if err != nil { + return fmt.Errorf("could not get repository information: %w", err) + } + opts.Branch = repo.DefaultBranchRef.Name + } + + if opts.Branch == "" { + opts.Branch, err = git.CurrentBranch(context.Background()) + if err != nil { + return fmt.Errorf("could not determine current branch: %w", err) + } + } + + var lol interface{} + + endpoint := fmt.Sprintf("repos/%s/%s/rules/branches/%s", repoI.RepoOwner(), repoI.RepoName(), url.PathEscape(opts.Branch)) + + if err = client.REST(repoI.RepoHost(), "GET", endpoint, nil, &lol); err != nil { + return fmt.Errorf("GET %s failed: %w", endpoint, err) + } + + // TODO handle 404s gracefully + // TODO actually parse JSON + + fmt.Printf("DBG %#v\n", lol) - // is the --push redundant? like, it needs to be specified every time for scripted use. can i tell if a branch is up to date with remote without a push? i could figure that out i think and then would know a push wasn't needed (but it will require a fetch per invocation. that seems fine?) return nil } diff --git a/pkg/cmd/ruleset/ruleset.go b/pkg/cmd/ruleset/ruleset.go index 2fa86684c..266fbc93e 100644 --- a/pkg/cmd/ruleset/ruleset.go +++ b/pkg/cmd/ruleset/ruleset.go @@ -2,6 +2,7 @@ package ruleset import ( "github.com/MakeNowJust/heredoc" + cmdCheck "github.com/cli/cli/v2/pkg/cmd/ruleset/check" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" ) @@ -20,7 +21,7 @@ func NewCmdRuleset(f *cmdutil.Factory) *cobra.Command { cmdutil.EnableRepoOverride(cmd, f) // cmd.AddCommand(cmdList.NewCmdList(f, nil) // cmd.AddCommand(cmdList.NewCmdView(f, nil) - // cmd.AddCommand(cmdCheck.NewCmdCheck(f, nil) + cmd.AddCommand(cmdCheck.NewCmdCheck(f, nil)) return cmd }