Skip to content

Commit c87b36d

Browse files
[CAP-9383] allow matching tasks by plain name in local dev server store (#322)
Add plain name matching to `GetTask` in addition to service/task matching. Tasks defined in a local environment aren't guaranteed to have workflow service slugs. GitOrigin-RevId: 3691221446432a0d5bcf98846635aa0c6a3c8cc2
1 parent b27d983 commit c87b36d

3 files changed

Lines changed: 49 additions & 12 deletions

File tree

pkg/workflows/orchestrator/coordinator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestStartTask(t *testing.T) {
108108
&noopStatusReporter{},
109109
)
110110

111-
_, err = coordinator.StartTask(ctx, "fake-workflow/test-task", []byte{}, nil)
111+
_, err = coordinator.StartTask(ctx, "test-task", []byte{}, nil)
112112
require.NoError(t, err)
113113

114114
ts := s.GetTasks()
@@ -216,7 +216,7 @@ func TestStartTaskWithSubtask(t *testing.T) {
216216

217217
// Trigger a task and then we will simulate a subtask
218218
go func() {
219-
_, err = coordinator.StartTask(ctx, "fake-workflow/test-task", []byte{}, nil)
219+
_, err = coordinator.StartTask(ctx, "test-task", []byte{}, nil)
220220
require.NoError(t, err)
221221
}()
222222

@@ -348,7 +348,7 @@ func TestStartTaskProcessExits(t *testing.T) {
348348
&noopStatusReporter{},
349349
)
350350

351-
taskRun, err := coordinator.StartTask(ctx, "fake-workflow/test-task", []byte{}, nil)
351+
taskRun, err := coordinator.StartTask(ctx, "test-task", []byte{}, nil)
352352
require.NoError(t, err)
353353

354354
// The background goroutine should detect the process exit and fail the task run

pkg/workflows/store/store.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package store
33
import (
44
"encoding/json"
55
"fmt"
6-
"regexp"
76
"slices"
7+
"strings"
88
"sync"
99
"time"
1010

@@ -184,14 +184,19 @@ func (s *TaskStore) GetTask(taskID string) *Task {
184184
s.mu.Lock()
185185
defer s.mu.Unlock()
186186

187-
for i := range s.tasks {
188-
if s.tasks[i].ID == taskID {
189-
return s.tasks[i]
190-
}
187+
// Strip workflow prefix (e.g. "workflow-name/task-name") and match by
188+
// plain task name so production slugs work against local dev.
189+
var name string
190+
hasStrippedName := false
191+
if idx := strings.LastIndex(taskID, "/"); idx != -1 {
192+
name = taskID[idx+1:]
193+
hasStrippedName = true
191194
}
192195

193-
for _, task := range s.tasks {
194-
if regexp.MustCompile(fmt.Sprintf("^.*/%s(:.*)?$", task.Name)).MatchString(taskID) {
196+
for i := range s.tasks {
197+
task := s.tasks[i]
198+
199+
if task.ID == taskID || task.Name == taskID || (hasStrippedName && task.Name == name) {
195200
return task
196201
}
197202
}

pkg/workflows/store/store_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ func TestGetTask(t *testing.T) {
141141
},
142142
})
143143

144-
task := s.GetTask("foo/test")
145-
require.Equal(t, "test", task.Name)
144+
t.Run("by name", func(t *testing.T) {
145+
task := s.GetTask("test")
146+
require.NotNil(t, task)
147+
require.Equal(t, "test", task.Name)
148+
})
149+
150+
t.Run("by ID", func(t *testing.T) {
151+
tasks := s.GetTasks()
152+
task := s.GetTask(tasks[0].ID)
153+
require.NotNil(t, task)
154+
require.Equal(t, "test", task.Name)
155+
})
156+
157+
t.Run("by slug with workflow prefix", func(t *testing.T) {
158+
task := s.GetTask("my-workflow/test")
159+
require.NotNil(t, task)
160+
require.Equal(t, "test", task.Name)
161+
})
162+
163+
t.Run("by slug with multi-segment prefix", func(t *testing.T) {
164+
task := s.GetTask("org/my-workflow/test")
165+
require.NotNil(t, task)
166+
require.Equal(t, "test", task.Name)
167+
})
168+
169+
t.Run("not found", func(t *testing.T) {
170+
task := s.GetTask("nonexistent")
171+
require.Nil(t, task)
172+
})
173+
174+
t.Run("slug not found", func(t *testing.T) {
175+
task := s.GetTask("my-workflow/nonexistent")
176+
require.Nil(t, task)
177+
})
146178
}

0 commit comments

Comments
 (0)