Skip to content

Commit b069551

Browse files
authored
cirrus run: match by a task's alias too, in addition to its name (#722)
1 parent d8d5495 commit b069551

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

internal/executor/taskfilter/taskfilter.go

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,66 @@ func MatchAnyTask() TaskFilter {
1717
}
1818
}
1919

20-
func MatchExactTask(desiredTaskName string) TaskFilter {
20+
func MatchExactTask(desiredTaskNameOrAlias string) TaskFilter {
2121
return func(tasks []*api.Task) ([]*api.Task, error) {
22-
var filteredTasks []*api.Task
22+
var matchedTasks []*api.Task
2323

2424
for _, task := range tasks {
25-
// Ensure that this task's name matches with the name we're looking for
26-
desiredTaskNameLower := strings.ToLower(desiredTaskName)
27-
taskNameLower := strings.ToLower(task.Name)
28-
29-
if !strings.HasPrefix(desiredTaskNameLower, taskNameLower) {
30-
continue
31-
}
32-
33-
// In case we're looking for a task with specific labels — extract them and ensure they all match
34-
desiredLabels := extractLabels(strings.TrimPrefix(desiredTaskNameLower, taskNameLower))
35-
36-
var actualLabels []string
37-
if task.Metadata != nil {
38-
actualLabels = task.Metadata.UniqueLabels
39-
}
40-
41-
if !containsAll(actualLabels, desiredLabels) {
25+
// Ensure that this task's name (or an alias) matches
26+
// with the name (or an alias) that we're looking for
27+
if !matchTask(desiredTaskNameOrAlias, task.Name, task.Metadata) &&
28+
!matchTask(desiredTaskNameOrAlias, taskAlias(task), task.Metadata) {
4229
continue
4330
}
4431

45-
// Clear task's dependencies
32+
// Clear the task's dependencies
4633
task.RequiredGroups = task.RequiredGroups[:0]
4734

48-
filteredTasks = append(filteredTasks, task)
35+
matchedTasks = append(matchedTasks, task)
4936
}
5037

51-
if len(filteredTasks) == 0 {
38+
if len(matchedTasks) == 0 {
5239
return nil, fmt.Errorf("%w: none of the %d task(s) were matched using a %q filter",
53-
ErrNoMatch, len(tasks), desiredTaskName)
40+
ErrNoMatch, len(tasks), desiredTaskNameOrAlias)
5441
}
5542

56-
return filteredTasks, nil
43+
return matchedTasks, nil
44+
}
45+
}
46+
47+
func taskAlias(task *api.Task) string {
48+
if task.Metadata == nil {
49+
return ""
50+
}
51+
52+
if task.Metadata.Properties == nil {
53+
return ""
5754
}
55+
56+
return task.Metadata.Properties["alias"]
57+
}
58+
59+
func matchTask(desiredTaskName string, nameOrAlias string, metadata *api.Task_Metadata) bool {
60+
if nameOrAlias == "" {
61+
return false
62+
}
63+
64+
desiredTaskNameLower := strings.ToLower(desiredTaskName)
65+
nameOrAliasLower := strings.ToLower(nameOrAlias)
66+
67+
if !strings.HasPrefix(desiredTaskNameLower, nameOrAliasLower) {
68+
return false
69+
}
70+
71+
// In case we're looking for a task with specific labels — extract them and ensure they all match
72+
desiredLabels := extractLabels(strings.TrimPrefix(desiredTaskNameLower, nameOrAliasLower))
73+
74+
var actualLabels []string
75+
if metadata != nil {
76+
actualLabels = metadata.UniqueLabels
77+
}
78+
79+
return containsAll(actualLabels, desiredLabels)
5880
}
5981

6082
func extractLabels(s string) (result []string) {

0 commit comments

Comments
 (0)