diff --git a/commands/ci/status/status.go b/commands/ci/status/status.go index 94e8f2eecbbfd6751d42be923ac40e019248bdaf..f37fc53eab0fdaed17bb6fda4642087c945aeb03 100644 --- a/commands/ci/status/status.go +++ b/commands/ci/status/status.go @@ -50,35 +50,48 @@ func NewCmdStatus(f *cmdutils.Factory) *cobra.Command { live, _ := cmd.Flags().GetBool("live") compact, _ := cmd.Flags().GetBool("compact") - if branch == "" { - branch, err = git.CurrentBranch() - if err != nil { - return err - } - dbg.Debug("Current branch:", branch) - } - var repo glrepo.Interface - branchConfig := git.ReadBranchConfig(branch) - if branchConfig.RemoteName == "" { - repo, err = f.BaseRepo() + repoGlobal, err := cmdutils.GlobalRepo(cmd) + if err != nil { + return err + } + if repoGlobal != "" { + repo, err = glrepo.FromFullName(repoGlobal) if err != nil { return err } } else { - remotes, err := f.Remotes() - if err != nil { - return err - } - repo, err = remotes.FindByName(branchConfig.RemoteName) - if err != nil { - redCheck := c.Red("x") - fmt.Fprintf(f.IO.StdOut, "%s Remote '%s' for branch '%s' is gone.\n", redCheck, branchConfig.RemoteName, branch) - return err + branchConfig := git.ReadBranchConfig(branch) + if branchConfig.RemoteName == "" { + repo, err = f.BaseRepo() + if err != nil { + return err + } + if branch == "" { + branch, err = git.CurrentBranch() + if err != nil { + return err + } + } + } else { + remotes, err := f.Remotes() + if err != nil { + return err + } + repo, err = remotes.FindByName(branchConfig.RemoteName) + if err != nil { + redCheck := c.Red("x") + fmt.Fprintf(f.IO.StdOut, "%s Remote '%s' for branch '%s' is gone.\n", redCheck, branchConfig.RemoteName, branch) + return err + } + // XXX uncomment and test this in another MR + // branch = branchConfig.MergeRef } } + repoName := repo.FullName() - dbg.Debug("Repository:", repoName) + dbg.Debug("Repo:", repoName) + dbg.Debug("Branch:", branch) runningPipeline, err := api.GetLatestPipeline(apiClient, repoName, branch) if err != nil { @@ -179,6 +192,8 @@ func NewCmdStatus(f *cmdutils.Factory) *cobra.Command { }, } + cmdutils.AddGlobalRepoFlag(pipelineStatusCmd) + pipelineStatusCmd.Flags().BoolP("live", "l", false, "Show status in real time until the pipeline ends.") pipelineStatusCmd.Flags().BoolP("compact", "c", false, "Show status in compact format.") pipelineStatusCmd.Flags().StringP("branch", "b", "", "Check pipeline status for a branch. Default: current branch.") diff --git a/commands/ci/status/status_test.go b/commands/ci/status/status_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c1cd26f36a78cbfeb2a7ba7ea61b150274f9ef47 --- /dev/null +++ b/commands/ci/status/status_test.go @@ -0,0 +1,47 @@ +package status + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "gitlab.com/gitlab-org/cli/commands/cmdtest" + "gitlab.com/gitlab-org/cli/commands/cmdutils" +) + +func TestStatus(t *testing.T) { + /* + create fixture + + - no repo + - repo with main + - repo with branch + - repo with branch that is tracking remote + + create tests + + - if -R is given, it overrides repo + - if branch is given, set branch + - if local repo exists, branch from local + - no repo, default branch + + - no -R, take current repo branch + - if branch param, use param + - if branch tracks remote, use remote + */ + tempDir := t.TempDir() + t.Chdir(tempDir) + + cmd := NewCmdStatus(cmdutils.NewFactory()) + cmdtest.CmdSetup(cmd) + t.Run("no repo", func(t *testing.T) { + err := cmd.Execute() + assert.Error(t, err) + }) + t.Run("no repo with -R", func(t *testing.T) { + cmd.SetArgs([]string{"-R", "forkrepo"}) + err := cmd.Execute() + assert.NoError(t, err) + }) + + // t.Log(os.Getwd()) +} diff --git a/commands/cluster/agent/bootstrap/agent_bootstrap_test.go b/commands/cluster/agent/bootstrap/agent_bootstrap_test.go index cd607395664f3aad088a622951afcf27d5e4f5b6..8fafc4d30d6ed7f32c523d4246649119e88d3794 100644 --- a/commands/cluster/agent/bootstrap/agent_bootstrap_test.go +++ b/commands/cluster/agent/bootstrap/agent_bootstrap_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/shlex" gitlab "gitlab.com/gitlab-org/api/client-go" glab_api "gitlab.com/gitlab-org/cli/api" + "gitlab.com/gitlab-org/cli/commands/cmdtest" "gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/internal/glrepo" "gitlab.com/gitlab-org/cli/pkg/iostreams" @@ -863,9 +864,7 @@ func setupCmdExec(t *testing.T) (execFunc, *MockAPI, *MockWriter, *MockWriter, * cmd.SetOut(mockStdout) cmd.SetErr(mockStderr) - // Set on root cmd, thus we also need to set it here. - // cmd.SilenceErrors = true - cmd.SilenceUsage = true + cmdtest.CmdSetup(cmd) exec := func(cli string) error { argv, err := shlex.Split(cli) diff --git a/commands/cmdtest/helper.go b/commands/cmdtest/helper.go index f537b9fe4cc38590f26c9602eeefc54a26c77d8d..fe3c8d16df2588daec91354a0b2ca64c8c0d93e8 100644 --- a/commands/cmdtest/helper.go +++ b/commands/cmdtest/helper.go @@ -53,6 +53,11 @@ func init() { } } +// Sets root command defaults for tests +func CmdSetup(cmd *cobra.Command) { + cmd.SilenceUsage = true +} + func InitTest(m *testing.M, suffix string) { // Build a glab binary with test symbols. If the parent test binary was run // with coverage enabled, enable coverage on the child binary, too. diff --git a/commands/cmdutils/global_flags.go b/commands/cmdutils/global_flags.go new file mode 100644 index 0000000000000000000000000000000000000000..3ea72c011ceb2772a5929892863b32eda237f7eb --- /dev/null +++ b/commands/cmdutils/global_flags.go @@ -0,0 +1,32 @@ +package cmdutils + +import ( + "os" + + "github.com/spf13/cobra" + "gitlab.com/gitlab-org/cli/pkg/dbg" +) + +func AddGlobalRepoFlag(cmd *cobra.Command) { + cmd.InheritedFlags().StringP("repo", "R", "", "Select another repository. Can use either `OWNER/REPO` or `GROUP/NAMESPACE/REPO` format. Also accepts full URL or Git URL.") +} + +func GlobalRepo(cmd *cobra.Command) (string, error) { + repo, err := cmd.InheritedFlags().GetString("repo") + if err != nil { + return "", err + } + if repo != "" { + dbg.Debug("Repo from global flag:", repo) + return repo, nil + } + + if repoFromEnv := os.Getenv("GITLAB_REPO"); repoFromEnv != "" { + dbg.Debug("Repo from ENV:", repo) + return repoFromEnv, nil + } + + // XXX cleanup f.RepoOverride() calls + // return f.RepoOverride(repoOverride) + return "", nil +}