Skip to content

Commit

Permalink
Merge pull request #28 from screwdriver-cd/https
Browse files Browse the repository at this point in the history
Use https as default url for git clone operation
  • Loading branch information
jer authored Aug 5, 2016
2 parents b97417c + a4abfeb commit 6e59496
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
18 changes: 18 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ func Clone(scmURL, destination string) error {
}
return nil
}

//SetConfig sets up git configuration
func SetConfig(setting, name string) error {
cmd := execCommand("git", "config", setting, name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Start()
if err != nil {
return fmt.Errorf("starting git config command: %v", err)
}

err = cmd.Wait()
if err != nil {
return fmt.Errorf("setting git config: %v", err)
}
return nil
}
25 changes: 25 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,32 @@ func TestHelperProcess(*testing.T) {
switch args[1] {
case "clone":
return
case "config":
return
}
}
os.Exit(255)
}

func TestSetConfig(t *testing.T) {
testUserName := "sd-buildbot"
testSetting := "user.name"
execCommand = getFakeExecCommand(func(cmd string, args ...string) {
want := []string{
"config", "user.name", testUserName,
}
if len(args) != len(want) {
t.Errorf("Incorrect args sent to git: %q, want %q", args, want)
}
for i, arg := range args {
if arg != want[i] {
t.Errorf("args[%d] = %q, want %q", i, arg, want[i])
}
}
})

err := SetConfig(testSetting, testUserName)
if err != nil {
t.Errorf("Unexpected error from git config: %v", err)
}
}
22 changes: 19 additions & 3 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ type scmPath struct {
var mkdirAll = os.MkdirAll
var stat = os.Stat
var gitClone = git.Clone
var gitSetConfig = git.SetConfig

func (s scmPath) String() string {
return fmt.Sprintf("%s:%s/%s#%s", s.Host, s.Org, s.Repo, s.Branch)
return fmt.Sprintf("git@%s:%s/%s#%s", s.Host, s.Org, s.Repo, s.Branch)
}

func (s scmPath) httpsString() string {
return fmt.Sprintf("https://%s/%s/%s#%s", s.Host, s.Org, s.Repo, s.Branch)
}

// e.g. "[email protected]:screwdriver-cd/launch.git#master"
func parseScmURL(url string) (scmPath, error) {
r := regexp.MustCompile("(.*):(.*)/(.*)#(.*)")
r := regexp.MustCompile("git@(.*):(.*)/(.*)#(.*)")
matched := r.FindAllStringSubmatch(url, -1)
if matched == nil || len(matched) != 1 || len(matched[0]) != 5 {
return scmPath{}, fmt.Errorf("Unable to parse SCM URL %v, match: %q", url, matched)
Expand Down Expand Up @@ -114,10 +119,21 @@ func launch(api screwdriver.API, buildID string, rootDir string) error {
return err
}

err = gitClone(p.ScmURL, w.Src)
err = gitClone(scm.httpsString(), w.Src)
if err != nil {
return err
}

err = gitSetConfig("user.name", "sd-buildbot")
if err != nil {
return err
}

err = gitSetConfig("user.email", "[email protected]")
if err != nil {
return err
}

return nil
}

Expand Down
35 changes: 31 additions & 4 deletions launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (f MockAPI) PipelineFromID(pipelineID string) (screwdriver.Pipeline, error)
func TestMain(m *testing.M) {
mkdirAll = func(path string, perm os.FileMode) (err error) { return nil }
stat = func(path string) (info os.FileInfo, err error) { return nil, os.ErrExist }
gitSetConfig = func(setting, name string) error { return nil }
os.Exit(m.Run())
}

Expand Down Expand Up @@ -142,7 +143,7 @@ func TestPipelineFromIdError(t *testing.T) {
}

func TestParseScmURL(t *testing.T) {
wantHost := "git@github.com"
wantHost := "github.com"
wantOrg := "screwdriver-cd"
wantRepo := "launcher.git"
wantBranch := "master"
Expand Down Expand Up @@ -219,17 +220,43 @@ func TestCreateWorkspace(t *testing.T) {
func TestClone(t *testing.T) {
testBuildID := "BUILDID"
testJobID := "JOBID"
testSCMURL := "[email protected]:screwdriver-cd/launcher#master"
testSCMURL := "[email protected]:screwdriver-cd/launcher.git#master"
testHttps := "https://github.com/screwdriver-cd/launcher.git#master"
testRoot := "/sd/workspace"
api := mockAPI(t, testBuildID, testJobID, "")
api.pipelineFromID = func(pipelineID string) (screwdriver.Pipeline, error) {
return screwdriver.Pipeline(FakePipeline{ScmURL: testSCMURL}), nil
}
gitClone = func(repo, dest string) error {
if repo != testSCMURL {
t.Errorf("Git clone was called with repo %q, want %q", repo, testSCMURL)
if repo != testHttps {
t.Errorf("Git clone was called with repo %q, want %q", repo, testHttps)
}
return nil
}
launch(screwdriver.API(api), testBuildID, testRoot)
}

func TestHttpsString(t *testing.T) {
testScmPath := scmPath{
Host: "github.com",
Org: "screwdriver-cd",
Repo: "launcher.git",
Branch: "master",
}

wantHttpsString := "https://github.com/screwdriver-cd/launcher.git#master"

httpsString := testScmPath.httpsString()

if httpsString != wantHttpsString {
t.Errorf("httpsString == %q, want %q", httpsString, wantHttpsString)
}
}

func TestSetUserNameAndEmail(t *testing.T) {
testBuildID := "BUILDID"
testRoot := "/sd/workspace"
api := mockAPI(t, testBuildID, "", "")

launch(screwdriver.API(api), testBuildID, testRoot)
}

0 comments on commit 6e59496

Please sign in to comment.