Skip to content

Commit b93994e

Browse files
committed
Fix case for 'API Key' and set up a table test to ensure that all cases successfully pass
1 parent bc3f0f8 commit b93994e

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

cmd/contrib/main.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,7 @@ func newPlugin() error {
200200
// "GitHub API Key" => "API Key"
201201
// "Credentials" => "Credentials"
202202
lengthCutoff := 7
203-
fieldNameSplit := []string{credNameSplit[len(credNameSplit)-1]}
204-
for i := range credNameSplit[:len(credNameSplit)-1] {
205-
// Skip credNameSplit's last word because it's already added to fieldNameSplit
206-
if i == 0 {
207-
continue
208-
}
209-
word := credNameSplit[len(credNameSplit)-1-i]
210-
if len(strings.Join(append(fieldNameSplit, word), " ")) > lengthCutoff {
211-
break
212-
}
213-
fieldNameSplit = append([]string{word}, fieldNameSplit...)
214-
}
203+
fieldNameSplit := fieldNameSplitFromCredNameSplit(credNameSplit, lengthCutoff)
215204
result.FieldName = strings.Join(fieldNameSplit, " ")
216205
result.FieldNameUpperCamelCase = strings.Join(fieldNameSplit, "")
217206
result.CredentialEnvVarName = strings.ToUpper(strings.Join(append([]string{result.Name}, fieldNameSplit...), "_"))
@@ -644,3 +633,28 @@ func generateRegistryJSON() error {
644633
}
645634
return os.WriteFile(filepath.Join("plugins", "registry.json"), b, 0600)
646635
}
636+
637+
// fieldNameSplitFromCredNameSplit takes in a credential name split array and returns a field name split array.
638+
//
639+
// As a placeholder, assume the field name is the short version (max 7 chars, including space between words) of the credential name, starting from the last word.
640+
//
641+
// When the last word of the credential name is greater than seven characters, the last word is used as the field name.
642+
//
643+
// For example:
644+
//
645+
// "Personal Access Token" => "Token"
646+
// "Secret Key" => "Key"
647+
// "API Key" => "API Key"
648+
// "GitHub API Key" => "API Key"
649+
// "Credentials" => "Credentials"
650+
func fieldNameSplitFromCredNameSplit(credNameSplit []string, lengthCutoff int) []string {
651+
fieldNameSplit := []string{credNameSplit[len(credNameSplit)-1]}
652+
for i := len(credNameSplit) - 2; i >= 0; i-- {
653+
word := credNameSplit[i]
654+
if len(strings.Join(append(fieldNameSplit, word), " ")) > lengthCutoff {
655+
break
656+
}
657+
fieldNameSplit = append([]string{word}, fieldNameSplit...)
658+
}
659+
return fieldNameSplit
660+
}

cmd/contrib/main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -27,3 +28,41 @@ func TestValidateCredentialNameReturnError(t *testing.T) {
2728
})
2829
}
2930
}
31+
32+
type FieldNameSplitFromCredNameSplit struct {
33+
FieldNameSplit []string
34+
CredNameSplit []string
35+
}
36+
37+
func TestValidateFieldNameSplitFromCredNameSplitReturnError(t *testing.T) {
38+
lengthCutoff := 7
39+
cases := []FieldNameSplitFromCredNameSplit{
40+
{
41+
CredNameSplit: []string{"Personal", "Access", "Token"},
42+
FieldNameSplit: []string{"Token"},
43+
},
44+
{
45+
CredNameSplit: []string{"Secret", "Key"},
46+
FieldNameSplit: []string{"Key"},
47+
},
48+
{
49+
CredNameSplit: []string{"API", "Key"},
50+
FieldNameSplit: []string{"API", "Key"},
51+
},
52+
{
53+
CredNameSplit: []string{"GitHub", "API", "Key"},
54+
FieldNameSplit: []string{"API", "Key"},
55+
},
56+
{
57+
CredNameSplit: []string{"Credentials"},
58+
FieldNameSplit: []string{"Credentials"},
59+
},
60+
}
61+
62+
for _, c := range cases {
63+
t.Run(strings.Join(c.CredNameSplit, " "), func(t *testing.T) {
64+
fieldNameSplit := fieldNameSplitFromCredNameSplit(c.CredNameSplit, lengthCutoff)
65+
assert.Equal(t, c.FieldNameSplit, fieldNameSplit)
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)