Skip to content

Commit 7eed898

Browse files
committed
feat: implement HexDecodeWorkflowName
1 parent db7919d commit 7eed898

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pkg/workflows/utils.go

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"crypto/sha256"
55
"encoding/hex"
66
"strings"
7+
8+
"github.com/smartcontractkit/chainlink-common/pkg/logger"
79
)
810

911
func EncodeExecutionID(workflowID, eventID string) (string, error) {
@@ -84,3 +86,14 @@ func HashTruncateName(name string) [10]byte {
8486

8587
return result
8688
}
89+
90+
// HexDecodeWorkflowName makes the best effort to decode the hex workflow name.
91+
// In case of failure it will log the error and return the input name.
92+
func HexDecodeWorkflowName(encoded string, logger logger.Logger) string {
93+
decoded, err := hex.DecodeString(encoded)
94+
if err != nil {
95+
logger.Errorf("failed to decode WorkflowName %q: %v", encoded, err)
96+
return encoded
97+
}
98+
return string(decoded)
99+
}

pkg/workflows/utils_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"testing"
77

8+
"github.com/smartcontractkit/chainlink-common/pkg/logger"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
)
@@ -99,3 +100,29 @@ func TestNormalizeWorkflowName(t *testing.T) {
99100
})
100101
}
101102
}
103+
104+
func TestHexDecodeWorkflowName(t *testing.T) {
105+
tt := []struct {
106+
name string
107+
input string
108+
expected string
109+
}{
110+
{
111+
name: "OK-correct_workflow_name",
112+
input: "776f726b666c6f772d6e616d65",
113+
expected: "workflow-name",
114+
},
115+
{
116+
name: "NOK-incorrect_workflow_name",
117+
input: "776f726b666c6f772d6e616d65!",
118+
expected: "776f726b666c6f772d6e616d65!",
119+
},
120+
}
121+
122+
for _, tc := range tt {
123+
t.Run(tc.name, func(t *testing.T) {
124+
result := HexDecodeWorkflowName(tc.input, logger.TestSugared(t))
125+
require.Equal(t, tc.expected, result)
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)