Skip to content

Commit 8f71c92

Browse files
Merge pull request #263 from arunsathiya/fix/164-missing-field-name
fix(new-plugin): Set only the last word of the credential name as the field name, if it's larger than seven characters
2 parents a753919 + 59a5c55 commit 8f71c92

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

cmd/contrib/main.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,18 @@ func newPlugin() error {
189189
}
190190
}
191191

192-
// As a placeholder, assume the field name is the short version (max 7 chars) of the credential name, starting
193-
// from the last word. For example:
192+
// As a placeholder, assume the field name is the short version (max 7 chars) of the credential name, starting from the last word.
193+
//
194+
// When the last word of the credential name is greater than seven characters, the last word is used as the field name.
195+
//
196+
// For example:
194197
// "Personal Access Token" => "Token"
195198
// "Secret Key" => "Key"
196199
// "API Key" => "API Key"
197-
var fieldNameSplit []string
200+
// "GitHub API Key" => "API Key"
201+
// "Credentials" => "Credentials"
198202
lengthCutoff := 7
199-
for i := range credNameSplit {
200-
word := credNameSplit[len(credNameSplit)-1-i]
201-
if len(strings.Join(append(fieldNameSplit, word), " ")) > lengthCutoff {
202-
break
203-
}
204-
205-
fieldNameSplit = append([]string{word}, fieldNameSplit...)
206-
}
203+
fieldNameSplit := fieldNameSplitFromCredNameSplit(credNameSplit, lengthCutoff)
207204
result.FieldName = strings.Join(fieldNameSplit, " ")
208205
result.FieldNameUpperCamelCase = strings.Join(fieldNameSplit, "")
209206
result.CredentialEnvVarName = strings.ToUpper(strings.Join(append([]string{result.Name}, fieldNameSplit...), "_"))
@@ -636,3 +633,28 @@ func generateRegistryJSON() error {
636633
}
637634
return os.WriteFile(filepath.Join("plugins", "registry.json"), b, 0600)
638635
}
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 TestValidateFieldNameSplitFromCredNameSplit(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)