Skip to content

Commit 86c548b

Browse files
authored
Port workspaces path fix to src-cli (#779)
1 parent f02a713 commit 86c548b

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

internal/batches/service/workspaces.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"context"
55
"sort"
6+
"strings"
67

78
"github.com/gobwas/glob"
89
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
@@ -137,7 +138,19 @@ func findWorkspaces(
137138
fetchWorkspace = false
138139
}
139140

140-
steps, err := stepsForRepo(spec, util.NewTemplatingRepo(workspace.Repo.Name, workspace.Repo.FileMatches))
141+
// Filter file matches by workspace. Only include paths that are
142+
// _within_ the directory.
143+
paths := map[string]bool{}
144+
for probe := range workspace.Repo.FileMatches {
145+
if strings.HasPrefix(probe, path) {
146+
paths[probe] = true
147+
}
148+
}
149+
150+
repo := *workspace.Repo
151+
repo.FileMatches = paths
152+
153+
steps, err := stepsForRepo(spec, util.NewTemplatingRepo(repo.Name, repo.FileMatches))
141154
if err != nil {
142155
return nil, err
143156
}
@@ -148,7 +161,7 @@ func findWorkspaces(
148161
}
149162

150163
workspaces = append(workspaces, RepoWorkspace{
151-
Repo: workspace.Repo,
164+
Repo: &repo,
152165
Path: path,
153166
OnlyFetchWorkspace: fetchWorkspace,
154167
})

internal/batches/service/workspaces_test.go

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515

1616
func TestFindWorkspaces(t *testing.T) {
1717
repos := []*graphql.Repository{
18-
{ID: "repo-id-0", Name: "github.com/sourcegraph/automation-testing"},
19-
{ID: "repo-id-1", Name: "github.com/sourcegraph/sourcegraph"},
20-
{ID: "repo-id-2", Name: "bitbucket.sgdev.org/SOUR/automation-testing"},
18+
{ID: "repo-id-0", Name: "github.com/sourcegraph/automation-testing", FileMatches: map[string]bool{}},
19+
{ID: "repo-id-1", Name: "github.com/sourcegraph/sourcegraph", FileMatches: map[string]bool{}},
20+
{ID: "repo-id-2", Name: "bitbucket.sgdev.org/SOUR/automation-testing", FileMatches: map[string]bool{}},
21+
{ID: "repo-id-3", Name: "github.com/sourcegraph/src-cli", FileMatches: map[string]bool{"a/b": true, "a/b/c": true, "d/e/f": true}},
2122
}
2223
steps := []batcheslib.Step{{Run: "echo 1"}}
2324

@@ -37,6 +38,7 @@ func TestFindWorkspaces(t *testing.T) {
3738
{Repo: repos[0], Path: ""},
3839
{Repo: repos[1], Path: ""},
3940
{Repo: repos[2], Path: ""},
41+
{Repo: repos[3], Path: ""},
4042
},
4143
},
4244

@@ -52,10 +54,11 @@ func TestFindWorkspaces(t *testing.T) {
5254
{Repo: repos[0], Path: ""},
5355
{Repo: repos[1], Path: ""},
5456
{Repo: repos[2], Path: ""},
57+
{Repo: repos[3], Path: ""},
5558
},
5659
},
5760

58-
"workspace configuration matching 2 repos with no results": {
61+
"workspace configuration matching 3 repos with no results": {
5962
spec: &batcheslib.BatchSpec{
6063
Steps: steps,
6164
Workspaces: []batcheslib.WorkspaceConfiguration{
@@ -68,6 +71,7 @@ func TestFindWorkspaces(t *testing.T) {
6871
},
6972
wantWorkspaces: []RepoWorkspace{
7073
{Repo: repos[1], Path: ""},
74+
{Repo: repos[3], Path: ""},
7175
},
7276
},
7377

@@ -90,6 +94,7 @@ func TestFindWorkspaces(t *testing.T) {
9094
{Repo: repos[2], Path: "a/b"},
9195
{Repo: repos[2], Path: "a/b/c"},
9296
{Repo: repos[2], Path: "d/e/f"},
97+
{Repo: repos[3], Path: ""},
9398
},
9499
},
95100

@@ -116,6 +121,7 @@ func TestFindWorkspaces(t *testing.T) {
116121
{Repo: repos[2], Path: "a/b", OnlyFetchWorkspace: true},
117122
{Repo: repos[2], Path: "a/b/c", OnlyFetchWorkspace: true},
118123
{Repo: repos[2], Path: "d/e/f", OnlyFetchWorkspace: true},
124+
{Repo: repos[3], Path: ""},
119125
},
120126
},
121127
"workspace configuration without 'in' matches all": {
@@ -136,6 +142,53 @@ func TestFindWorkspaces(t *testing.T) {
136142
{Repo: repos[2], Path: "a/b"},
137143
},
138144
},
145+
"workspace gets subset of search_result_paths": {
146+
spec: &batcheslib.BatchSpec{
147+
Steps: steps,
148+
Workspaces: []batcheslib.WorkspaceConfiguration{
149+
{
150+
In: "*src-cli",
151+
RootAtLocationOf: "package.json",
152+
},
153+
},
154+
},
155+
finderResults: finderResults{
156+
repos[3]: {"a/b", "d"},
157+
},
158+
wantWorkspaces: []RepoWorkspace{
159+
{Repo: repos[0], Path: ""},
160+
{Repo: repos[1], Path: ""},
161+
{Repo: repos[2], Path: ""},
162+
{
163+
Repo: &graphql.Repository{
164+
ID: repos[3].ID,
165+
Name: repos[3].Name,
166+
URL: repos[3].URL,
167+
ExternalRepository: repos[3].ExternalRepository,
168+
DefaultBranch: repos[3].DefaultBranch,
169+
Branch: repos[3].Branch,
170+
Commit: repos[3].Commit,
171+
// Only expect the file matches that are children of a/b.
172+
FileMatches: map[string]bool{"a/b": true, "a/b/c": true},
173+
},
174+
Path: "a/b",
175+
},
176+
{
177+
Repo: &graphql.Repository{
178+
ID: repos[3].ID,
179+
Name: repos[3].Name,
180+
URL: repos[3].URL,
181+
ExternalRepository: repos[3].ExternalRepository,
182+
DefaultBranch: repos[3].DefaultBranch,
183+
Branch: repos[3].Branch,
184+
Commit: repos[3].Commit,
185+
// Only expect the file matches that are children of d.
186+
FileMatches: map[string]bool{"d/e/f": true},
187+
},
188+
Path: "d",
189+
},
190+
},
191+
},
139192
}
140193

141194
for name, tt := range tests {

0 commit comments

Comments
 (0)