Skip to content

Commit 5052c18

Browse files
committed
Make search results accessible in campaign spec templates
1 parent bb73932 commit 5052c18

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

internal/campaigns/graphql/repository.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Repository struct {
3030
URL string
3131
ExternalRepository struct{ ServiceType string }
3232
DefaultBranch *Branch
33+
34+
FileMatches map[string]bool
3335
}
3436

3537
func (r *Repository) BaseRef() string {
@@ -43,3 +45,22 @@ func (r *Repository) Rev() string {
4345
func (r *Repository) Slug() string {
4446
return strings.ReplaceAll(r.Name, "/", "-")
4547
}
48+
49+
func (r *Repository) FileMatchPaths() string {
50+
files := []string{}
51+
for f := range r.FileMatches {
52+
files = append(files, f)
53+
}
54+
return strings.Join(files, " ")
55+
}
56+
57+
type fileMatchPathList []string
58+
59+
func (f fileMatchPathList) String() string { return strings.Join(f, " ") }
60+
61+
func (r *Repository) FileMatchPathList() (list fileMatchPathList) {
62+
for f := range r.FileMatches {
63+
list = append(list, f)
64+
}
65+
return list
66+
}

internal/campaigns/run_steps.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ func parseStepRun(stepCtx StepContext, run string) (*template.Template, error) {
186186
t := template.New("step-run").Delims("${{", "}}")
187187

188188
funcMap := template.FuncMap{
189-
"repository_name": func() string { return stepCtx.Repository.Name },
189+
"repository_name": func() string { return stepCtx.Repository.Name },
190+
"search_results": stepCtx.Repository.FileMatchPathList,
191+
"search_result_list": stepCtx.Repository.FileMatchPathList,
192+
"join": func(sl []string, sep string) string { return strings.Join(sl, sep) },
190193
}
191194

192195
funcMap["modified_files"] = stepCtx.PreviousStep.ModifiedFiles

internal/campaigns/run_steps_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ func TestParseStepRun(t *testing.T) {
6060
run: `${{ modified_files }} ${{ repository_name }}`,
6161
want: ` github.com/sourcegraph/src-cli`,
6262
},
63+
{
64+
stepCtx: StepContext{
65+
Repository: &graphql.Repository{
66+
Name: "github.com/sourcegraph/src-cli",
67+
FileMatches: map[string]bool{
68+
"README.md": true,
69+
"main.go": true,
70+
},
71+
},
72+
},
73+
74+
run: `${{ search_results }}`,
75+
want: `README.md main.go`,
76+
},
6377
}
6478

6579
for _, tc := range tests {

internal/campaigns/service.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ query ChangesetRepos(
380380
...repositoryFields
381381
}
382382
... on FileMatch {
383+
file { path }
383384
repository {
384385
...repositoryFields
385386
}
@@ -404,13 +405,18 @@ func (svc *Service) resolveRepositorySearch(ctx context.Context, query string) (
404405
return nil, err
405406
}
406407

407-
ids := map[string]struct{}{}
408+
ids := map[string]*graphql.Repository{}
408409
var repos []*graphql.Repository
409410
for _, r := range result.Search.Results.Results {
410-
if _, ok := ids[r.ID]; !ok {
411+
existing, ok := ids[r.ID]
412+
if !ok {
411413
repo := r.Repository
412414
repos = append(repos, &repo)
413-
ids[r.ID] = struct{}{}
415+
ids[r.ID] = &repo
416+
} else {
417+
for file := range r.FileMatches {
418+
existing.FileMatches[file] = true
419+
}
414420
}
415421
}
416422
return repos, nil
@@ -442,12 +448,18 @@ func (sr *searchResult) UnmarshalJSON(data []byte) error {
442448

443449
switch tn.Typename {
444450
case "FileMatch":
445-
var result struct{ Repository graphql.Repository }
451+
var result struct {
452+
Repository graphql.Repository
453+
File struct {
454+
Path string
455+
}
456+
}
446457
if err := json.Unmarshal(data, &result); err != nil {
447458
return err
448459
}
449460

450461
sr.Repository = result.Repository
462+
sr.Repository.FileMatches = map[string]bool{result.File.Path: true}
451463
return nil
452464

453465
case "Repository":

0 commit comments

Comments
 (0)