Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(workflows): adds workflow name normalizer #980

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/workflows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ func GenerateWorkflowID(owner []byte, name string, workflow []byte, config []byt

return sha, nil
}

// HashTruncateName returns the SHA-256 hash of the workflow name truncated to the first 10 bytes.
func HashTruncateName(name string) [10]byte {
// Compute SHA-256 hash of the input string
hash := sha256.Sum256([]byte(name))

// Truncate the hash to 10 bytes
var result [10]byte
copy(result[:], hash[:10])

return result
}
33 changes: 33 additions & 0 deletions pkg/workflows/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,36 @@ func Test_GenerateWorkflowIDFromStrings(t *testing.T) {
_, err = GenerateWorkflowIDFromStrings(owner, "porporpore", []byte("workflow"), []byte("config"), "http://mysecrets.com")
assert.ErrorContains(t, err, "encoding/hex")
}

func TestNormalizeWorkflowName(t *testing.T) {
tt := []struct {
input string
expected [10]byte
}{
{
input: "Hello, world!",
expected: [10]byte{0x31, 0x5f, 0x5b, 0xdb, 0x76, 0xd0, 0x78, 0xc4, 0x3b, 0x8a},
},
{
input: "My Incredible Workflow Name",
expected: [10]byte{0x84, 0x00, 0x2e, 0xb9, 0xe2, 0xa0, 0x6b, 0x09, 0x97, 0x7c},
},
{
input: "You either die a hero, or live long enough to see yourself become the villain.",
expected: [10]byte{0x6b, 0xa1, 0xf7, 0xa6, 0xa0, 0x91, 0x95, 0x1a, 0x2d, 0xd2},
},
}

for _, tc := range tt {
t.Run(tc.input, func(t *testing.T) {
// Call the function with the test input
result := HashTruncateName(tc.input)

// Assert that the result is exactly the expected output
require.Equal(t, tc.expected, result)

// Assert that the result is 10 bytes long
require.Len(t, result, 10)
})
}
}
Loading