Repository: Azure/draft
Pull request #269 (2024-04-24 18:09:23)

Added yaml file extension validation for manifest retrieval code
# Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)
Feature # (details)

## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Is it a  breaking change which will impact consuming tool(s).

- [x] TestIsYaml


# Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream modules



msftclas: 
@bacongobbler,
Thanks for your contribution as a Microsoft full-time employee or intern. You do not need to sign a CLA.
_Thanks,
Microsoft Pull Request Bot_
michelleN: `make bootstrap` fails on this guy :(
bacongobbler: This looks a lot like https://github.com/beego/wetalk/issues/32 which appears to indicate it's a RAM issue with our CI system. I'll keep trying to kick off jobs and see if we can free up some RAM on the nodes.
bacongobbler: punting to v0.8.0 due to CI errors.
bacongobbler: punting to 0.9; this isn't important to get in for this release... Plus 2.7 with k8s 1.8 compatibility is just around the corner and has some really nice client fixes in helm.
bacongobbler: closing and rolling this into #380.

Patch:
diff --git a/.github/workflows/integration-json.yml b/.github/workflows/integration-json.yml
index ef8f4ac..bea6718 100644
--- a/.github/workflows/integration-json.yml
+++ b/.github/workflows/integration-json.yml
@@ -16,7 +16,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v2
         with:
-          go-version: 1.18.2
+          go-version: 1.22
       - name: make
         run: make
       - uses: actions/upload-artifact@v2
diff --git a/.github/workflows/integration-linux.yml b/.github/workflows/integration-linux.yml
index 643f1c8..86c0952 100644
--- a/.github/workflows/integration-linux.yml
+++ b/.github/workflows/integration-linux.yml
@@ -2,7 +2,7 @@
 name: draft Linux Integrations
 on:
   pull_request:
-    branches: [ main ]
+    branches: [ main, staging ]
   workflow_dispatch:
 jobs:
   build:
@@ -12,7 +12,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v4
         with:
-          go-version: 1.18.2
+          go-version: 1.22
       - name: make
         run: make
       - uses: actions/upload-artifact@v3
diff --git a/.github/workflows/integration-windows.yml b/.github/workflows/integration-windows.yml
index 397e9a0..4b55e04 100644
--- a/.github/workflows/integration-windows.yml
+++ b/.github/workflows/integration-windows.yml
@@ -1,7 +1,7 @@
 name: draft Windows Integrations
 on:
-  pull_request_review:
-    types: [submitted]
+  pull_request:
+    branches: [ main, staging ]
   workflow_dispatch:
 jobs:
   build:
@@ -11,7 +11,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v2
         with:
-          go-version: 1.18
+          go-version: 1.22
       - name: make
         run: make
       - uses: actions/upload-artifact@v3
diff --git a/.github/workflows/release-and-publish.yml b/.github/workflows/release-and-publish.yml
index 77e0257..ece40e6 100644
--- a/.github/workflows/release-and-publish.yml
+++ b/.github/workflows/release-and-publish.yml
@@ -38,7 +38,7 @@ jobs:
     - name: Set up Go
       uses: actions/setup-go@v4
       with:
-        go-version: 1.18.2
+        go-version: 1.22
     # Check if the newest tag already exists
     - name: Check if tag exist
       uses: mukunku/tag-exists-action@5dfe2bf779fe5259360bb10b2041676713dcc8a3 # v1.1.0
diff --git a/cmd/validate.go b/cmd/validate.go
index 24d26d9..bdd8b9a 100644
--- a/cmd/validate.go
+++ b/cmd/validate.go
@@ -54,22 +54,36 @@ func isDirectory(path string) (bool, error) {
 	return fileInfo.IsDir(), nil
 }
 
-// getManifestFiles uses filepath.Walk to retrieve a list of the manifest files within the given manifest path
+// isYAML determines if a file is of the YAML extension or not
+func isYAML(path string) bool {
+	return filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml"
+}
+
+// getManifests uses filepath.Walk to retrieve a list of the manifest files within the given manifest path
 func getManifestFiles(p string) ([]safeguards.ManifestFile, error) {
 	var manifestFiles []safeguards.ManifestFile
 
-	err := filepath.Walk(p, func(filepath string, info fs.FileInfo, err error) error {
+	noYamlFiles := true
+	err := filepath.Walk(p, func(walkPath string, info fs.FileInfo, err error) error {
 		manifest := safeguards.ManifestFile{}
+		// skip when walkPath is just given path and also a directory
+		if p == walkPath && info.IsDir() {
+			return nil
+		}
+
 		if err != nil {
-			return fmt.Errorf("error walking path %s with error: %w", filepath, err)
+			return fmt.Errorf("error walking path %s with error: %w", walkPath, err)
 		}
 
-		if !info.IsDir() && info.Name() != "" {
+		if !info.IsDir() && info.Name() != "" && isYAML(walkPath) {
 			log.Debugf("%s is not a directory, appending to manifestFiles", info.Name())
+			noYamlFiles = false
 
 			manifest.Name = info.Name()
-			manifest.Path = filepath
+			manifest.Path = walkPath
 			manifestFiles = append(manifestFiles, manifest)
+		} else if !isYAML(p) {
+			log.Debugf("%s is not a manifest file, skipping...", info.Name())
 		} else {
 			log.Debugf("%s is a directory, skipping...", info.Name())
 		}
@@ -79,6 +93,9 @@ func getManifestFiles(p string) ([]safeguards.ManifestFile, error) {
 	if err != nil {
 		return nil, fmt.Errorf("could not walk directory: %w", err)
 	}
+	if noYamlFiles {
+		return nil, fmt.Errorf("no manifest files found within given path")
+	}
 
 	return manifestFiles, nil
 }
@@ -101,11 +118,13 @@ func (vc *validateCmd) run(c *cobra.Command) error {
 		if err != nil {
 			return err
 		}
-	} else {
+	} else if isYAML(vc.manifestPath) {
 		manifestFiles = append(manifestFiles, safeguards.ManifestFile{
 			Name: path.Base(vc.manifestPath),
 			Path: vc.manifestPath,
 		})
+	} else {
+		return fmt.Errorf("expected at least one .yaml or .yml file within given path")
 	}
 
 	if err != nil {
diff --git a/template/dockerfiles/go/Dockerfile b/template/dockerfiles/go/Dockerfile
index 93ef934..6975ca6 100644
--- a/template/dockerfiles/go/Dockerfile
+++ b/template/dockerfiles/go/Dockerfile
@@ -6,8 +6,7 @@ WORKDIR /go/src/app
 COPY . .
 
 ARG GO111MODULE=off
-RUN go get -d -v ./...
-RUN go build -v -o app ./...
+RUN go build -v -o app ./main.go
 RUN mv ./app /go/bin/
 
 CMD ["app"]
\ No newline at end of file
diff --git a/template/dockerfiles/gomodule/Dockerfile b/template/dockerfiles/gomodule/Dockerfile
index b5f1689..b6b8a05 100644
--- a/template/dockerfiles/gomodule/Dockerfile
+++ b/template/dockerfiles/gomodule/Dockerfile
@@ -5,7 +5,7 @@ EXPOSE {{PORT}}
 WORKDIR /go/src/app
 COPY . .
 
-RUN go mod vendor
+RUN go get
 RUN go build -v -o app
 RUN mv ./app /go/bin/
 


Test patch:
diff --git a/.github/workflows/e2e-info.yml b/.github/workflows/e2e-info.yml
index 69f9b59..7132658 100644
--- a/.github/workflows/e2e-info.yml
+++ b/.github/workflows/e2e-info.yml
@@ -16,7 +16,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v3
         with:
-          go-version: 1.18.2
+          go-version: 1.22
       - name: make
         run: make
       - name: Validate JSON
diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml
index a013e3f..71943b8 100644
--- a/.github/workflows/unit-tests.yml
+++ b/.github/workflows/unit-tests.yml
@@ -14,6 +14,6 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v3
         with:
-          go-version: 1.21
+          go-version: 1.22
       - name: make
         run: make run-unit-tests
\ No newline at end of file
diff --git a/cmd/validate_test.go b/cmd/validate_test.go
index 067aae8..8eb5592 100644
--- a/cmd/validate_test.go
+++ b/cmd/validate_test.go
@@ -32,6 +32,25 @@ func TestIsDirectory(t *testing.T) {
 	assert.NotNil(t, err)
 }
 
+// TestIsYAML tests the isYAML function for proper returns
+func TestIsYAML(t *testing.T) {
+	dirNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml")
+	dirYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
+	fileNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml/readme.md")
+	fileYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")
+
+	assert.False(t, isYAML(fileNotYaml))
+	assert.True(t, isYAML(fileYaml))
+
+	manifestFiles, err := getManifestFiles(dirNotYaml)
+	assert.Nil(t, manifestFiles)
+	assert.NotNil(t, err)
+
+	manifestFiles, err = getManifestFiles(dirYaml)
+	assert.NotNil(t, manifestFiles)
+	assert.Nil(t, err)
+}
+
 // TestRunValidate tests the run command for `draft validate` for proper returns
 func TestRunValidate(t *testing.T) {
 	ctx := context.TODO()
diff --git a/pkg/safeguards/tests/not-yaml/readme.md b/pkg/safeguards/tests/not-yaml/readme.md
new file mode 100644
index 0000000..68a4528
--- /dev/null
+++ b/pkg/safeguards/tests/not-yaml/readme.md
@@ -0,0 +1,1 @@
+this is a test file
\ No newline at end of file
diff --git a/test/gen_integration.sh b/test/gen_integration.sh
index 07665d7..722f5eb 100755
--- a/test/gen_integration.sh
+++ b/test/gen_integration.sh
@@ -26,7 +26,7 @@ echo "# this file is generated using gen_integration.sh
 name: draft Linux Integrations
 on:
   pull_request:
-    branches: [ main ]
+    branches: [ main, staging ]
   workflow_dispatch:
 jobs:
   build:
@@ -36,7 +36,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v4
         with:
-          go-version: 1.18.2
+          go-version: 1.22
       - name: make
         run: make
       - uses: actions/upload-artifact@v3
@@ -52,8 +52,8 @@ jobs:
 
 echo "name: draft Windows Integrations
 on:
-  pull_request_review:
-    types: [submitted]
+  pull_request:
+    branches: [ main, staging ]
   workflow_dispatch:
 jobs:
   build:
@@ -63,7 +63,7 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v2
         with:
-          go-version: 1.18
+          go-version: 1.22
       - name: make
         run: make
       - uses: actions/upload-artifact@v3
diff --git a/test/integration/go/helm.yaml b/test/integration/go/helm.yaml
index 042fe29..43cf1b7 100644
--- a/test/integration/go/helm.yaml
+++ b/test/integration/go/helm.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.20"
+    value: "1.22"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration/go/kustomize.yaml b/test/integration/go/kustomize.yaml
index b8cf0be..67bf8fe 100644
--- a/test/integration/go/kustomize.yaml
+++ b/test/integration/go/kustomize.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.20"
+    value: "1.22"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration/go/manifest.yaml b/test/integration/go/manifest.yaml
index 4e33dea..6739c05 100644
--- a/test/integration/go/manifest.yaml
+++ b/test/integration/go/manifest.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.20"
+    value: "1.22"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration/rust/helm.yaml b/test/integration/rust/helm.yaml
index 99e3a0e..aa85db6 100644
--- a/test/integration/rust/helm.yaml
+++ b/test/integration/rust/helm.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.59.0"
+    value: "1.77.0"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration/rust/kustomize.yaml b/test/integration/rust/kustomize.yaml
index 6c03239..3036ee3 100644
--- a/test/integration/rust/kustomize.yaml
+++ b/test/integration/rust/kustomize.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.59.0"
+    value: "1.77.0"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration/rust/manifest.yaml b/test/integration/rust/manifest.yaml
index fc6044e..97e411b 100644
--- a/test/integration/rust/manifest.yaml
+++ b/test/integration/rust/manifest.yaml
@@ -12,7 +12,7 @@ deployVariables:
     value: "host.minikube.internal:5001/testapp"
 languageVariables:
   - name: "VERSION"
-    value: "1.59.0"
+    value: "1.77.0"
   - name: "BUILDERVERSION"
     value: "null"
   - name: "PORT"
diff --git a/test/integration_config.json b/test/integration_config.json
index 8dd636b..d81f53f 100644
--- a/test/integration_config.json
+++ b/test/integration_config.json
@@ -1,14 +1,14 @@
 [
   {
     "language": "gomodule",
-    "version": "1.18",
+    "version": "1.22",
     "port": "1323",
     "serviceport": 80,
     "repo": "gambtho/go_echo"
   },
   {
     "language": "go",
-    "version": "1.20",
+    "version": "1.22",
     "port": "8080",
     "serviceport": 8080,
     "repo": "davidgamero/go-echo-no-mod"
@@ -22,7 +22,7 @@
   },
   {
     "language": "rust",
-    "version": "1.59.0",
+    "version": "1.77.0",
     "port": "8000",
     "serviceport": 80,
     "repo": "OliverMKing/tiny-http-hello-world"