diff --git a/.cloudbees/testing/action.yml b/.cloudbees/testing/action.yml index c4d9267..7988510 100644 --- a/.cloudbees/testing/action.yml +++ b/.cloudbees/testing/action.yml @@ -94,13 +94,13 @@ outputs: description: "The clone URL of the repository" commit: value: ${{ steps.checkout.outputs.commit }} - description: "Commit ID from source repository" + description: "The commit ID from the source repository" commit-url: value: ${{ steps.checkout.outputs.commit-url }} - description: "Commit URL from source repository" + description: "The commit URL from the source repository" ref: value: ${{ steps.checkout.outputs.ref }} - description: "Ref or branch of the checked-out repository" + description: "The ref or branch of the checked-out repository" runs: using: composite steps: diff --git a/internal/auth/util.go b/internal/auth/util.go index c62f879..f386177 100644 --- a/internal/auth/util.go +++ b/internal/auth/util.go @@ -17,6 +17,7 @@ import ( "strings" "text/template" + "github.com/cloudbees-io/checkout/internal/core" "github.com/cloudbees-io/checkout/internal/git" "github.com/cloudbees-io/checkout/internal/helper" @@ -107,48 +108,86 @@ func ConfigureToken(cli *git.GitCLI, configPath string, globalConfig bool, serve } } - helperCommand, cleaner, err := helper.InstallHelperFor(serverURL, token.options()) + path, err := exec.LookPath("git-credential-cloudbees") if err != nil { - return cleaner, "", err - } + // we are running on an older version of platform, fall-back to the built in helper + core.Debug("Could not find git-credential-cloudbees on the path, falling back to old-style helper") + + helperCommand, cleaner, err := helper.InstallHelperFor(serverURL, token.options()) + if err != nil { + return cleaner, "", err + } - oldHelper, _ := cli.GetConfig(globalConfig, "credential.helper") - oldUseHttpPath, _ := cli.GetConfig(globalConfig, "credential.useHttpPath") + oldHelper, _ := cli.GetConfig(globalConfig, "credential.helper") + oldUseHttpPath, _ := cli.GetConfig(globalConfig, "credential.useHttpPath") - fullCleaner := func() error { - var errs []error - if oldHelper == "" { - if _, err := cli.UnsetConfig(globalConfig, "credential.helper"); err != nil { + fullCleaner := func() error { + var errs []error + if oldHelper == "" { + if _, err := cli.UnsetConfig(globalConfig, "credential.helper"); err != nil { + errs = append(errs, err) + } + } else if err := cli.SetConfigStr(globalConfig, "credential.helper", oldHelper); err != nil { errs = append(errs, err) } - } else if err := cli.SetConfigStr(globalConfig, "credential.helper", oldHelper); err != nil { - errs = append(errs, err) - } - useHttpPath, _ := strconv.ParseBool(oldUseHttpPath) - if oldUseHttpPath == "" { - if _, err := cli.UnsetConfig(globalConfig, "credential.useHttpPath"); err != nil { + useHttpPath, _ := strconv.ParseBool(oldUseHttpPath) + if oldUseHttpPath == "" { + if _, err := cli.UnsetConfig(globalConfig, "credential.useHttpPath"); err != nil { + errs = append(errs, err) + } + } else if err := cli.SetConfigBool(globalConfig, "credential.useHttpPath", useHttpPath); err != nil { + errs = append(errs, err) + } + if err := cleaner(); err != nil { errs = append(errs, err) } - } else if err := cli.SetConfigBool(globalConfig, "credential.useHttpPath", useHttpPath); err != nil { - errs = append(errs, err) + if len(errs) > 0 { + return errors.Join(errs...) + } + return nil } - if err := cleaner(); err != nil { - errs = append(errs, err) + + if err := cli.SetConfigStr(globalConfig, "credential.helper", helperCommand); err != nil { + return fullCleaner, "", err } - if len(errs) > 0 { - return errors.Join(errs...) + if err := cli.SetConfigBool(globalConfig, "credential.useHttpPath", true); err != nil { + return fullCleaner, "", err } - return nil + + return fullCleaner, helperCommand, nil } - if err := cli.SetConfigStr(globalConfig, "credential.helper", helperCommand); err != nil { - return fullCleaner, "", err + core.Debug("Found git-credential-cloudbees on the path at %s", path) + + homeDir, err := os.UserHomeDir() + if err != nil { + return noOpClean, "", err } - if err := cli.SetConfigBool(globalConfig, "credential.useHttpPath", true); err != nil { - return fullCleaner, "", err + + helperConfig := filepath.Join(homeDir, ".git-credential-cloudbees-config") + + const tokenEnv = "CLOUDBEES_API_TOKEN" + + cmd := exec.Command(path, + "init", + "--config", helperConfig, + "--cloudbees-api-token-env-var", tokenEnv, + "--cloudbees-api-url", token.ApiURL, + "--git-config-file-path", configPath) + + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + fmt.Println(cmd.String()) + + cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", tokenEnv, token.ApiToken)) + + err = cmd.Run() + if err != nil { + return noOpClean, "", err } - return fullCleaner, helperCommand, nil + return noOpClean, fmt.Sprintf("%s helper --config %s", shellescape.Quote(path), shellescape.Quote(helperConfig)), nil } func ConfigureSubmoduleTokenAuth(cli *git.GitCLI, recursive bool, serverURL string, token string) error {