Skip to content

Commit 49b0582

Browse files
authored
Helper function for backward compatible execution IDs (#1970)
1 parent e481d4d commit 49b0582

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

pkg/workflows/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"crypto/sha256"
55
"encoding/binary"
66
"encoding/hex"
7+
"fmt"
8+
"strconv"
79
"strings"
810

911
"golang.org/x/crypto/sha3"
1012
)
1113

14+
// Deprecated: Use GenerateExecutionIDWithTriggerIndex instead.
1215
func EncodeExecutionID(workflowID, eventID string) (string, error) {
1316
s := sha256.New()
1417
_, err := s.Write([]byte(workflowID))
@@ -24,6 +27,34 @@ func EncodeExecutionID(workflowID, eventID string) (string, error) {
2427
return hex.EncodeToString(s.Sum(nil)), nil
2528
}
2629

30+
func GenerateExecutionIDWithTriggerIndex(workflowID, triggerEventID string, triggerIndex int) (string, error) {
31+
s := sha256.New()
32+
_, err := s.Write([]byte(workflowID))
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
_, err = s.Write([]byte(triggerEventID))
38+
if err != nil {
39+
return "", err
40+
}
41+
42+
_, err = s.Write([]byte(strconv.Itoa(triggerIndex)))
43+
if err != nil {
44+
return "", err
45+
}
46+
47+
return hex.EncodeToString(s.Sum(nil)), nil
48+
}
49+
50+
func GetTriggerReferenceID(triggerIndex int) string {
51+
return fmt.Sprintf("trigger_%d", triggerIndex)
52+
}
53+
54+
func GetTriggerIndexFromReferenceID(referenceID string) (int, error) {
55+
return strconv.Atoi(strings.TrimPrefix(referenceID, "trigger_"))
56+
}
57+
2758
func GenerateWorkflowIDFromStrings(owner string, name string, workflow []byte, config []byte, secretsURL string) (string, error) {
2859
ownerWithoutPrefix := owner
2960
if strings.HasPrefix(owner, "0x") {

pkg/workflows/utils_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func Test_EncodeExecutionID(t *testing.T) {
4242
assert.NotEqual(t, reversed, actual)
4343
}
4444

45+
func Test_GetTriggerReferenceID_RoundTrip(t *testing.T) {
46+
for _, idx := range []int{0, 1, 5, 100} {
47+
refID := GetTriggerReferenceID(idx)
48+
got, err := GetTriggerIndexFromReferenceID(refID)
49+
require.NoError(t, err)
50+
assert.Equal(t, idx, got)
51+
}
52+
}
53+
4554
func Test_GenerateWorkflowIDFromStrings(t *testing.T) {
4655
// With prefix
4756
owner := "0x26729408f179371be6433b9585d8427f121bfe82"

0 commit comments

Comments
 (0)