Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure git to use https instead of ssh. #797

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 13 additions & 6 deletions server/events/git_cred_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package events

import (
"fmt"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"
)

// WriteGitCreds generates a .git-credentials file containing the username and token
Expand Down Expand Up @@ -42,10 +43,16 @@ func WriteGitCreds(gitUser string, gitToken string, gitHostname string, home str

logger.Info("wrote git credentials to %s", credsFile)

cmd := exec.Command("git", "config", "--global", "credential.helper", "store")
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "There was an error running %s: %s", strings.Join(cmd.Args, " "), string(out))
credentialCmd := exec.Command("git", "config", "--global", "credential.helper", "store")
if out, err := credentialCmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "There was an error running %s: %s", strings.Join(credentialCmd.Args, " "), string(out))
}
logger.Info("successfully ran %s", strings.Join(credentialCmd.Args, " "))

urlCmd := exec.Command("git", "config", "--global", fmt.Sprintf("url.https://%s/.insteadOf", gitHostname), fmt.Sprintf("git@%s:", gitHostname))
if out, err := urlCmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "There was an error running %s: %s", strings.Join(urlCmd.Args, " "), string(out))
}
logger.Info("successfully ran %s", strings.Join(cmd.Args, " "))
logger.Info("successfully ran %s", strings.Join(urlCmd.Args, " "))
return nil
}
16 changes: 15 additions & 1 deletion server/events/git_cred_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestWriteGitCreds_ErrIfCannotWrite(t *testing.T) {
}

// Test that git is actually configured to use the credentials
func TestWriteGitCreds_ConfigureGit(t *testing.T) {
func TestWriteGitCreds_ConfigureGitCredentialHelper(t *testing.T) {
tmp, cleanup := TempDir(t)
defer cleanup()

Expand All @@ -96,3 +96,17 @@ func TestWriteGitCreds_ConfigureGit(t *testing.T) {
Ok(t, err)
Equals(t, expOutput+"\n", string(actOutput))
}

// Test that git is configured to use https instead of ssh
func TestWriteGitCreds_ConfigureGitUrlOveride(t *testing.T) {
tmp, cleanup := TempDir(t)
defer cleanup()

err := events.WriteGitCreds("user", "token", "hostname", tmp, logger)
Ok(t, err)

expOutput := `git@hostname:`
actOutput, err := exec.Command("git", "config", "--global", "url.https://hostname/.insteadof").Output()
Ok(t, err)
Equals(t, expOutput+"\n", string(actOutput))
}