From 078048fe8cf177b9544af739d5159cc176f83a32 Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Tue, 19 Dec 2023 18:23:24 +0200 Subject: [PATCH] fix(cmd): Use default token if not able to back up Signed-off-by: Cezar Craciunoiu --- cmd/governctl/pr/merge.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/governctl/pr/merge.go b/cmd/governctl/pr/merge.go index c99af96..3a696a0 100644 --- a/cmd/governctl/pr/merge.go +++ b/cmd/governctl/pr/merge.go @@ -292,7 +292,9 @@ func (opts *Merge) Run(ctx context.Context, args []string) error { } token := string(output) - if !strings.HasPrefix(token, "gh") { + if strings.HasPrefix(token, "no oauth token found") { + token = "" + } else if !strings.HasPrefix(token, "gh") { return fmt.Errorf("could not backup token, invalid format (try running `gh auth token` manually): %w", err) } @@ -303,7 +305,9 @@ func (opts *Merge) Run(ctx context.Context, args []string) error { cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) cmd.Stdin = bytes.NewReader([]byte(kitcfg.G[config.Config](ctx).GithubToken)) if err := cmd.Run(); err != nil { - return fmt.Errorf("could not update token: %w", err) + if token == "" { + return fmt.Errorf("could not update token and no token already exists: %w", err) + } } // Change PR base branch to "-PRID" @@ -324,14 +328,16 @@ func (opts *Merge) Run(ctx context.Context, args []string) error { return fmt.Errorf("could not merge with rebase into %s: %w", tempBranch, err) } - // Replace token with the original one - // Use gh and run: gh auth login --with-token < - cmd = exec.Command("gh", "auth", "login", "--with-token") - cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) - cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) - cmd.Stdin = bytes.NewReader([]byte(token)) - if err := cmd.Run(); err != nil { - return fmt.Errorf("could not update token: %w", err) + if token != "" { + // Replace token with the original one + // Use gh and run: gh auth login --with-token < + cmd = exec.Command("gh", "auth", "login", "--with-token") + cmd.Stderr = log.G(ctx).WriterLevel(logrus.ErrorLevel) + cmd.Stdout = log.G(ctx).WriterLevel(logrus.DebugLevel) + cmd.Stdin = bytes.NewReader([]byte(token)) + if err := cmd.Run(); err != nil { + return fmt.Errorf("could not update token: %w", err) + } } }