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

[CAPPL-320] implement HexDecodeWorkflowName #983

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions pkg/workflows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/sha256"
"encoding/hex"
"strings"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
)

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

return result
}

// HexDecodeWorkflowName makes the best effort to decode the hex workflow name.
// In case of failure it will log the error and return the input name.
func HexDecodeWorkflowName(encoded string, logger logger.Logger) string {
decoded, err := hex.DecodeString(encoded)
if err != nil {
logger.Errorf("failed to decode WorkflowName %q: %v", encoded, err)
return encoded
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a logger as a function argument is usually a code smell. Where is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this, we are going to expose a new decodedWorkflowName as part of the metadata. The decoding will happen in chainlink instead. Will change the implementation there to not pass the logger 🙇🏼

}
return string(decoded)
}
27 changes: 27 additions & 0 deletions pkg/workflows/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"testing"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -99,3 +100,29 @@ func TestNormalizeWorkflowName(t *testing.T) {
})
}
}

func TestHexDecodeWorkflowName(t *testing.T) {
tt := []struct {
name string
input string
expected string
}{
{
name: "OK-correct_workflow_name",
input: "776f726b666c6f772d6e616d65",
expected: "workflow-name",
},
{
name: "NOK-incorrect_workflow_name",
input: "776f726b666c6f772d6e616d65!",
expected: "776f726b666c6f772d6e616d65!",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
result := HexDecodeWorkflowName(tc.input, logger.TestSugared(t))
require.Equal(t, tc.expected, result)
})
}
}
Loading