From 2b3ed1f3c22b91c0d894cfeee3b56147e646c469 Mon Sep 17 00:00:00 2001 From: Andrew Jeffree Date: Wed, 2 Oct 2019 16:10:10 +1000 Subject: [PATCH] Configure git to use https instead of ssh. Fix formatting --- server/events/git_cred_writer.go | 19 +++++++++++++------ server/events/git_cred_writer_test.go | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/server/events/git_cred_writer.go b/server/events/git_cred_writer.go index c093f92c49..fb7ee215f5 100644 --- a/server/events/git_cred_writer.go +++ b/server/events/git_cred_writer.go @@ -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 @@ -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 } diff --git a/server/events/git_cred_writer_test.go b/server/events/git_cred_writer_test.go index d4c0033597..e2a901493e 100644 --- a/server/events/git_cred_writer_test.go +++ b/server/events/git_cred_writer_test.go @@ -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() @@ -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)) +}