diff --git a/pkg/filebrowser/git_file_browser_test.go b/pkg/filebrowser/git_file_browser_test.go index 1b1d7112a..3194c67d2 100644 --- a/pkg/filebrowser/git_file_browser_test.go +++ b/pkg/filebrowser/git_file_browser_test.go @@ -2,10 +2,14 @@ package filebrowser import ( "fmt" + "io/ioutil" + "os" + "path/filepath" "testing" "github.com/jenkins-x/go-scm/scm" "github.com/jenkins-x/lighthouse/pkg/git/v2" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -93,3 +97,69 @@ func assertNoScmFileExists(t *testing.T, files []*scm.FileEntry, name, message s } t.Logf("correctly does not include file name %s for %s", name, message) } + +func TestGitFileBrowser_Clone_CreateTag_FetchRef(t *testing.T) { + logger := logrus.WithField("client", "git") + + baseDir, err := ioutil.TempDir("", "localdir") + require.NoError(t, err, "failed to find git binary") + fmt.Println(baseDir) + + defer os.RemoveAll(baseDir) + + repoDir := filepath.Join(baseDir, "org", "repo") + + err = os.MkdirAll(repoDir, 0700) + require.NoError(t, err, "failed to make repo dir") + + userGetter := func() (name, email string, err error) { + return "bot", "bot@example.com", nil + } + censor := func(content []byte) []byte { return content } + + executor, err := git.NewCensoringExecutor(repoDir, censor, logger) + require.NoError(t, err, "failed to find git binary") + + err = ioutil.WriteFile(filepath.Join(repoDir, "README.md"), []byte("README"), 0600) + require.NoError(t, err, "failed to write README.md file") + + _, err = executor.Run("init", ".") + require.NoError(t, err, "failed to init git repo") + + _, err = executor.Run("add", "README.md") + require.NoError(t, err, "failed to add README.md status") + + _, err = executor.Run("commit", "-m", "add README.md") + require.NoError(t, err, "failed to add README.md status") + + cf, err := git.NewLocalClientFactory(baseDir, userGetter, censor) + require.NoError(t, err, "failed to create git client factory") + fb := NewFileBrowserFromGitClient(cf) + + files, err := fb.ListFiles("org", "repo", "", "master") + require.NoError(t, err, "failed to list files") + + require.True(t, len(files) == 1, "exepecting 1 file") + require.Equal(t, files[0].Name, "README.md") + + _, err = executor.Run("checkout", "-b", "update-1") + require.NoError(t, err, "failed to create new branch") + + err = ioutil.WriteFile(filepath.Join(repoDir, "README.md"), []byte("README-update-1"), 0600) + require.NoError(t, err, "failed write updated README.md") + + _, err = executor.Run("commit", "-a", "-m", "update README.md") + require.NoError(t, err, "failed to update README.md status") + + _, err = executor.Run("tag", "v0.0.1") + require.NoError(t, err, "failed to create v0.0.1 tag") + + files, err = fb.ListFiles("org", "repo", "", "v0.0.1") + require.NoError(t, err, "failed to lst files in v0.0.1 tag") + require.True(t, len(files) == 1, "exepecting 1 file") + require.Equal(t, files[0].Name, "README.md") + + data, err := fb.GetFile("org", "repo", "README.md", "v0.0.1") + require.NoError(t, err, "failed to lst files in v0.0.1 tag") + require.Equal(t, string(data), "README-update-1") +} diff --git a/pkg/git/v2/interactor.go b/pkg/git/v2/interactor.go index df05ecd5b..417a33e35 100644 --- a/pkg/git/v2/interactor.go +++ b/pkg/git/v2/interactor.go @@ -294,7 +294,7 @@ func (i *interactor) FetchRef(refspec string) error { return fmt.Errorf("could not resolve remote for fetching: %v", err) } i.logger.Debugf("Fetching %q from %s", refspec, remote) - if out, err := i.executor.Run("fetch", remote, refspec); err != nil { + if out, err := i.executor.Run("fetch", "--tags", remote, refspec); err != nil { return fmt.Errorf("error fetching %q: %v %v", refspec, err, string(out)) } return nil diff --git a/pkg/git/v2/interactor_test.go b/pkg/git/v2/interactor_test.go index b274c4bd4..eb798d3cd 100644 --- a/pkg/git/v2/interactor_test.go +++ b/pkg/git/v2/interactor_test.go @@ -1174,12 +1174,12 @@ func TestInteractor_FetchRef(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com shasum": { + "fetch --tags someone.com shasum": { out: []byte(`ok`), }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "shasum"}, + {"fetch", "--tags", "someone.com", "shasum"}, }, expectedErr: false, }, @@ -1200,12 +1200,12 @@ func TestInteractor_FetchRef(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com shasum": { + "fetch --tags someone.com shasum": { err: errors.New("oops"), }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "shasum"}, + {"fetch", "--tags", "someone.com", "shasum"}, }, expectedErr: true, }, @@ -1340,7 +1340,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com pull/1/head": { + "fetch --tags someone.com pull/1/head": { out: []byte(`ok`), }, "checkout FETCH_HEAD": { @@ -1351,7 +1351,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "pull/1/head"}, + {"fetch", "--tags", "someone.com", "pull/1/head"}, {"checkout", "FETCH_HEAD"}, {"checkout", "-b", "pull1"}, }, @@ -1374,12 +1374,12 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com pull/1/head": { + "fetch --tags someone.com pull/1/head": { err: errors.New("oops"), }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "pull/1/head"}, + {"fetch", "--tags", "someone.com", "pull/1/head"}, }, expectedErr: true, }, @@ -1390,7 +1390,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com pull/1/head": { + "fetch --tags someone.com pull/1/head": { out: []byte(`ok`), }, "checkout FETCH_HEAD": { @@ -1398,7 +1398,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "pull/1/head"}, + {"fetch", "--tags", "someone.com", "pull/1/head"}, {"checkout", "FETCH_HEAD"}, }, expectedErr: true, @@ -1410,7 +1410,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { return "someone.com", nil }, responses: map[string]execResponse{ - "fetch someone.com pull/1/head": { + "fetch --tags someone.com pull/1/head": { out: []byte(`ok`), }, "checkout FETCH_HEAD": { @@ -1421,7 +1421,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) { }, }, expectedCalls: [][]string{ - {"fetch", "someone.com", "pull/1/head"}, + {"fetch", "--tags", "someone.com", "pull/1/head"}, {"checkout", "FETCH_HEAD"}, {"checkout", "-b", "pull1"}, },