Repository: jenkins-x/lighthouse
Pull request #1273 (2021-04-04 14:04:32)

fix: include tags when fetching updates from the server
If I run a pipeline that has a uses: line with an @version suffix, then change the pipeline to have a later @version suffix and re-trigger, I get this error

```
{"Branch":"master","Clone":"https://git.drivenow.com.au/drivenow/octane-staging.git","ID":"2206","Link":"https://git.drivenow.com.au/drivenow/octane-staging","Name":"octane-staging","Namespace":"drivenow","Webhook":"push","error":"failed to create agent: failed to calculate in repo config: failed to load trigger config for repository drivenow/octane-staging for ref ad417c592f9a80ca5c559d41885b3e96f07b3a6b: failed to load file .lighthouse/jenkins-x/triggers.yaml in drivenow/octane-staging with sha ad417c592f9a80ca5c559d41885b3e96f07b3a6b: failed to load source for presubmit pr: failed to unmarshal YAML file .lighthouse/jenkins-x/pullrequest.yaml in repo drivenow/octane-staging with sha ad417c592f9a80ca5c559d41885b3e96f07b3a6b: failed to inherit steps: failed to process uses steps: failed to resolve git URI lighthouse:drivenow/octane/.lighthouse/jenkins-x/site-import-octane.yaml@0.15.4 for step : failed to load URI lighthouse:drivenow/octane/.lighthouse/jenkins-x/site-import-octane.yaml@0.15.4: failed to find file .lighthouse/jenkins-x/site-import-octane.yaml in repo drivenow/octane with sha 0.15.4: failed to switch to ref 0.15.4: failed to fetch repository drivenow/octane: error fetching \"0.15.4\": exit status 128 fatal: couldn't find remote ref 0.15.4\n","head":"ad417c592f9a80ca5c559d41885b3e96f07b3a6b","level":"error","msg":"Error creating agent for PushEvent.","org":"drivenow","ref":"refs/heads/master","repo":"octane-staging","time":"2021-04-04T12:24:42Z"}
```

The only way to resolve it is to delete the pod

```
k --context live -n jx delete pod -l app=lighthouse-webhooks --wait=false
```

then re-trigger the same pipeline with a dummy commit

Even though it says `couldn't find remote ref 0.15.4` the remote ref **is** there since it was just created by a previous pipeline run



Patch:
diff --git a/pkg/git/v2/interactor.go b/pkg/git/v2/interactor.go
index df05ecd5..417a33e3 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


Test patch:
diff --git a/pkg/filebrowser/git_file_browser_test.go b/pkg/filebrowser/git_file_browser_test.go
index 1b1d7112..3194c67d 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_test.go b/pkg/git/v2/interactor_test.go
index b274c4bd..eb798d3c 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"},
 			},