Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .cloudbees/testing/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
93 changes: 66 additions & 27 deletions internal/auth/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down