From 9a064db67517e7b0989c955cdf3cacc38fe6b5b5 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Wed, 13 Sep 2023 16:09:00 +0100 Subject: [PATCH 01/10] feat: get current preview --- pkg/cmd/get/get.go | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index 00a0882..b374de6 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "os" + "strconv" + "github.com/jenkins-x-plugins/jx-preview/pkg/apis/preview/v1alpha1" "github.com/jenkins-x-plugins/jx-preview/pkg/client/clientset/versioned" "github.com/jenkins-x-plugins/jx-preview/pkg/previews" "github.com/jenkins-x-plugins/jx-preview/pkg/rootcmd" @@ -104,30 +106,38 @@ func (o *Options) Validate() error { } func (o *Options) CurrentPreviewURL() error { - /* TODO - pipeline := o.GetJenkinsJobName() - if pipeline == "" { - return fmt.Errorf("No $JOB_NAME defined for the current pipeline job to use") - } - name := naming.ToValidName(pipeline) - - client, ns, err := o.JXClientAndDevNamespace() + ctx := context.Background() + ns := o.Namespace + resourceList, err := o.PreviewClient.PreviewV1alpha1().Previews(ns).List(ctx, metav1.ListOptions{}) if err != nil { - return err + if !apierrors.IsNotFound(err) { + return errors.Wrapf(err, "failed to list previews in namespace %s", ns) + } } - envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{}) + repoName := os.Getenv("REPO_NAME") + prNumber, err := strconv.Atoi(os.Getenv("PULL_NUMBER")) if err != nil { - return err + return errors.Wrapf(err, "failed to retrieve current preview in namespace %s", ns) } - for _, env := range envList.Items { - if env.Spec.Kind == v1.EnvironmentKindTypePreview && env.Name == name { - // lets log directly to stdout for easy capture of the URL from shell scripts - fmt.Println(env.Spec.PreviewGitSpec.ApplicationURL) - return nil + + var currentPreview v1alpha1.Preview + for _, preview := range resourceList.Items { + if preview.Spec.PullRequest.Number == prNumber && + preview.Spec.PullRequest.Repository == repoName { + currentPreview = preview } } - return fmt.Errorf("No Preview for name: %s", name) - */ + if ¤tPreview == nil { + return fmt.Errorf("no current preview for %s on pull request #%s", repoName, prNumber) + } + + t := table.CreateTable(os.Stdout) + t.AddRow("PULL REQUEST", "NAMESPACE", "APPLICATION") + t.AddRow(currentPreview.Spec.PullRequest.URL, + currentPreview.Spec.Resources.Namespace, + currentPreview.Spec.Resources.URL) + + t.Render() return nil } From fd0c6f45e330a6001a200e4db822648b030d9304 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Wed, 13 Sep 2023 17:16:51 +0100 Subject: [PATCH 02/10] refactor: use scmhelpers instead of manual env read --- pkg/cmd/get/get.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index b374de6..5686c88 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strconv" "github.com/jenkins-x-plugins/jx-preview/pkg/apis/preview/v1alpha1" "github.com/jenkins-x-plugins/jx-preview/pkg/client/clientset/versioned" @@ -12,6 +11,7 @@ import ( "github.com/jenkins-x-plugins/jx-preview/pkg/rootcmd" "github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper" "github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates" + "github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers" "github.com/jenkins-x/jx-helpers/v3/pkg/table" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -21,6 +21,8 @@ import ( // Options containers the CLI options type Options struct { + scmhelpers.PullRequestOptions + PreviewClient versioned.Interface Namespace string @@ -68,6 +70,11 @@ func (o *Options) Run() error { return errors.Wrapf(err, "failed to validate options") } + _, err = o.DiscoverPullRequest() + if err != nil { + return errors.Wrapf(err, "failed to read pull request options") + } + if o.Current { return o.CurrentPreviewURL() } @@ -102,6 +109,12 @@ func (o *Options) Validate() error { if err != nil { return errors.Wrapf(err, "failed to create Preview client") } + + err = o.PullRequestOptions.Validate() + if err != nil { + return errors.Wrapf(err, "failed to create Preview client") + } + return nil } @@ -115,21 +128,16 @@ func (o *Options) CurrentPreviewURL() error { } } - repoName := os.Getenv("REPO_NAME") - prNumber, err := strconv.Atoi(os.Getenv("PULL_NUMBER")) - if err != nil { - return errors.Wrapf(err, "failed to retrieve current preview in namespace %s", ns) - } - var currentPreview v1alpha1.Preview for _, preview := range resourceList.Items { - if preview.Spec.PullRequest.Number == prNumber && - preview.Spec.PullRequest.Repository == repoName { + if preview.Spec.PullRequest.Number == o.Number && + preview.Spec.PullRequest.Repository == o.Repository { currentPreview = preview + break } } - if ¤tPreview == nil { - return fmt.Errorf("no current preview for %s on pull request #%s", repoName, prNumber) + if currentPreview.Spec.PullRequest.Repository != o.Repository { + return fmt.Errorf("no current preview for %s on pull request #%v", o.Repository, o.Number) } t := table.CreateTable(os.Stdout) From 3205742004e63d7871568bdb416ba32e7ad8da51 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Wed, 13 Sep 2023 17:31:40 +0100 Subject: [PATCH 03/10] feat: get preview url writes to variables.sh --- pkg/cmd/create/create.go | 46 ++--------------------------------- pkg/cmd/get/get.go | 17 +++++++++++++ pkg/common/helpers.go | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 44 deletions(-) diff --git a/pkg/cmd/create/create.go b/pkg/cmd/create/create.go index 140f054..a8fb00f 100644 --- a/pkg/cmd/create/create.go +++ b/pkg/cmd/create/create.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "fmt" + "github.com/jenkins-x-plugins/jx-preview/pkg/common" "os" "os/exec" "path/filepath" @@ -228,7 +229,7 @@ func (o *Options) Run() error { o.updatePipelineActivity(url, preview.Spec.PullRequest.URL) - err = o.writeOutputEnvVars() + err = common.WriteOutputEnvVars(o.Dir, o.OutputEnvVars) if err != nil { return errors.Wrapf(err, "failed to write output environment variables") } @@ -704,49 +705,6 @@ func (o *Options) DiscoverPreviewHelmfile() error { return nil } -func (o *Options) writeOutputEnvVars() error { - path := filepath.Join(o.Dir, ".jx", "variables.sh") - - text := "" - exists, err := files.FileExists(path) - if err != nil { - return errors.Wrapf(err, "failed to check for file exist %s", path) - } - if exists { - data, err := os.ReadFile(path) - if err != nil { - return errors.Wrapf(err, "failed to read %s", path) - } - text = string(data) - } - - buf := strings.Builder{} - buf.WriteString("# preview environment variables\n") - for k, v := range o.OutputEnvVars { - buf.WriteString(fmt.Sprintf("export %s=%q\n", k, v)) - } - if text != "" { - buf.WriteString("\n\n") - buf.WriteString(text) - } - text = buf.String() - - // make sure dir exists - dir := filepath.Dir(path) - err = os.MkdirAll(dir, files.DefaultDirWritePermissions) - if err != nil { - return errors.Wrapf(err, "failed to make dir %s", dir) - } - - err = os.WriteFile(path, []byte(text), files.DefaultFileWritePermissions) - if err != nil { - return errors.Wrapf(err, "failed to save file %s", path) - } - - log.Logger().Infof("wrote preview environment variables to %s", info(path)) - return nil -} - func (o *Options) watchNamespaceStart() error { previewNamespace := o.Preview.Spec.Resources.Namespace cmd := exec.Command("kubectl", "get", "event", "-w", "-n", previewNamespace) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index 5686c88..bddcc02 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -7,6 +7,7 @@ import ( "github.com/jenkins-x-plugins/jx-preview/pkg/apis/preview/v1alpha1" "github.com/jenkins-x-plugins/jx-preview/pkg/client/clientset/versioned" + "github.com/jenkins-x-plugins/jx-preview/pkg/common" "github.com/jenkins-x-plugins/jx-preview/pkg/previews" "github.com/jenkins-x-plugins/jx-preview/pkg/rootcmd" "github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper" @@ -25,6 +26,7 @@ type Options struct { PreviewClient versioned.Interface Namespace string + OutputEnvVars map[string]string Current bool } @@ -110,6 +112,10 @@ func (o *Options) Validate() error { return errors.Wrapf(err, "failed to create Preview client") } + if o.OutputEnvVars == nil { + o.OutputEnvVars = map[string]string{} + } + err = o.PullRequestOptions.Validate() if err != nil { return errors.Wrapf(err, "failed to create Preview client") @@ -147,5 +153,16 @@ func (o *Options) CurrentPreviewURL() error { currentPreview.Spec.Resources.URL) t.Render() + + o.OutputEnvVars["PREVIEW_URL"] = currentPreview.Spec.Resources.URL + o.OutputEnvVars["PREVIEW_NAME"] = currentPreview.Name + o.OutputEnvVars["PREVIEW_NAMESPACE"] = currentPreview.Spec.Resources.Namespace + o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"] = currentPreview.Spec.PullRequest.URL + + err = common.WriteOutputEnvVars(o.Dir, o.OutputEnvVars) + if err != nil { + return errors.Wrapf(err, "failed to write output environment variables") + } + return nil } diff --git a/pkg/common/helpers.go b/pkg/common/helpers.go index 722a116..3fc84a9 100644 --- a/pkg/common/helpers.go +++ b/pkg/common/helpers.go @@ -1,7 +1,14 @@ package common import ( + "fmt" + "github.com/jenkins-x/jx-helpers/v3/pkg/files" + "github.com/jenkins-x/jx-helpers/v3/pkg/termcolor" + "github.com/jenkins-x/jx-logging/v3/pkg/log" + "github.com/pkg/errors" "os" + "path/filepath" + "strings" ) // BinaryName the binary name to use in help docs @@ -20,3 +27,48 @@ func init() { TopLevelCommand = "jx remote" } } + +var info = termcolor.ColorInfo + +func WriteOutputEnvVars(currentDir string, outputEnvVars map[string]string) error { + path := filepath.Join(currentDir, ".jx", "variables.sh") + + text := "" + exists, err := files.FileExists(path) + if err != nil { + return errors.Wrapf(err, "failed to check for file exist %s", path) + } + if exists { + data, err := os.ReadFile(path) + if err != nil { + return errors.Wrapf(err, "failed to read %s", path) + } + text = string(data) + } + + buf := strings.Builder{} + buf.WriteString("# preview environment variables\n") + for k, v := range outputEnvVars { + buf.WriteString(fmt.Sprintf("export %s=%q\n", k, v)) + } + if text != "" { + buf.WriteString("\n\n") + buf.WriteString(text) + } + text = buf.String() + + // make sure dir exists + dir := filepath.Dir(path) + err = os.MkdirAll(dir, files.DefaultDirWritePermissions) + if err != nil { + return errors.Wrapf(err, "failed to make dir %s", dir) + } + + err = os.WriteFile(path, []byte(text), files.DefaultFileWritePermissions) + if err != nil { + return errors.Wrapf(err, "failed to save file %s", path) + } + + log.Logger().Infof("wrote preview environment variables to %s", info(path)) + return nil +} From 5c7e3b3ffeac8f4c28e26de803e8f5b3a054c042 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 09:18:27 +0100 Subject: [PATCH 04/10] fix: get preview validation fixes --- pkg/cmd/get/get.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index bddcc02..2648be3 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -115,10 +115,11 @@ func (o *Options) Validate() error { if o.OutputEnvVars == nil { o.OutputEnvVars = map[string]string{} } + o.DiscoverFromGit = true err = o.PullRequestOptions.Validate() if err != nil { - return errors.Wrapf(err, "failed to create Preview client") + return errors.Wrapf(err, "failed to validate pull request options") } return nil From ef59b0b41c5ff505f78db6d99988cf4733b5eb72 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 13:53:26 +0100 Subject: [PATCH 05/10] feat(test): get current preview tests --- pkg/cmd/get/get_test.go | 46 ++++++++++++++++++++++++++++ pkg/previews/fakepreviews/helpers.go | 3 ++ 2 files changed, 49 insertions(+) create mode 100644 pkg/cmd/get/get_test.go diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go new file mode 100644 index 0000000..cdfac1b --- /dev/null +++ b/pkg/cmd/get/get_test.go @@ -0,0 +1,46 @@ +package get + +import ( + "fmt" + "github.com/jenkins-x-plugins/jx-preview/pkg/client/clientset/versioned/fake" + "github.com/jenkins-x-plugins/jx-preview/pkg/fakescms" + "github.com/jenkins-x-plugins/jx-preview/pkg/previews/fakepreviews" + fakescm "github.com/jenkins-x/go-scm/scm/driver/fake" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" +) + +func TestPreviewGetCurrent(t *testing.T) { + scmClient, fakeData := fakescm.NewDefault() + + owner := "owner" + repo := "test-repo" + prNumber := 1 + sourceUrl := fmt.Sprintf("https://fake.com/%s/%s", owner, repo) + ns := "jx" + + preview1, _ := fakepreviews.CreateTestPreviewAndPullRequest(fakeData, ns, owner, repo, 1) + fakescms.CreatePullRequest(fakeData, owner, repo, prNumber) + + previewClient := fake.NewSimpleClientset(preview1) + + _, o := NewCmdGetPreview() + + o.ScmClient = scmClient + o.PreviewClient = previewClient + o.SourceURL = sourceUrl + o.PullRequestOptions.Number = prNumber + o.Repository = repo + o.DiscoverFromGit = false + o.Namespace = ns + o.Current = true + + err := o.Run() + require.NoError(t, err) + + assert.Equal(t, fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber), o.OutputEnvVars["PREVIEW_URL"]) + assert.Equal(t, fmt.Sprintf("%s-%s-%v", owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAME"]) + assert.Equal(t, fmt.Sprintf("%s-%s-%s-pr-%v", ns, owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAMESPACE"]) + assert.Equal(t, sourceUrl, o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"]) +} diff --git a/pkg/previews/fakepreviews/helpers.go b/pkg/previews/fakepreviews/helpers.go index e36d29b..bb4f83b 100644 --- a/pkg/previews/fakepreviews/helpers.go +++ b/pkg/previews/fakepreviews/helpers.go @@ -1,6 +1,7 @@ package fakepreviews import ( + "fmt" "strconv" "github.com/jenkins-x-plugins/jx-preview/pkg/apis/preview/v1alpha1" @@ -20,6 +21,7 @@ func CreateTestPreviewAndPullRequest(data *fakescm.Data, ns, owner, repo string, prLink := repoLink + "/pull/" + prNumberText sha := "abcdef1234" previewNamespace := ns + "-" + owner + "-" + repo + "-pr-" + prNumberText + previewUrl := fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber) pr := &scm.PullRequest{ Number: prNumber, Title: prTitle, @@ -64,6 +66,7 @@ func CreateTestPreviewAndPullRequest(data *fakescm.Data, ns, owner, repo string, }, Resources: v1alpha1.Resources{ Namespace: previewNamespace, + URL: previewUrl, }, }, } From 491e21d747828fe71bbde6d487b15423994cd6e7 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 13:58:59 +0100 Subject: [PATCH 06/10] fix(test): add jx client mock to get current preview test --- pkg/cmd/get/get_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go index cdfac1b..ff0ff0f 100644 --- a/pkg/cmd/get/get_test.go +++ b/pkg/cmd/get/get_test.go @@ -6,6 +6,8 @@ import ( "github.com/jenkins-x-plugins/jx-preview/pkg/fakescms" "github.com/jenkins-x-plugins/jx-preview/pkg/previews/fakepreviews" fakescm "github.com/jenkins-x/go-scm/scm/driver/fake" + jxfake "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/fake" + "github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -25,10 +27,17 @@ func TestPreviewGetCurrent(t *testing.T) { previewClient := fake.NewSimpleClientset(preview1) + devEnv := jxenv.CreateDefaultDevEnvironment(ns) + devEnv.Namespace = ns + devEnv.Spec.Source.URL = sourceUrl + + jxClient := jxfake.NewSimpleClientset(devEnv) + _, o := NewCmdGetPreview() o.ScmClient = scmClient o.PreviewClient = previewClient + o.JXClient = jxClient o.SourceURL = sourceUrl o.PullRequestOptions.Number = prNumber o.Repository = repo From 41e44b3c7f6363b4e26be1c8ea551bf5d9d15e29 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 14:16:59 +0100 Subject: [PATCH 07/10] feat(test): add regular get test & refactor get current --- pkg/cmd/get/get.go | 37 ++++++++++++++++---------- pkg/cmd/get/get_test.go | 59 +++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index 2648be3..280ca22 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -72,11 +72,6 @@ func (o *Options) Run() error { return errors.Wrapf(err, "failed to validate options") } - _, err = o.DiscoverPullRequest() - if err != nil { - return errors.Wrapf(err, "failed to read pull request options") - } - if o.Current { return o.CurrentPreviewURL() } @@ -112,20 +107,20 @@ func (o *Options) Validate() error { return errors.Wrapf(err, "failed to create Preview client") } - if o.OutputEnvVars == nil { - o.OutputEnvVars = map[string]string{} - } - o.DiscoverFromGit = true + return nil +} - err = o.PullRequestOptions.Validate() +func (o *Options) CurrentPreviewURL() error { + err := o.ValidateCurrent() if err != nil { - return errors.Wrapf(err, "failed to validate pull request options") + return errors.Wrapf(err, "failed to validate current options") } - return nil -} + _, err = o.DiscoverPullRequest() + if err != nil { + return errors.Wrapf(err, "failed to read pull request options") + } -func (o *Options) CurrentPreviewURL() error { ctx := context.Background() ns := o.Namespace resourceList, err := o.PreviewClient.PreviewV1alpha1().Previews(ns).List(ctx, metav1.ListOptions{}) @@ -167,3 +162,17 @@ func (o *Options) CurrentPreviewURL() error { return nil } + +func (o *Options) ValidateCurrent() error { + if o.OutputEnvVars == nil { + o.OutputEnvVars = map[string]string{} + } + o.DiscoverFromGit = true + + err := o.PullRequestOptions.Validate() + if err != nil { + return errors.Wrapf(err, "failed to validate pull request options") + } + + return nil +} diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go index ff0ff0f..87b3749 100644 --- a/pkg/cmd/get/get_test.go +++ b/pkg/cmd/get/get_test.go @@ -13,7 +13,7 @@ import ( "testing" ) -func TestPreviewGetCurrent(t *testing.T) { +func TestPreviewGet(t *testing.T) { scmClient, fakeData := fakescm.NewDefault() owner := "owner" @@ -33,23 +33,42 @@ func TestPreviewGetCurrent(t *testing.T) { jxClient := jxfake.NewSimpleClientset(devEnv) - _, o := NewCmdGetPreview() - - o.ScmClient = scmClient - o.PreviewClient = previewClient - o.JXClient = jxClient - o.SourceURL = sourceUrl - o.PullRequestOptions.Number = prNumber - o.Repository = repo - o.DiscoverFromGit = false - o.Namespace = ns - o.Current = true - - err := o.Run() - require.NoError(t, err) - - assert.Equal(t, fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber), o.OutputEnvVars["PREVIEW_URL"]) - assert.Equal(t, fmt.Sprintf("%s-%s-%v", owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAME"]) - assert.Equal(t, fmt.Sprintf("%s-%s-%s-pr-%v", ns, owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAMESPACE"]) - assert.Equal(t, sourceUrl, o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"]) + testCases := []struct { + name string + current bool + }{ + { + name: "get", + current: false, + }, + { + name: "get current", + current: true, + }, + } + + for _, tc := range testCases { + _, o := NewCmdGetPreview() + + o.ScmClient = scmClient + o.PreviewClient = previewClient + o.JXClient = jxClient + o.SourceURL = sourceUrl + o.PullRequestOptions.Number = prNumber + o.Repository = repo + o.DiscoverFromGit = false + o.Namespace = ns + o.Current = tc.current + + t.Logf("running get for test: %s", tc.name) + err := o.Run() + require.NoError(t, err) + + if tc.current { + assert.Equal(t, fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber), o.OutputEnvVars["PREVIEW_URL"]) + assert.Equal(t, fmt.Sprintf("%s-%s-%v", owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAME"]) + assert.Equal(t, fmt.Sprintf("%s-%s-%s-pr-%v", ns, owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAMESPACE"]) + assert.Equal(t, sourceUrl, o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"]) + } + } } From b51cb17b331348f280a3c298decdcdddd5839a0f Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 14:45:41 +0100 Subject: [PATCH 08/10] fix(test): add temp dir to get tests --- pkg/cmd/get/get_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go index 87b3749..d5f9b6e 100644 --- a/pkg/cmd/get/get_test.go +++ b/pkg/cmd/get/get_test.go @@ -10,6 +10,7 @@ import ( "github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "os" "testing" ) @@ -21,6 +22,8 @@ func TestPreviewGet(t *testing.T) { prNumber := 1 sourceUrl := fmt.Sprintf("https://fake.com/%s/%s", owner, repo) ns := "jx" + tmpDir, err := os.MkdirTemp("", "") + require.NoError(t, err, "failed to create temp dir") preview1, _ := fakepreviews.CreateTestPreviewAndPullRequest(fakeData, ns, owner, repo, 1) fakescms.CreatePullRequest(fakeData, owner, repo, prNumber) @@ -59,6 +62,7 @@ func TestPreviewGet(t *testing.T) { o.DiscoverFromGit = false o.Namespace = ns o.Current = tc.current + o.Dir = tmpDir t.Logf("running get for test: %s", tc.name) err := o.Run() From a7df10d29139c6ea6fcec53dc52c171b2de7a7a4 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 15:31:10 +0100 Subject: [PATCH 09/10] chore: lint --- cmd/docs/main.go | 2 +- cmd/docs/md_docs.go | 2 +- pkg/client/clientset/versioned/fake/register.go | 14 +++++++------- pkg/client/clientset/versioned/scheme/register.go | 14 +++++++------- pkg/cmd/create/create.go | 3 ++- pkg/cmd/get/get_test.go | 5 +++-- pkg/common/helpers.go | 7 ++++--- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cmd/docs/main.go b/cmd/docs/main.go index ef6dc05..2b08a5b 100644 --- a/cmd/docs/main.go +++ b/cmd/docs/main.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, diff --git a/cmd/docs/md_docs.go b/cmd/docs/md_docs.go index ec94bf3..1b09002 100644 --- a/cmd/docs/md_docs.go +++ b/cmd/docs/md_docs.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, diff --git a/pkg/client/clientset/versioned/fake/register.go b/pkg/client/clientset/versioned/fake/register.go index 5e66490..6a8734a 100644 --- a/pkg/client/clientset/versioned/fake/register.go +++ b/pkg/client/clientset/versioned/fake/register.go @@ -36,14 +36,14 @@ var localSchemeBuilder = runtime.SchemeBuilder{ // AddToScheme adds all types of this clientset into the given scheme. This allows composition // of clientsets, like in: // -// import ( -// "k8s.io/client-go/kubernetes" -// clientsetscheme "k8s.io/client-go/kubernetes/scheme" -// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme" -// ) +// import ( +// "k8s.io/client-go/kubernetes" +// clientsetscheme "k8s.io/client-go/kubernetes/scheme" +// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme" +// ) // -// kclientset, _ := kubernetes.NewForConfig(c) -// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) +// kclientset, _ := kubernetes.NewForConfig(c) +// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) // // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types // correctly. diff --git a/pkg/client/clientset/versioned/scheme/register.go b/pkg/client/clientset/versioned/scheme/register.go index 27d639f..234689b 100644 --- a/pkg/client/clientset/versioned/scheme/register.go +++ b/pkg/client/clientset/versioned/scheme/register.go @@ -36,14 +36,14 @@ var localSchemeBuilder = runtime.SchemeBuilder{ // AddToScheme adds all types of this clientset into the given scheme. This allows composition // of clientsets, like in: // -// import ( -// "k8s.io/client-go/kubernetes" -// clientsetscheme "k8s.io/client-go/kubernetes/scheme" -// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme" -// ) +// import ( +// "k8s.io/client-go/kubernetes" +// clientsetscheme "k8s.io/client-go/kubernetes/scheme" +// aggregatorclientsetscheme "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/scheme" +// ) // -// kclientset, _ := kubernetes.NewForConfig(c) -// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) +// kclientset, _ := kubernetes.NewForConfig(c) +// _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) // // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types // correctly. diff --git a/pkg/cmd/create/create.go b/pkg/cmd/create/create.go index a8fb00f..f7bf5ab 100644 --- a/pkg/cmd/create/create.go +++ b/pkg/cmd/create/create.go @@ -4,7 +4,6 @@ import ( "bufio" "context" "fmt" - "github.com/jenkins-x-plugins/jx-preview/pkg/common" "os" "os/exec" "path/filepath" @@ -12,6 +11,8 @@ import ( "strings" "time" + "github.com/jenkins-x-plugins/jx-preview/pkg/common" + "github.com/jenkins-x/jx-helpers/v3/pkg/kube/pods" corev1 "k8s.io/api/core/v1" diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go index d5f9b6e..e246b17 100644 --- a/pkg/cmd/get/get_test.go +++ b/pkg/cmd/get/get_test.go @@ -2,6 +2,9 @@ package get import ( "fmt" + "os" + "testing" + "github.com/jenkins-x-plugins/jx-preview/pkg/client/clientset/versioned/fake" "github.com/jenkins-x-plugins/jx-preview/pkg/fakescms" "github.com/jenkins-x-plugins/jx-preview/pkg/previews/fakepreviews" @@ -10,8 +13,6 @@ import ( "github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxenv" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "os" - "testing" ) func TestPreviewGet(t *testing.T) { diff --git a/pkg/common/helpers.go b/pkg/common/helpers.go index 3fc84a9..dc5b3fa 100644 --- a/pkg/common/helpers.go +++ b/pkg/common/helpers.go @@ -2,13 +2,14 @@ package common import ( "fmt" + "os" + "path/filepath" + "strings" + "github.com/jenkins-x/jx-helpers/v3/pkg/files" "github.com/jenkins-x/jx-helpers/v3/pkg/termcolor" "github.com/jenkins-x/jx-logging/v3/pkg/log" "github.com/pkg/errors" - "os" - "path/filepath" - "strings" ) // BinaryName the binary name to use in help docs From 79f796f25cc129c99a92d044ecd0c6ddb1182477 Mon Sep 17 00:00:00 2001 From: ZackJagger Date: Thu, 14 Sep 2023 16:41:48 +0100 Subject: [PATCH 10/10] chore: lint --- pkg/cmd/get/get.go | 10 ++++++---- pkg/cmd/get/get_test.go | 8 ++++---- pkg/previews/fakepreviews/helpers.go | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/get/get.go b/pkg/cmd/get/get.go index 280ca22..1e8bad1 100644 --- a/pkg/cmd/get/get.go +++ b/pkg/cmd/get/get.go @@ -131,13 +131,15 @@ func (o *Options) CurrentPreviewURL() error { } var currentPreview v1alpha1.Preview - for _, preview := range resourceList.Items { - if preview.Spec.PullRequest.Number == o.Number && - preview.Spec.PullRequest.Repository == o.Repository { - currentPreview = preview + + for i := 0; i < len(resourceList.Items); i++ { + if resourceList.Items[i].Spec.PullRequest.Number == o.Number && + resourceList.Items[i].Spec.PullRequest.Repository == o.Repository { + currentPreview = resourceList.Items[i] break } } + if currentPreview.Spec.PullRequest.Repository != o.Repository { return fmt.Errorf("no current preview for %s on pull request #%v", o.Repository, o.Number) } diff --git a/pkg/cmd/get/get_test.go b/pkg/cmd/get/get_test.go index e246b17..ed9af70 100644 --- a/pkg/cmd/get/get_test.go +++ b/pkg/cmd/get/get_test.go @@ -21,7 +21,7 @@ func TestPreviewGet(t *testing.T) { owner := "owner" repo := "test-repo" prNumber := 1 - sourceUrl := fmt.Sprintf("https://fake.com/%s/%s", owner, repo) + sourceURL := fmt.Sprintf("https://fake.com/%s/%s", owner, repo) ns := "jx" tmpDir, err := os.MkdirTemp("", "") require.NoError(t, err, "failed to create temp dir") @@ -33,7 +33,7 @@ func TestPreviewGet(t *testing.T) { devEnv := jxenv.CreateDefaultDevEnvironment(ns) devEnv.Namespace = ns - devEnv.Spec.Source.URL = sourceUrl + devEnv.Spec.Source.URL = sourceURL jxClient := jxfake.NewSimpleClientset(devEnv) @@ -57,7 +57,7 @@ func TestPreviewGet(t *testing.T) { o.ScmClient = scmClient o.PreviewClient = previewClient o.JXClient = jxClient - o.SourceURL = sourceUrl + o.SourceURL = sourceURL o.PullRequestOptions.Number = prNumber o.Repository = repo o.DiscoverFromGit = false @@ -73,7 +73,7 @@ func TestPreviewGet(t *testing.T) { assert.Equal(t, fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber), o.OutputEnvVars["PREVIEW_URL"]) assert.Equal(t, fmt.Sprintf("%s-%s-%v", owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAME"]) assert.Equal(t, fmt.Sprintf("%s-%s-%s-pr-%v", ns, owner, repo, prNumber), o.OutputEnvVars["PREVIEW_NAMESPACE"]) - assert.Equal(t, sourceUrl, o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"]) + assert.Equal(t, sourceURL, o.OutputEnvVars["PREVIEW_PULL_REQUEST_URL"]) } } } diff --git a/pkg/previews/fakepreviews/helpers.go b/pkg/previews/fakepreviews/helpers.go index bb4f83b..90225b3 100644 --- a/pkg/previews/fakepreviews/helpers.go +++ b/pkg/previews/fakepreviews/helpers.go @@ -21,7 +21,7 @@ func CreateTestPreviewAndPullRequest(data *fakescm.Data, ns, owner, repo string, prLink := repoLink + "/pull/" + prNumberText sha := "abcdef1234" previewNamespace := ns + "-" + owner + "-" + repo + "-pr-" + prNumberText - previewUrl := fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber) + previewURL := fmt.Sprintf("https://%s-pr%v.mqube-test.com", repo, prNumber) pr := &scm.PullRequest{ Number: prNumber, Title: prTitle, @@ -66,7 +66,7 @@ func CreateTestPreviewAndPullRequest(data *fakescm.Data, ns, owner, repo string, }, Resources: v1alpha1.Resources{ Namespace: previewNamespace, - URL: previewUrl, + URL: previewURL, }, }, }