Repository: plexsystems/konstraint
Pull request #115 (2021-01-18 06:48:52)

Bump EndBug/add-and-commit from v6.2.0 to v7
Bumps [EndBug/add-and-commit](https://github.com/EndBug/add-and-commit) from v6.2.0 to v7.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/EndBug/add-and-commit/blob/master/CHANGELOG.md">EndBug/add-and-commit's changelog</a>.</em></p>
<blockquote>
<h2>[6.2.0] - 2020-12-23</h2>
<h3>Added:</h3>
<ul>
<li><code>push</code> input: allow for custom <code>git push</code> arguments to be used, more info in the README. (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/100">#100</a>)</li>
</ul>
<h2>[6.1.0] - 2020-12-22</h2>
<h3>Added:</h3>
<ul>
<li><code>token</code> input: you can now use this instead of setting the <code>GITHUB_TOKEN</code> env variable, which has been deprecated. This input is optional, its default value is the default <code>secrets.GITHUB_TOKEN</code>. You only need to use this if you want the action to run with a PAT. (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/110">#110</a>)</li>
</ul>
<h3>Deprecated:</h3>
<ul>
<li><code>GITHUB_TOKEN</code>: the use of this env variable is now deprecated in favor of the <code>token</code> input, you'll start receiving warnings if you keep using it. (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/110">#110</a>)</li>
</ul>
<h2>[6.0.0] - 2020-12-22</h2>
<h3>Added:</h3>
<ul>
<li>(BREAKING?) Multiple git commands: support JSON/YAML arrays for the <code>add</code> and <code>remove</code> parameters (you still need pass a string as input, but that can be parsed to an array by the action). Using them will run multiple commands in succession. I don't think this will be really breaking anything, but I've used a major version change just to be sure. There's more info about this stuff on the README. (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/95">#95</a>)</li>
</ul>
<h2>[5.3.0] - 2020-12-12</h2>
<h3>Added:</h3>
<ul>
<li>Outputs: the action now has 3 outputs (<code>committed</code>, <code>pushed</code>, and <code>tagged</code>) that will be set to either <code>true</code> or <code>false</code> to tell you what the action has done. The outputs are also shown in the action logs.</li>
</ul>
<h2>[5.2.0] - 2020-11-11</h2>
<h3>Added:</h3>
<ul>
<li>New <code>push</code> option: this lets you tell the action whether to push commit and tags to the repo. The default value is <code>true</code>, so that the default behavior is not changed. (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/86">#86</a>)</li>
</ul>
<h2>[5.1.2] - 2020-11-10</h2>
<h3>Fixed:</h3>
<ul>
<li>Fixed an issue with the build (issue <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/88">#88</a>)</li>
</ul>
<h2>[5.1.1] - 2020-11-07</h2>
<h3>Fixed:</h3>
<ul>
<li>Fixed typo in parameter name: <code>pull_strategy</code> was written as <code>pull_stategy</code> (PR <a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/83">#83</a>)</li>
</ul>
<h2>[5.1.0] - 2020-10-07</h2>
<h3>Added:</h3>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/EndBug/add-and-commit/commit/b3c7c1e078a023d75fb0bd326e02962575ce0519"><code>b3c7c1e</code></a> release: v7.0.0</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/267e04bd5b3b4b37f913fe9de9dfceca233aa708"><code>267e04b</code></a> chore: remove obsolete input from Input type</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/fab25118228c2a9d7ea09671440d0b59ab17666e"><code>fab2511</code></a> chore(deps): bump js-yaml</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/57a540778bd53ade69e38c29c0e486dc78d85247"><code>57a5407</code></a> docs: add ocean90 as a contributor (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/134">#134</a>)</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/e59e7a19124d49b8553c968fddb38572ab83a4fe"><code>e59e7a1</code></a> [auto] build: update compiled version</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/9c056146c18d9c78261f4ecc153e297f662ede44"><code>9c05614</code></a> Use github.actor as default value for commit author (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/128">#128</a>)</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/1146c681e367a778f6806f74b8e670f9bf46db82"><code>1146c68</code></a> chore(deps-dev): bump eslint-plugin-prettier from 3.3.0 to 3.3.1 (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/133">#133</a>)</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/e3e22e10682c15cba90fddbe81e3ec57a076bd4d"><code>e3e22e1</code></a> chore(deps-dev): bump @vercel/ncc from 0.26.1 to 0.26.2 (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/132">#132</a>)</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/544fb5f5071c183fddc3535fa801a7ddccff6801"><code>544fb5f</code></a> chore(deps-dev): bump husky from 4.3.6 to 4.3.7 (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/130">#130</a>)</li>
<li><a href="https://github.com/EndBug/add-and-commit/commit/60c76066aef226d2c7cfd7207dc9071960e2a800"><code>60c7606</code></a> docs: add ocean90 as a contributor (<a href="https://github-redirect.dependabot.com/EndBug/add-and-commit/issues/129">#129</a>)</li>
<li>Additional commits viewable in <a href="https://github.com/EndBug/add-and-commit/compare/v6.2.0...b3c7c1e078a023d75fb0bd326e02962575ce0519">compare view</a></li>
</ul>
</details>
<br />


Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>


Patch:
diff --git a/.github/workflows/update_cli_docs.yaml b/.github/workflows/update_cli_docs.yaml
index fa6e016..b2275ed 100644
--- a/.github/workflows/update_cli_docs.yaml
+++ b/.github/workflows/update_cli_docs.yaml
@@ -26,7 +26,7 @@ jobs:
           go run .github/gendocs/konstraint.go
 
       - name: commit changes
-        uses: EndBug/add-and-commit@v6.2.0
+        uses: EndBug/add-and-commit@v7
         with:
           author_name: konstraint-bot
           author_email: konstraint-bot@dev.null
diff --git a/docs/cli/konstraint.md b/docs/cli/konstraint.md
index b431244..9d57ab1 100644
--- a/docs/cli/konstraint.md
+++ b/docs/cli/konstraint.md
@@ -17,4 +17,4 @@ A tool to create and manage Gatekeeper CRDs from Rego
 * [konstraint create](konstraint_create.md)	 - Create Gatekeeper constraints from Rego policies
 * [konstraint doc](konstraint_doc.md)	 - Generate documentation from Rego policies
 
-###### Auto generated by spf13/cobra on 14-Dec-2020
+###### Auto generated by spf13/cobra on 19-Jan-2021
diff --git a/docs/cli/konstraint_create.md b/docs/cli/konstraint_create.md
index ff6d788..61aa7e7 100644
--- a/docs/cli/konstraint_create.md
+++ b/docs/cli/konstraint_create.md
@@ -32,4 +32,4 @@ Create constraints with the Gatekeeper enforcement action set to dryrun
 
 * [konstraint](konstraint.md)	 - Konstraint
 
-###### Auto generated by spf13/cobra on 14-Dec-2020
+###### Auto generated by spf13/cobra on 19-Jan-2021
diff --git a/docs/cli/konstraint_doc.md b/docs/cli/konstraint_doc.md
index 525b702..ff01d3d 100644
--- a/docs/cli/konstraint_doc.md
+++ b/docs/cli/konstraint_doc.md
@@ -31,4 +31,4 @@ Set the URL where the policies are hosted at
 
 * [konstraint](konstraint.md)	 - Konstraint
 
-###### Auto generated by spf13/cobra on 14-Dec-2020
+###### Auto generated by spf13/cobra on 19-Jan-2021
diff --git a/internal/commands/create.go b/internal/commands/create.go
index d8cc06f..3a9a1af 100644
--- a/internal/commands/create.go
+++ b/internal/commands/create.go
@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strings"
 
 	"github.com/plexsystems/konstraint/internal/rego"
 
@@ -218,10 +219,10 @@ func getConstraint(violation rego.Rego) (unstructured.Unstructured, error) {
 }
 
 func setKindMatcher(constraint *unstructured.Unstructured, kindMatchers []rego.KindMatcher) error {
-	var kinds []interface{}
-	var apiGroups []interface{}
+	var kinds []string
+	var apiGroups []string
 	for _, kindMatcher := range kindMatchers {
-		kinds = append(kinds, kindMatcher.Kind)
+		kinds = appendIfNotExists(kinds, kindMatcher.Kind)
 	}
 
 	for _, kindMatcher := range kindMatchers {
@@ -229,21 +230,12 @@ func setKindMatcher(constraint *unstructured.Unstructured, kindMatchers []rego.K
 		if kindMatcher.APIGroup == "core" {
 			apiGroup = ""
 		}
-
-		var exists bool
-		for _, addedGroup := range apiGroups {
-			if apiGroup == addedGroup {
-				exists = true
-			}
-		}
-		if !exists {
-			apiGroups = append(apiGroups, apiGroup)
-		}
+		apiGroups = appendIfNotExists(apiGroups, apiGroup)
 	}
 
 	constraintMatcher := map[string]interface{}{
-		"apiGroups": apiGroups,
-		"kinds":     kinds,
+		"apiGroups": toInterfaceSlice(apiGroups),
+		"kinds":     toInterfaceSlice(kinds),
 	}
 
 	if err := unstructured.SetNestedSlice(constraint.Object, []interface{}{constraintMatcher}, "spec", "match", "kinds"); err != nil {
@@ -252,6 +244,23 @@ func setKindMatcher(constraint *unstructured.Unstructured, kindMatchers []rego.K
 	return nil
 }
 
+func appendIfNotExists(currentItems []string, newItem string) []string {
+	for _, item := range currentItems {
+		if strings.EqualFold(newItem, item) {
+			return currentItems
+		}
+	}
+	return append(currentItems, newItem)
+}
+
+func toInterfaceSlice(input []string) []interface{} {
+	res := make([]interface{}, len(input))
+	for i, v := range input {
+		res[i] = v
+	}
+	return res
+}
+
 func setMatchLabelsMatcher(constraint *unstructured.Unstructured, matcher rego.MatchLabelsMatcher) error {
 	if err := unstructured.SetNestedStringMap(constraint.Object, matcher, "spec", "match", "labelSelector", "matchLabels"); err != nil {
 		return fmt.Errorf("set constraint labelSelector.matchLabels matchers: %w", err)


Test patch:
diff --git a/internal/commands/create_test.go b/internal/commands/create_test.go
new file mode 100644
index 0000000..a41ddc7
--- /dev/null
+++ b/internal/commands/create_test.go
@@ -0,0 +1,65 @@
+package commands
+
+import (
+	"reflect"
+	"testing"
+)
+
+func TestAppendIfNotExists(t *testing.T) {
+	type args struct {
+		currentItems []string
+		newItem      string
+	}
+	tests := []struct {
+		name string
+		args args
+		want []string
+	}{
+		{
+			"append",
+			args{currentItems: []string{"a"}, newItem: "b"},
+			[]string{"a", "b"},
+		},
+		{
+			"skip",
+			args{currentItems: []string{"a"}, newItem: "a"},
+			[]string{"a"},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := appendIfNotExists(tt.args.currentItems, tt.args.newItem); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("appendIfNotExists() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
+
+func TestToInterfaceSlice(t *testing.T) {
+	type args struct {
+		input []string
+	}
+	tests := []struct {
+		name string
+		args args
+		want []interface{}
+	}{
+		{
+			"empty",
+			args{[]string{}},
+			[]interface{}{},
+		},
+		{
+			"valid",
+			args{[]string{"a", "b"}},
+			[]interface{}{"a", "b"},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := toInterfaceSlice(tt.args.input); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("toInterfaceSlice() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}