Skip to content

Commit

Permalink
feat(workflows): adds workflow name normalizer (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 authored Dec 20, 2024
1 parent 7c7d06f commit 41f4bc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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)
})
}
}

0 comments on commit 41f4bc0

Please sign in to comment.